Mihai
Mihai

Reputation: 111

C# - ListView : How to handle the mouse click event on a listViewItem?

Let's say I have a ListView on a form and it is populated with records. How can I do this : when I click (single click) on a row , something has to happen - for example MessageBox.Show("row selected");

How to make this happen? Do I need a mouse click event ? And how can I do this?

Upvotes: 3

Views: 22353

Answers (5)

Mahdi the great
Mahdi the great

Reputation: 13

The other solutions are almost correct, but everyone has forgotten one thing, You should add Event handler.

  private void listView1_Click(object sender, EventArgs e) {
  try {

     index = listView1.SelectedItems[0].SubItems[0].Text;

  } 
  catch {

  }
  /// Event handler
  this.listView1.Click += new System.EventHandler(this.listView1_Click);

Upvotes: 0

user13028588
user13028588

Reputation:

If you want to select listview item on mouse click over it try this.

 private void timeTable_listView_MouseUp(object sender, MouseEventArgs e)
        {
            Point mousePos = timeTable_listView.PointToClient(Control.MousePosition);
            ListViewHitTestInfo hitTest = timeTable_listView.HitTest(mousePos);



            try
            {
            int columnIndex = hitTest.Item.SubItems.IndexOf(hitTest.SubItem);
            edit_textBox.Text = timeTable_listView.SelectedItems[0].SubItems[columnIndex].Text;
            }
            catch(Exception)
            {

            }



        }

Upvotes: 0

Pierpaolo Simoncini
Pierpaolo Simoncini

Reputation: 11

To prevent unwished behavior on ListView with checkboxes my solution is:

private void lvMembers_MouseClick(object sender, MouseEventArgs e)
{
    for (int itemIndex = 0; itemIndex < lvMembers.Items.Count; itemIndex++)
    {
        ListViewItem item = lvMembers.Items[itemIndex];
        Rectangle itemRect = item.GetBounds(ItemBoundsPortion.Label);
        if (itemRect.Contains(e.Location))
        {
            item.Checked = !item.Checked;
            break;
        }
    }
}

Upvotes: 0

Barun
Barun

Reputation: 1915

@Tommy answer is for ListBox, this one is for ListView :

    private void listView1_MouseClick(object sender, MouseEventArgs e)
    {
        for (int i = 0; i < listView1.Items.Count; i++)
        {
            var rectangle = listView1.GetItemRect(i);
            if (rectangle.Contains(e.Location))
            {
                //Write your code here
                return;
            }
        }
    }

Upvotes: 1

Tommy
Tommy

Reputation: 3124

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var selectedItemText = (listBox1.SelectedItem ?? "(none)").ToString();
    MessageBox.Show("Selected: " + selectedItemText);
}

private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
    for (int i = 0; i < listBox1.Items.Count; i++)
    {
        var rectangle = listBox1.GetItemRectangle(i);
        if (rectangle.Contains(e.Location))
        {
            MessageBox.Show("Item " + i);
            return;
        }
    }

    MessageBox.Show("None");
}

Upvotes: 1

Related Questions