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
-
-
AcceptButton.PerformClick();
-
Simply set the form's DialogResult property:
this.DialogResult = DialogResult.OK;
Then you can close the form using Close() as normal. -
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?
-
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.
-
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...
-
No, the sender will be the button you call PerformClick on. EDIT: What littleguru said.
-
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.
-
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.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.