Tech Off Thread

9 posts

ShowDialog() return values (in C#)

Back to Forum: Tech Off
  • User profile image
    misterikkit

    So I'm using a dialog with a list of items in it. The user can select one item and press a select button, causing ShowDialog() in the parent form to return DialogResult.OK

    The reason DialogResult.OK gets returned is that in the form properties, I set the select button as the "AcceptButton" property. What I would like, though, is that when the user double clicks on an item in the list, that item is selected, the dialog closes, and ShowDialog() returns DialogResult.OK.

    Does anybody know how to cause ShowDialog() to return a DialogResult.OK from a doubleClick event handler?

    Thanks,
    -j

  • User profile image
    TommyCarlier

    AcceptButton.PerformClick();

  • User profile image
    mabster

    Simply set the form's DialogResult property:

    this.DialogResult = DialogResult.OK;

    Then you can close the form using Close() as normal.

  • User profile image
    jsampsonPC

    TommyCarlier wrote:
    AcceptButton.PerformClick();


    Does that send EventArgs regarding what you actually clicked if you do it that way? So you can tell which object was clicked, what it's .text value was, etc?

  • User profile image
    TommyCarlier

    PerformClick internally calls the protected method OnClick(EventArgs.Empty), which raises the Click-event. If you have an EventHandler on the Click-event of the button, the sender-argument of your handler will be the button that was clicked.

  • User profile image
    jsampsonPC

    TommyCarlier wrote:
    PerformClick internally calls the protected method OnClick(EventArgs.Empty), which raises the Click-event. If you have an EventHandler on the Click-event of the button, the sender-argument of your handler will be the button that was clicked.


    Ah, excellent. I'm assuming the sender object will be anything you double-click to invoke DialogResult.OK...

  • User profile image
    TommyCarlier

    No, the sender will be the button you call PerformClick on. EDIT: What littleguru said.

  • User profile image
    littleguru

    misterikkit wrote:
    So I'm using a dialog with a list of items in it. The user can select one item and press a select button, causing ShowDialog() in the parent form to return DialogResult.OK

    The reason DialogResult.OK gets returned is that in the form properties, I set the select button as the "AcceptButton" property. What I would like, though, is that when the user double clicks on an item in the list, that item is selected, the dialog closes, and ShowDialog() returns DialogResult.OK.

    Does anybody know how to cause ShowDialog() to return a DialogResult.OK from a doubleClick event handler?

    Thanks,
    -j


    You can also just set the DialogResult of the dialog to DialogResult.OK:

    this.DialogResult = DialogResult.OK;

    That closes the dialog and returns OK. It's nice, when you don't want the code of the accept button's Click event being executed, when the user double clicks the control.

  • User profile image
    littleguru

    jsampsonPC wrote:
    TommyCarlier wrote:PerformClick internally calls the protected method OnClick(EventArgs.Empty), which raises the Click-event. If you have an EventHandler on the Click-event of the button, the sender-argument of your handler will be the button that was clicked.


    Ah, excellent. I'm assuming the sender object will be anything you double-click to invoke DialogResult.OK...


    The sender is always the instance that fired the event. It is so defined in the event's design pattern for .NET.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.