Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
171 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...