Reputation: 123
I got some probs with the close button on my form. it used to be set as cancel button of the form, but I change it to none. The form works like guide...
I want to popup yes-no-messagebox if the form is in some step of guide. But with code I have now it is closing the form whatever I click yes or no.
Code of button:
if (krok <= 5)
{
if (MessageBox.Show("este ste neurobili vsetko naozaj chcete zavriet okno?",
"Pozor",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) == DialogResult.Yes)
{
Close();
}
}
else Close();
Upvotes: 1
Views: 165
Reputation: 216343
Every button has a property called DialogResult
. You can set this property directly in the designer window or in the code. The effect of this property is to close the form and (if the form is shown modally) return the value of the property to the form calling code.
In your case, I suspect that this property is set (in designer window) to something different from DialogResult.None
. If this is the case then your form will close whater you call Close() or not.
Upvotes: 1
Reputation: 8252
The debugger is your friend. Make your life easier and break out the ShowDialog line.
var result = MessageBox.Show...
if (result == DialogResult.Yes)
{
Close();
}
Check the value of result in the debugger. You can also put a breakpoint on the line with Close() and see if it gets hit. Then you'll know for sure if that line or something else is closing your form.
Upvotes: 1