locked
How to tell a click on which item, which column in a ListViewControl? RRS feed

  • Question

  • hi,

    How to tell a click on which item, which column in a ListViewControl?  I guess what I need to do is , get the mouse click event position, and test to see which item, which column has this position. But, don't know how to do it exactly.

    Thanks.

    Friday, November 10, 2006 4:10 PM

Answers

  • Try something like this:

        private void listView1_MouseDown(object sender, MouseEventArgs e) {
          ListViewItem item = listView1.GetItemAt(0, e.Y);
          if (item == null) return;
          int x = 0;
          for (int ix = 0; ix < listView1.Columns.Count; ++ix) {
            int w = listView1.Columns[ix].Width;
            if (e.X < x + w) {
              Console.WriteLine("You clicked in column {0}, {1}", ix+1, item.SubItems[ix].Text);
              break;
            }
            x += w;
          }
        }

    Friday, November 10, 2006 9:49 PM

All replies

  • You can use the GetItemAt() method which accepts the mouse coordinate relative to the listview control.
    Friday, November 10, 2006 4:46 PM
  • Try something like this:

        private void listView1_MouseDown(object sender, MouseEventArgs e) {
          ListViewItem item = listView1.GetItemAt(0, e.Y);
          if (item == null) return;
          int x = 0;
          for (int ix = 0; ix < listView1.Columns.Count; ++ix) {
            int w = listView1.Columns[ix].Width;
            if (e.X < x + w) {
              Console.WriteLine("You clicked in column {0}, {1}", ix+1, item.SubItems[ix].Text);
              break;
            }
            x += w;
          }
        }

    Friday, November 10, 2006 9:49 PM