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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In fact you are changing the wrong property. You certainly do want
AcceptButtonto 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 changingAcceptButtonyou 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
DialogResultproperty of your button toDialogResult.Nonesince 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’sDialogResultproperty. For example