How to get index of item in listView on MouseDown?

kutiJack
kutiJack used Ask the Experts™
on
How can I get index of a listView item, on which  MouseDown event  was performed?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You can use HitTest method of ListView.

private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
              ListViewHitTestInfo info  =   listView1.HitTest(e.X, e.Y);
/* info contain all information . info.Item  check that it is null or not. process */
        }

Open in new window

Commented:
As you want index you can do

private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
              ListViewHitTestInfo info  =   listView1.HitTest(e.X, e.Y);
              if (info.Item != null)
              {
                  int index = info.Item.Index;
              }
        }

Open in new window



to get the index on the listview..
try sumthing like this...


MsgBox Me.ListView1.SelectedItem.Index



game-master

Author

Commented:
Nicely simple and effective. Thank you!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial