locked
Double click on an item displayed in a ListBox RRS feed

  • Question

  • My application displays a modal dialog box that contains a ListBox object. When the user double clicks on an item in this list box, the following event handler is called:

            private void listPlanes_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                Close();
            }

    The code that displays the dialog box looks like this:

                using (SelectPlane dlg = new SelectPlane(planes))
                {
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        // do something with the selected item
                    }
                }

    How can I close the dialog so that I get a DialogResult.OK above?

    Thanks for your help,
    Mike
    Friday, June 12, 2009 9:40 PM

Answers

  • I'm assuming you have a form called SelectPlane, and on this form, you have some button that calls this.Close() to close the form.  In the handler, simply set this.DialogResult = DialogResult.OK before closing, and that will be returned from ShowDialog().

    public partial class ShowPlane : Form
    {
        // everything else
        private void OKButton_Click(object sender, EventArgs e)
        {
              this.DialogResult = DialogResult.OK;
              this.Close();
        
    }
    }
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF Forums Client
    • Marked as answer by mikeondmsn Saturday, June 13, 2009 3:08 PM
    Friday, June 12, 2009 9:49 PM

All replies

  • I'm assuming you have a form called SelectPlane, and on this form, you have some button that calls this.Close() to close the form.  In the handler, simply set this.DialogResult = DialogResult.OK before closing, and that will be returned from ShowDialog().

    public partial class ShowPlane : Form
    {
        // everything else
        private void OKButton_Click(object sender, EventArgs e)
        {
              this.DialogResult = DialogResult.OK;
              this.Close();
        
    }
    }
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF Forums Client
    • Marked as answer by mikeondmsn Saturday, June 13, 2009 3:08 PM
    Friday, June 12, 2009 9:49 PM
  • Don't call Close().  Simply assigning the DialogResult property is sufficient.

    Hans Passant.
    Saturday, June 13, 2009 1:33 AM
    Moderator