locked
Index of Item in ListView RRS feed

  • Question

  • Hi Guys,

    I am trying to somehow get the index of the item which has been clicked by the mouse (single click). The list view has around 20 items and i need to show the description of each item in a seperate textbox, that means i should know which instruction has been clicked and for that i need the index.

    I am using visual studio express edition 2005 and C++.

    Any suggestions how i can do it?

    Cheers!

    Wednesday, December 27, 2006 2:15 PM

Answers

  • You can use the SelectedIndexChanged event if the ListView's MultiSelect property is False.  The selected item index is SelectedIndices[0].  If MultiSelect is True, you can use the GetItemAt() method in the MouseDown event.
    Wednesday, December 27, 2006 4:13 PM
  •     private void listView1_MouseDown(object sender, MouseEventArgs e) {
          ListViewItem item = listView1.GetItemAt(e.X, e.Y);
          if (item != null) Console.WriteLine("Mouse at item #{0}", item.Index);
        }

    Wednesday, December 27, 2006 5:50 PM
  • Implement the CellMouseDown event.  You can pass the selected cell in the dialog's constructor.  For example:

    In Form1:
        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
          if (e.Button == MouseButtons.Right) {
            Form2 f2 = new Form2(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]);
            f2.ShowDialog();
          }
        }

    In Form2, the dialog:
      public partial class Form2 : Form {
        private DataGridViewCell mCell;
        public Form2(DataGridViewCell cell) {
          mCell = cell;
          InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
          mCell.Value = "nobugz waz here";
          this.DialogResult = DialogResult.OK;
        }
      }

    Wednesday, December 27, 2006 10:02 PM

All replies

  • You can use the SelectedIndexChanged event if the ListView's MultiSelect property is False.  The selected item index is SelectedIndices[0].  If MultiSelect is True, you can use the GetItemAt() method in the MouseDown event.
    Wednesday, December 27, 2006 4:13 PM
  • Thanks a lot!

    with multiselect false,it works fine.With MultiSelect true i have some problems.The arguements of GetItemAt() are position of the item.But I want to get the index where the mouse is currently located.How do i use GetItemAt() to get the index?

    Can u just write one line of code where i get index from GetItemAt(), it will be graet.

    Cheers

    Wednesday, December 27, 2006 5:46 PM
  •     private void listView1_MouseDown(object sender, MouseEventArgs e) {
          ListViewItem item = listView1.GetItemAt(e.X, e.Y);
          if (item != null) Console.WriteLine("Mouse at item #{0}", item.Index);
        }

    Wednesday, December 27, 2006 5:50 PM
  • excellent!Finally it works!

    Could u also give me a tip on Popups?!I want to generate a popup when i right click on a cell of data grid view and in the pop up i want to display some parameters which the user could gives values to and then when the user presses the button apply in the popup,these parameters( which are defined in a structure defined by me)should appear in the datagridview cell where he originally right clicked!

    Any idea how to go about it?

    Wednesday, December 27, 2006 9:36 PM
  • Implement the CellMouseDown event.  You can pass the selected cell in the dialog's constructor.  For example:

    In Form1:
        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
          if (e.Button == MouseButtons.Right) {
            Form2 f2 = new Form2(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]);
            f2.ShowDialog();
          }
        }

    In Form2, the dialog:
      public partial class Form2 : Form {
        private DataGridViewCell mCell;
        public Form2(DataGridViewCell cell) {
          mCell = cell;
          InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
          mCell.Value = "nobugz waz here";
          this.DialogResult = DialogResult.OK;
        }
      }

    Wednesday, December 27, 2006 10:02 PM
  •   

    Here's a simple solution using the ListView.MouseUp event:

           private void GetIndexOfSelectedItem(object sender, MouseEventArgs e)
            {
                 int i = lvwListView.Items.IndexOf(lvwListView.SelectedItems[0]);
            }

    I first tried the ItemSelectionChanged event which seemed to be the logical choice - it also works the first time you click a row/item, however as soon as you click another item you get an ArgumentOutOfRange exception, possibly because the statement in the procedure is triggered before the selection of a new item/row is registered. A bug? I don't know, but it doesn't seem logical to me.

    You kan use the ListView.KeyUp event the same way if you want to have keyboard (Up/Down keys) support.

     

    Friday, April 8, 2011 12:31 PM