Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent ShowDialog() from returning when OK button is clicked

I have a dialog that I want to prevent from closing when the OK button is clicked, but it returns, and that even if the AcceptButton property is set to none instead of my OK button. What is the best way to stop it from closing?

like image 801
user1151923 Avatar asked Apr 26 '12 09:04

user1151923


4 Answers

In fact you are changing the wrong property. You certainly do want AcceptButton to be the OK button. This property determines which is the default button in Windows terms. That is the button which is pressed when you hit ENTER on your keyboard. By changing AcceptButton you are simply breaking the keyboard interface to your dialog. You are not influencing in any way what happens when the button is pressed.

What you need to do is set the DialogResult property of your button to DialogResult.None since that's what determines whether or not a button press closes the form. Then, inside the button's click handler you need to decide how to respond to the button press. I expect that, if the validation of the dialog is successful, you should close the dialog by setting the form's DialogResult property. For example

private void OKbuttonClick(object sender, EventArgs e)
{
    if (this.CanClose())
        this.DialogResult = DialogResult.OK;
}
like image 178
David Heffernan Avatar answered Oct 12 '22 23:10

David Heffernan


The best way to stop this behavior is changing the DialogResult property of your OK button to DialogResult.None in the property window at design time.

Also, If you have already some code in the click event of the OK button you could change the form DialogResult.

private void comOK_Click(object sender, EventArgs e)
{
    // your code .....

    // Usually this kind of processing is the consequence of some validation check that failed
    // so probably you want something like this
    if(MyValidationCheck() == false)
    {
        // show a message to the user and then stop the form closing with
        this.DialogResult = DialogResult.None;
    }
}
like image 21
Steve Avatar answered Oct 13 '22 01:10

Steve


You need to remove the DialogResult of the button itself as well, in the properties window on the button set it to None.

http://msdn.microsoft.com/en-us/library/system.windows.forms.button.dialogresult.aspx

If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events.

Obviously, now your button won't do anything so you will need to register a handler for the Click event.

like image 30
Adam Houldsworth Avatar answered Oct 13 '22 00:10

Adam Houldsworth


The best practice is to actually set the Ok button to be disabled rather than not respond to user input.

The DialogResult property SHOULD be set to Ok or Yes depending on the form and the AcceptButton should also be linked to Ok.

I normally create a function on all dialogs and call it whenever the user interacts with the data.

void RefreshControls() { button.Enabled = this.ValidateInput(); }

like image 41
Raheel Khan Avatar answered Oct 13 '22 01:10

Raheel Khan