Reputation: 331
I have a form that I use the ShowDialog() method to bring up so the user can't change control back to the main form, and on the sub form I have a MessageBox.Show() method call which returns a DialogResult.
Only problem is, no matter what the dialog result of the message box, it causes my sub form to close. Is there a behaviour I am overlooking or is there something the matter with my code?
The code in the main form to open the sub form:
private void btnScanFree_Click(object sender, EventArgs e)
{
frmScan scanForm = new frmScan();
scanForm.ShowDialog();
}
And the code in the cancel button click method on the sub form:
private void btnCancel_Click(object sender, EventArgs e)
{
if (dgvScannedItems.RowCount > 0)
{
DialogResult dr = MessageBox.Show("There are scanned items that have not been inserted to the database. Are you sure you want to go back?", "Go Back", MessageBoxButtons.YesNo);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
this.Close();
}
}
else
{
this.Close();
}
}
On the sub form, if there are no rows in the data grid view, then the form should close, otherwise a message box should appear with yes and no buttons and a question asking the user if they want to continue closing the form. But whether they press yes or no, it closes both the message box (which it always should) and the sub form (which half the time it should not).
Upvotes: 0
Views: 92
Reputation: 1124
BtnCancel
is a dialog Button and sets DialogResult
of the form to cancel or no or something similar. Since you have opened the form as a dialog via ShowDialog
setting the DialogResult
Causes the form to close and return the result.
So you need to set the DialogResult
property of the BtnCancel
to nothing to prevent this "starnge" behaviour.
Upvotes: 1
Reputation: 293
Why not add a watch on dgvScannedItems.RowCount
and see what the value is?
Upvotes: 0