1

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
                }
            }
        }
WholeMilk32
  • 13
  • 1
  • 3
  • The buttons DialogResult is copied to form.DialogResult before OnClick is executed not after. Changing it in the event before closing the form is to late. Try to set form.DialogResult not button.DialogResult. – Ralf Jan 19 '17 at 17:38

2 Answers2

0

Based on the behaviour you are decribing. It is likely that you have set the DialogResult property set on your designer, so the first time it executes, it runs whatever value has set on the designer, and on the following executions, it runs correcly as you expect.

Look at your designer and you´ll find the problem.

NicoRiff
  • 4,803
  • 3
  • 25
  • 54
  • That is what I initially thought too. I looked in the designer I did not find anything that would modify the result. When I used the user interface to set the return value there is a line of code added "this.buttonSaveSet.DialogResult = System.Windows.Forms.DialogResult.Yes;" that modified the dialog result. Maybe that is what you meant? Thanks for the answer though! I appreciate the help. – WholeMilk32 Jan 19 '17 at 18:58
0

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.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Steve, that did it. Thank you for the help. I ended up removing close() method and the result returned is "Yes" as I expected it to be. That is one thing that I suspected, the form closing before the value change has been recorded, but I did not put much though to it. – WholeMilk32 Jan 19 '17 at 18:55