I have created a button on a form for which I set a return value programatically, seen below. First, is the event handler psudocode, followed by where the dialog result is returned to.
There is a default property where one can set a button's return behavior in the user interface, i.e. the Dialog Result behavior. In my full code I cannot see anywhere this button's return is set or modified.
When testing (running in debug mode) the first time the buttonSaveSet_Click event handler is used during the execution of code the returned dialog result value is "Cancel" despite the fact that I clicked the "Set" button. However, the second time that I execute the function, by pressing the same button, the dialog result is returned as "Yes".
It seems like there is another place that the Dialog Result is being modified, and I am setting that value in the incorrect location.
psudo code
private void buttonSaveSet_Click( object sender , EventArgs e )
{
setChars = new setChars();
//set the dr to yes.
buttonSaveSet.DialogResult = DialogResult.Yes;
// set the charCount
// set the rowCount
if ( conditional statement is true )
{
//return values;
}
else
{
//return nothing;
}
Close();
}
return location:
try
{
DialogResult dResult = setValPopup.ShowDialog();
SetChars sc = setValPopup.setChars;
int max;
if ( dResult == DialogResult.Yes )
{
if ( sc.set == true )
{
//do other work
}
}
}
You should set the DialogResult property of the form to exit. Any value but DialogResult.None will force the form to close and return whatever you set as DialogResult (on the form, not on the button)
private void buttonSaveSet_Click( object sender , EventArgs e )
{
setChars = new setChars();
this.DialogResult = DialogResult.Yes;
....
// No need to call Close here
// Close();
}
The behavior you observe is due to the fact that probably the form engine examines the DialogResult property of the button before entering the click event and it is not expected to reevaluate it again at the exit from the event. Thus, your first click sets the property on the button, at the second click the property on the button is noted by the form engine and everything closes.