C# Listbox Item Double Click Event
Asked Answered
E

7

64

I have a list box with some items. Is there anyway I can attach a double click event to each item?

Item 1
Item 2
Item 3

If i was to double click Item 2, a Messagebox saying "Item 2" would pop up

How would i do this?

Esplanade answered 15/12, 2010 at 20:14 Comment(0)
T
138
void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    int index = this.listBox1.IndexFromPoint(e.Location);
    if (index != System.Windows.Forms.ListBox.NoMatches)
    {
        MessageBox.Show(index.ToString());
    }
}

This should work...check

Trouper answered 15/12, 2010 at 20:24 Comment(8)
e.Location!?!?!? Are you sure is there this method? visual studio didn't suggest me this method by ctrl+space!Loewe
@Milas: I was stumped by the same issue. Make sure you are using the MouseDoubleClick event (and hence MouseEventArgs) and NOT Doubleclick (which only has EventArgs).Stradivari
Why not rather use SelectedItem?Dehiscent
@Dehiscent When the user clicks on blank area of the list box, SelectedItem may not be null.Peskoff
What is IndexFromPoint ?Turfman
according to visual studio 'Listbox does not contain a definition for IndexFromPoint'Dessalines
This doesn't seem to work if the ListBox is scrolled. I got what I wanted by using the DoubleClick event (not MouseDoubleClick) and listBox1.SelectedItem = listBox1.SelectedItems[0]; (This is for WinForms.)Jarlathus
Actually I don't even need that. I just call the onOk event to be done with the ListBox and use the selected item. (My case is a single select ListBox.)Jarlathus
V
32

WinForms

Add an event handler for the Control.DoubleClick event for your ListBox, and in that event handler open up a MessageBox displaying the selected item.

E.g.:

 private void ListBox1_DoubleClick(object sender, EventArgs e)
 {
     if (ListBox1.SelectedItem != null)
     {
         MessageBox.Show(ListBox1.SelectedItem.ToString());
     }
 }

Where ListBox1 is the name of your ListBox.

Note that you would assign the event handler like this:

ListBox1.DoubleClick += new EventHandler(ListBox1_DoubleClick);

WPF
Pretty much the same as above, but you'd use the MouseDoubleClick event instead:

ListBox1.MouseDoubleClick += new RoutedEventHandler(ListBox1_MouseDoubleClick);

And the event handler:

 private void ListBox1_MouseDoubleClick(object sender, RoutedEventArgs e)
 {
     if (ListBox1.SelectedItem != null)
     {
         MessageBox.Show(ListBox1.SelectedItem.ToString());
     }
 }

Edit: Sisya's answer checks to see if the double-click occurred over an item, which would need to be incorporated into this code to fix the issue mentioned in the comments (MessageBox shown if ListBox is double-clicked while an item is selected, but not clicked over an item).

Hope this helps!

Vested answered 15/12, 2010 at 20:18 Comment(2)
Heyy, I just tried it and it does work... but if i select an itm then double click a blank part of the list box, the event still fires and shows the message box because the item is still selected even tho i didnt double click on it... any ideas? :/Esplanade
I tried this and had to use new MouseButtonEventHandler(ListBox1_NouseDoubleClick); for it to work on WPF.Cottle
C
16

I know this question is quite old, but I was looking for a solution to this problem too. The accepted solution is for WinForms not WPF which I think many who come here are looking for.

For anyone looking for a WPF solution, here is a great approach (via Oskar's answer here):

private void myListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DependencyObject obj = (DependencyObject)e.OriginalSource;

    while (obj != null && obj != myListBox)
    {
        if (obj.GetType() == typeof(ListBoxItem))
        {
             // Do something
             break;
         }
         obj = VisualTreeHelper.GetParent(obj);
    }
}

Basically, you walk up the VisualTree until you've either found a parent item that is a ListBoxItem, or you ascend up to the actual ListBox (and therefore did not click a ListBoxItem).

Calvities answered 29/10, 2013 at 17:39 Comment(1)
This appears to be the best solution given here for WPF, but how would I get only the text of the clicked on item? e.g. If I do obj.ToString(), I get "System.Windows.Controls.ListBoxItem: myItem", how would I get just myItem? I've tried to access it a few different ways, but I get the same thing as obj.ToString() gives. I know I can parse the string (obj.ToString().Split(' ')[1]), but I'm wondering if there's a cleaner solution.Suzy
M
9

For Winforms

private void listBox1_DoubleClick(object sender, MouseEventArgs e)
    {
        int index = this.listBox1.IndexFromPoint(e.Location);
        if (index != System.Windows.Forms.ListBox.NoMatches)
        {
            MessageBox.Show(listBox1.SelectedItem.ToString());
        }
    }

and

public Form()
{
    InitializeComponent();
    listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_DoubleClick);
}

that should also, prevent for the event firing if you select an item then click on a blank area.

Moffit answered 15/5, 2012 at 13:40 Comment(0)
S
2

It depends whether you ListBox object of the System.Windows.Forms.ListBox class, which does have the ListBox.IndexFromPoint() method. But if the ListBox object is from the System.Windows.Control.Listbox class, the answer from @dark-knight (marked as correct answer) does not work.

Im running Win 10 (1903) and current versions of the .NET framework (4.8). This issue should not be version dependant though, only whether your Application is using WPF or Windows Form for the UI. See also: WPF vs Windows Form

Sinclair answered 6/8, 2019 at 19:17 Comment(3)
Add OS and current software versions. (From Review).Pastry
OS: Win 10 1903, I'm not sure which software version would be necessary for this answer?Sinclair
I hope this is fine now? I actually would have liked to add this answer as an reply to the marked answer of @dark-knight but it didn't let me.Sinclair
M
1

This is very old post but if anyone ran into similar problem and need quick answer:

  • To capture if a ListBox item is clicked use MouseDown event.
  • To capture if an item is clicked rather than empty space in list box check if listBox1.IndexFromPoint(new Point(e.X,e.Y))>=0
  • To capture doubleclick event check if e.Clicks == 2
Mcgowen answered 25/3, 2018 at 13:37 Comment(0)
C
0

The post is old but there is a simple solution for those who need it

private void listBox1_DoubleClick(object sender, EventArgs e)
{
   if (listBox1.SelectedIndex > -1)
   {
      MessageBox.Show(listBox1.Items[listBox1.SelectedIndex].ToString());
   }
}
Cottony answered 23/9, 2022 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.