Search code examples
c#.netwinformsdialogresult

MessageBox not providing expected DialogResult


I have a simple OK/Cancel DialogResult instance within my form:

MessageBox.Show("Number of records affected:" + numberOfRecordsroll + " Please check the data is correct before proceeding:", "Please validate your changes", MessageBoxButtons.OKCancel);

DialogResult dr = new DialogResult();
if (dr == DialogResult.OK)
{
    // CommitTheChanges();
    MessageBox.Show("test", "test");
}
else if (dr == DialogResult.Cancel)
{
    //Do nothing
}

As you can see if the user clicks yes the method CommitTheChanges should be called, however this was not firing. I have since commented it out and placed a test MessageBox.Show(). Again this is not working. What am I doing wrong here?


Solution

  • You're just creating a new DialogResult and ignoring the click from the dialog. Instead you need to get the DialogResult from the MessageBox

     String msg = "Number of records affected:" + numberOfRecordsroll + " Please check the data is correct before proceeding:";
     DialogResult dr = MessageBox.Show(msg, "Please validate your changes", MessageBoxButtons.OKCancel);