Thursday, February 18, 2010

Double-Click to Close WinForm ListBox

If you have a ListBox on a WinForm, and you want the double-click on a selected item to close the form, you need to create a handler for the ListBox double click event.
In the handling method, use the following code:

 private void listBox1_DoubleClick(object sender, EventArgs e)

        {

        if (listBox1.SelectedItem != null)

        {

            listBox1.FindForm().DialogResult = DialogResult.OK;

        }

        }

or, you could use a generic version of the above code:

 ListBox listBox = sender as ListBox ;

       if (listBox.SelectedItem != null)

       {

       listBox.FindForm().DialogResult = DialogResult.OK;

       }

0 comments:

Post a Comment