Search code examples
c#visual-studio-2010dialogresult

Visual Studio c# DialogResult doesn't seem to work


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.


Solution

  • Use ShowDialog

    newReg = new frmRegisterNew();
    var dialogResult = newReg.ShowDialog();
    
    if(dialogResult==DialogResult.OK)
    {
        ....
    }