Click here to Skip to main content
15,923,015 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hopefully this is a painfully simple question.

I have a listbox with 3 entries:

Dog
Cat
Fish

I have a string x.

When the user double-clicks on "Cat", I want a method/event handler to fire which sets x = "Cat". If they double-click on "Dog", I want x = "Dog".

Amazingly, I'm having a lot of trouble doing this. Just need a quick code sample.
Posted
Comments
Sergey Alexandrovich Kryukov 8-Jan-12 0:17am    
A lot of trouble? It's really amazing... and what kind of trouble do you have? Be so kind to share it with us, otherwise we won't be able to help you.
--SA

Handle the DoubleClick event for the listbox:
C#
private string x;
private void myListBox_DoubleClick(object sender, EventArgs e)
    {
    x = myListBox.SelectedItem.ToString();
    }
 
Share this answer
 
I'm going to respond to your question on a little "broader" level (once an educator, always an educator ?) than just the how of you get a specific DoubleClick EventHandler "wired-up" to a specific ListBox.

First, what are your goals here:

1. what happens if the user only single-clicks on an entry in the ListBox, but that single-click changes the current SelectedIndex Property of that ListBox: does that potential change in selection by single-click "mean" anything in the context of your solution ?

Please note that ... sometimes .... depending on external variables, system settings, mouse-type, how fast you click, how worn-down your mouse-buttons are, etc., it's possible to "have trouble" getting a double-click Event.

Try this WinForm experiment: put a ListBox (here called 'listBox1), with the same three Items in it (Dog Cat Fish) you are using now, and add a large-sizedTextBox with 'Multi-Line set to 'true, and set to show a vertical scrollbar, named 'tbEventReport.

Add this code to the Form that contains the 'listBox1 ListBox, and the 'tbEventReport TextBox:
C#
    private int eventId;

    private void ReportEvent(string eventName)
    {
        tbEventReport.Text +=
            "Event ID ="
            + eventId
            + " : "
            + eventName
            + " : "
            + "Selected Index = "
            // note that use of 'ToString() is not required here ...
            + listBox1.SelectedIndex
            + Environment.NewLine
            + "-----------------------------------------------------------------------------------------------------"
            +Environment.NewLine;

        eventId++;
    }

    private void listBox1_MouseClick(object sender, MouseEventArgs e)
    {
        ReportEvent("MouseClick");
    }

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        ReportEvent("MouseDoubleClick");
    }

    private void listBox1_Click(object sender, EventArgs e)
    {
        ReportEvent("Click");
    }

    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
        ReportEvent("DoubleClick");
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ReportEvent("SelectedIndexChanged");
    }
}
Do the right thing at design-time to "hook-up" those EventHandlers defined above to 'listBox1.

Then run the app, and experiment with clicking and double-clicking in a variety of ways: notice how clicking on the same item that's already selected still fires the SelectedIndexChanged EventHandler.

Getting "grounded" in the sequence and behavior of typical Mouse action generated Events will, I believe, equip you with a "mental toolkit" you can use to design many types of solution strategies in the future.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900