Determine clicked column in ListView
Asked Answered
W

5

11

I need to get the column clicked in a ListView in C#

I have some sample code from How to determine the clicked column index in a Listview but I'm not sure how I should implement it.

Who answered 4/3, 2010 at 8:26 Comment(0)
A
20

Jeez, everyone's too lazy to post code. There are three steps to the process:

  1. Get the mouse position using Control.MousePosition and convert to client coordinates.
  2. 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...
  3. Search the subitems array using IndexOf to find the column number.

Here's the 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);
}
Accolade answered 22/4, 2012 at 23:29 Comment(3)
If you can't get this to work simply code the two last lines using e.Location in the MouseDown event!Phocis
If you have a checkbox in the first column (ListView.CheckBoxes = true, FullRowSelect = true) you might want to utilize if (mousePosition.X >= 20) {... toggle checkbox ...}Comprehensible
This don't work if you scroll down a littleEuropean
P
4

The ListView control has a HitTest method. You give it the x- and y-coordinates of the mouse click event, and it gives you an object that tells you the row (list view item) and column (list view subitem) at that point.

Primalia answered 4/3, 2010 at 8:39 Comment(1)
Sathish - it's one line of code; it has a call to listView.HitTest; you'll need to provide the X and Y coordinates of the mouse click.Primalia
D
1

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";
    }
Dade answered 7/8, 2014 at 10:38 Comment(1)
ColumnClick is only valid for clicking on the column headersPhocis
E
1

Maybe it's for the new frameworks or something, but with my VS2022 I can call the ColumnClick event and use e.Column and that gives me the index, 0 for first column, 1 for second column and so on.

example:

private void ListViewD_ColumnClick(object sender, ColumnClickEventArgs e)
{
    // MessageBox.Show("column index: " + e.Column);
    if(e.Column == 1) // column index
    {
        MessageBox.Show("Clicked on second column");
    }
}
European answered 26/10, 2022 at 21:5 Comment(0)
M
0

This is VB.NET code, but the objects should 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
Mallet answered 21/12, 2010 at 20:18 Comment(1)
if you want the column that was (Single) clicked use the Columnclick Event. From the code above i could imagine that you could get the index of the subitem from the items subitem array, then get the coresponding indexed item from the Listview's columns to retrieve the ColumnHeader object that was dblclicked (regardless of column re-ordering at run time)Mallet

© 2022 - 2024 — McMap. All rights reserved.