[ACCEPTED]-Determine clicked column in ListView-listview
Accepted answer
Jeez, everyone's too lazy to post code. There 2 are three steps to the process:
- Get the mouse position using
Control.MousePosition
and convert to client coordinates. - Call the
HitTest
function to find what the mouse is pointing to. This returns an object with lots of information except the actual column number so... - Search the subitems array using
IndexOf
to find the column number.
Here's the 1 code:
private void listViewMasterVolt_DoubleClick(object sender, EventArgs e)
{
Point mousePosition = myListView.PointToClient(Control.MousePosition);
ListViewHitTestInfo hit = myListView.HitTest(mousePosition);
int columnindex = hit.Item.SubItems.IndexOf(hit.SubItem);
}
The ListView
control has a HitTest
method. You give it 4 the x- and y-coordinates of the mouse click 3 event, and it gives you an object that tells 2 you the row (list view item) and column 1 (list view subitem) at that point.
This is VB.NET code, but the objects should 1 be the same.
Private LVUsersLastHit As Point
Private Sub lvUsers_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvUsers.MouseUp
Me.LVUsersLastHit = e.Location
End Sub
Private Sub LvUsers_Doubleclick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvUsers.DoubleClick
Dim HTI As ListViewHitTestInfo = Me.lvUsers.HitTest(Me.LVUsersLastHit)
If HTI.Item Is Nothing OrElse HTI.SubItem Is Nothing Then Exit Sub 'nothing was dblclicked
MsgBox("doubleClicked the " & HTI.Item.ToString & " Item on the " & HTI.SubItem.ToString & " sub Item")
End Sub
the e.Column actually holds the index
private void lv_ColumnClick(object sender, ColumnClickEventArgs e)
{
Int32 colIndex = Convert.ToInt32(e.Column.ToString());
lv.Columns[colIndex].Text = "new text";
}
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.