ListView 中如何获得选中的当前单元 我想用listview做一个工具,需要选中某一个单元,用的是详细信息显示,也就是作为一个表格来用,怎样才能通过鼠标事件知道是选中了这个单元格呢?高手指点! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以通过鼠标的坐标来得到其下是否正好有一个Item:ListView ls;ls.GetItemAt(x, y); listview.SelectedItems返回的是一个SelectedListViewCollention集合,应为可能选择多行嘛 是这样,我要在没有点columnheader的情况下想知道目前鼠标焦点在哪一列!也就是知道落在哪一个单元格! 使用ListViewItem的GetSubItemAt(x,y) 使用ListViewItem的GetSubItemAt(x,y)1.1里边好象没有这样一个函数啊!!!郁闷,昨天看到一帖子用位置来确定,还以为很好,没想到当有滚动条的时候就不好使了!!!private void m_ListViewInstance_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { //确定被选中的单元格 ListViewItem tlvi = m_ListViewInstance.GetItemAt(e.X, e.Y); int cout = 0; for ( int i = 0; i < m_ListViewInstance.Columns.Count; i++ ) { cout += m_ListViewInstance.Columns[i].Width; if ( true == ( cout > e.X)) { m_ColumnIndex = i; break; } } //确定被选中的单元格 } 没想到当有滚动条的时候就不好使了-------------------你可能需要使用AutoScrollPosition来偏移坐标。 ListView1.Items[ListView1.ItemIndex].CaptionListView1.GetItemAt(x,y); 谢谢大家伙,不过我还是没能找到解决方案!我的是Framework 1.1还希望大家再帮帮忙!!呵呵!楼上说的一些都没有那属性和方法!!!郁闷了!! 重写listview,如果嫌麻烦就用datagrid。有时它比listview好用的。 重写listview,如果嫌麻烦就用datagrid。有时它比listview好用的。重写也解决不了啊!!!如何能确定那一列?? <PRE lang=cs id=pre2 style="MARGIN-TOP: 0px">private bool HitTest(Point hitPoint, out int row, out int column){ const int LVM_GETSUBITEMRECT = 0x1038; //Is LVM_FIRST (0x1000) + 56 const int LVM_COLUMNORDERARRAY = 0x103B; //Is LVM_FIRST (0x1000) + 59 const int LVIR_BOUNDS = 0; bool retval = false; RECT subItemRect; row = column = -1; ListViewItem item = m_lvListView.GetItemAt(hitPoint.X, hitPoint.Y); if(item != null && m_lvListView.Columns.Count > 1) { if(m_lvListView.AllowColumnReorder) { int[] columnOrder = new int[m_lvListView.Columns.Count]; // Get the order of columns in case // they've changed from the user. if(SendMessage(m_lvListView.Handle, LVM_COLUMNORDERARRAY, m_lvListView.Columns.Count, columnOrder) != 0) { int i; // Get the subitem rectangles (except column 0), // but get them in the proper order. RECT[] subItemRects = new RECT[m_lvListView.Columns.Count]; for(i = 1; i < m_lvListView.Columns.Count; i++) { subItemRects[columnOrder[i]].top = i; subItemRects[columnOrder[i]].left = LVIR_BOUNDS; SendMessage(m_lvListView.Handle, LVM_GETSUBITEMRECT, item.Index, ref subItemRects[columnOrder[i]]); } // Find where column 0 is. for(i = 0; i < columnOrder.Length; i++) if(columnOrder[i] == 0) break; // Fix column 0 since we can't get // the rectangle bounds of it using above. if(i > 0) { // If column 0 not at index 0, set using the previous. subItemRects[i].left = subItemRects[i-1].right; subItemRects[i].right = subItemRects[i].left + m_lvListView.Columns[0].Width; } else { // Else, column 0 is at index 0, so use the next. subItemRects[0].left = subItemRects[1].left - m_lvListView.Columns[0].Width; subItemRects[0].right = subItemRects[1].left; } // Go through the subitem rectangle bounds and // see where our point is. for(int index = 0; index < subItemRects.Length; index++) { if(hitPoint.X >= subItemRects[index].left & hitPoint.X <= subItemRects[index].right) { row = item.Index; column = columnOrder[index]; retval = true; break; } } } } // No column reordering...much simpler. else { for(int index = 1; index <= m_lvListView.Columns.Count-1; index++) { subItemRect = new RECT(); subItemRect.top = index; subItemRect.left = LVIR_BOUNDS; if(SendMessage(m_lvListView.Handle, LVM_GETSUBITEMRECT, item.Index, ref subItemRect) != 0) { if(hitPoint.X < subItemRect.left) { row = item.Index; column = 0; retval = true; break; } if(hitPoint.X >= subItemRect.left & hitPoint.X <= subItemRect.right) { row = item.Index; column = index; retval = true; break; } } } } } return retval;}</PRE>http://www.codeproject.com/cs/miscctrl/CSharpHitTest.asp 既然是1.1的就这样吧。foreach(ListViewItem lvi in listView1.SelectedItems){ Console.WriteLine(lvi.Text); } 大家来支招吧!C# 字节编码成字符串 下面图片中划红线的是类吗? 是怎么建的? 急! 在线等! 弱弱的问下,啥是存储过程。 获取系统时间能否引发异常? C# 数据读取 父窗体弹出子窗体,子窗体弹处messagebox,为什么点击完messagebox确定按钮后子窗体也关闭了 什么是句柄 IntPtr 用Excel.Application打开Excel文件和SaveAs的问题 使用SqlDataReader的问题 win 10系统 Visual Studio 2015 访问不了代码路径请师傅们指点 正则表达式的使用 是否可以在listView控件中添加其他控件?
ls.GetItemAt(x, y);
也就是知道落在哪一个单元格!
郁闷,昨天看到一帖子用位置来确定,
还以为很好,没想到当有滚动条的时候就不好使了!!!private void m_ListViewInstance_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//确定被选中的单元格
ListViewItem tlvi = m_ListViewInstance.GetItemAt(e.X, e.Y);
int cout = 0;
for ( int i = 0; i < m_ListViewInstance.Columns.Count; i++ )
{
cout += m_ListViewInstance.Columns[i].Width;
if ( true == ( cout > e.X))
{
m_ColumnIndex = i;
break;
}
}
//确定被选中的单元格
}
-------------------
你可能需要使用AutoScrollPosition来偏移坐标。
ListView1.GetItemAt(x,y);
我的是Framework 1.1
还希望大家再帮帮忙!!
呵呵!
楼上说的一些都没有那属性和方法!!!
郁闷了!!
重写也解决不了啊!!!
如何能确定那一列??
{
const int LVM_GETSUBITEMRECT = 0x1038; //Is LVM_FIRST (0x1000) + 56
const int LVM_COLUMNORDERARRAY = 0x103B; //Is LVM_FIRST (0x1000) + 59
const int LVIR_BOUNDS = 0;
bool retval = false;
RECT subItemRect;
row = column = -1;
ListViewItem item = m_lvListView.GetItemAt(hitPoint.X, hitPoint.Y); if(item != null && m_lvListView.Columns.Count > 1)
{
if(m_lvListView.AllowColumnReorder)
{
int[] columnOrder = new int[m_lvListView.Columns.Count];
// Get the order of columns in case
// they've changed from the user.
if(SendMessage(m_lvListView.Handle,
LVM_COLUMNORDERARRAY, m_lvListView.Columns.Count,
columnOrder) != 0)
{
int i;
// Get the subitem rectangles (except column 0),
// but get them in the proper order.
RECT[] subItemRects = new RECT[m_lvListView.Columns.Count];
for(i = 1; i < m_lvListView.Columns.Count; i++)
{
subItemRects[columnOrder[i]].top = i;
subItemRects[columnOrder[i]].left = LVIR_BOUNDS;
SendMessage(m_lvListView.Handle,
LVM_GETSUBITEMRECT, item.Index,
ref subItemRects[columnOrder[i]]);
} // Find where column 0 is.
for(i = 0; i < columnOrder.Length; i++)
if(columnOrder[i] == 0)
break;
// Fix column 0 since we can't get
// the rectangle bounds of it using above.
if(i > 0)
{
// If column 0 not at index 0, set using the previous.
subItemRects[i].left = subItemRects[i-1].right;
subItemRects[i].right = subItemRects[i].left
+ m_lvListView.Columns[0].Width;
}
else
{
// Else, column 0 is at index 0, so use the next.
subItemRects[0].left = subItemRects[1].left -
m_lvListView.Columns[0].Width;
subItemRects[0].right = subItemRects[1].left;
} // Go through the subitem rectangle bounds and
// see where our point is.
for(int index = 0; index < subItemRects.Length; index++)
{
if(hitPoint.X >= subItemRects[index].left &
hitPoint.X <= subItemRects[index].right)
{
row = item.Index;
column = columnOrder[index];
retval = true;
break;
}
}
}
}
// No column reordering...much simpler.
else
{
for(int index = 1; index <= m_lvListView.Columns.Count-1;
index++)
{
subItemRect = new RECT();
subItemRect.top = index;
subItemRect.left = LVIR_BOUNDS;
if(SendMessage(m_lvListView.Handle,
LVM_GETSUBITEMRECT, item.Index, ref subItemRect) != 0)
{
if(hitPoint.X < subItemRect.left)
{
row = item.Index;
column = 0;
retval = true;
break;
}
if(hitPoint.X >= subItemRect.left & hitPoint.X <=
subItemRect.right)
{
row = item.Index;
column = index;
retval = true;
break;
}
}
}
}
}
return retval;
}</PRE>http://www.codeproject.com/cs/miscctrl/CSharpHitTest.asp
foreach(ListViewItem lvi in listView1.SelectedItems)
{
Console.WriteLine(lvi.Text);
}