Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms Close() sets DialogResult to Cancel

Tags:

c#

winforms

If I call Close() in my WinForm, it seems that even though DialogResult is None at the time, right after I call Close() I see that it is set to Cancel.

Does this sound normal?

like image 533
Fernando Avatar asked Aug 23 '11 15:08

Fernando


People also ask

How do I close a form in Winforms?

To close a form, you just need to set the form's DialogResult property (to any value by DialogResult. None ) in some event handler. When your code exits from the event handler the WinForm engine will hide the form and the code that follows the initial ShowDialog method call will continue execution.

How do I completely close a form in C#?

When we need to exit or close opened form then we should use "this. Close( )" method to close the form on some button click event. When we are running a winform application & need to exit or close SUB APPLICATION or CURRENT THREAD then we should use "System.

What is DialogResult OK?

DialogResult. A DialogResult that represents the result of the form when used as a dialog box. Attributes. BrowsableAttribute.

What is error provider in C#?

ErrorProvider presents a simple mechanism for indicating to the end user that a control on a form has an error associated with it. If an error description string is specified for the control, an icon appears next to the control.


1 Answers

Or even easier, you can set DialogResult just after Close. For example, assuming ValidateSettings will show the user any problems with the form or return true otherwise:

    private void btnOK_Click(object sender, EventArgs e)     {         if (ValidateSettings())         {             SaveSettings();             Close();             DialogResult = DialogResult.OK;         }     } 
like image 159
John Lockwood Avatar answered Oct 20 '22 11:10

John Lockwood