Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Listbox Item Double Click Event

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?

like image 250
Ozzy Avatar asked Dec 15 '10 20:12

Ozzy


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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

like image 109
Dark Knight Avatar answered Sep 19 '22 11:09

Dark Knight


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!

like image 25
Donut Avatar answered Sep 17 '22 11:09

Donut