Question

I'm using visual studio 2010 frmMain has a "Register" button which calls another form newReg

This is the code for the button in frmMain that calls the second form. The problem is that the MessageBox("So Far So Good") never gets called. The dialogResult doesnt seem to be recognized.

private void btnRegisterNew_Click(object sender, EventArgs e)
{
   // newReg Constructor Call
   newReg = new frmRegisterNew();

   // show form
   newReg.Show();

   if (newReg.DialogResult.Equals(DialogResult.OK)) 
   {
      MessageBox.Show ("So Far So Good");
   }
}

The second form has some fields to fill in and a button "register". I've set the dialogResult of this button to 'ok' in the properties window and also, I think, in the code. When the "register" button in the second form is clicked it checks the input, tries to update a database and closes if successful. Here is that bit of code:

dbConnection db = new dbConnection();
db.dbConnect();
if (db.dbRegisterVehicle(txtNewReg.Text, txtNewMake.Text, txtNewModel.Text, txtNewColour.Text, OwnerID))
{
   // if insert worked close
   this.DialogResult = DialogResult.OK;
   this.Close();
}
db.dbDisconnect();

I'm sure what to try, or what I might be over looking.

Était-ce utile?

La solution

Use ShowDialog

newReg = new frmRegisterNew();
var dialogResult = newReg.ShowDialog();

if(dialogResult==DialogResult.OK)
{
    ....
}

Autres conseils

The ShowDialog method is a good way to go but be aware of the differences between Show and ShowDialog. The latter will be modal which means that you can't access your original form until the new form is closed. This is why it blocks the check and may or may not be what you want.

When you call Show, it doesn't block, so that's why your code was immediately checking to see if the DialogResult was equal to OK (it wasn't equal to OK because your new form had barely opened by the time the check was made).

The alternative to using ShowDialog, if you want to use Show, is to handle the new form's closed event.

frmRegisterNew newReg = new frmRegisterNew();    
newReg.FormClosed += (s, o) =>
    {
        if (newReg.DialogResult == DialogResult.OK)
        {
            MessageBox.Show ("So Far So Good");
        }
    };
newReg.Show();

This means that your code will continue to work and your new form will not be modal, but when the new form it closed, the FormClosed event handler will be fired. Don't worry if you're not familiar with the event handler notation above (they're called anonymous methods) but you can still use the event handler as normal.

newReg.FormClosed += new FormClosedEventHandler(newReg_FormClosed);

void newReg_FormClosed(object sender, FormClosedEventArgs e)
{
    MessageBox.Show ("So Far So Good");
}

Try instantiating the DialogResult class and using it this way:

DialogResult dr = new DialogResult();

newReg = new frmRegisterNew();
dr = frmResgisterNew.ShowDialog();
if ( dr == DialogResult.OK )
   //Take an action here.

Form.Show() is non-blocking and will return very quickly. Your check on newReg.DialogResult.Equals(DialogResult.OK)) will therefore occur before the user has had a chance to press the button. Furthermore, note this warning about closing the window:

If a Form is displayed as a modeless window, the value returned by the DialogResult property might not return a value assigned to the form because the form's resources are automatically released when the form is closed.

(via the Form.DialogResult Property msdn library page)

You can either call From.ShowDialog() or, if you need to keep interaction on the main form, pass a delegate for the other form to call when it's completed.


Edit: A couple of points to keep in mind:

  • In addition to the warning above about closing the form, you have to be careful about trying to access the contents from a method dispatched from newReg's message loop (including the function that called Close()) after it's been disposed.

  • If you end up using ShowDialog() instead of Show(), however, this.Close() will not dispose the form. In fact, it essentially does nothing since setting DialogResult to anything other than None will automatically hide the form. If you need deterministic clean-up (presumably why you're calling Close() in the first place), you should call newReg.Dispose() after you're done with it in btnRegisterNew_Click. Otherwise, the form will be disposed at some unpredictable time in the future (provided your application doesn't end abnormally in the interim).

  • If you use an anonymous function as mentioned by keyboardP, be aware that it can get hard to debug when something goes wrong (especially if you're relatively new to the language and framework).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top