Trouble with Form.DialogResult
-
19-04-2021 - |
Pregunta
private void mnuCustomerAdd_Click(object sender, EventArgs e)
{
CustomerForm frmCust = new CustomerForm("Add A New Customer");
int index = lstCustomers.SelectedIndex;
if (index != -1)
frmCust.CustomerData = new Customer(customerMngr.GetCustomer(index).ContactData);
MessageBox.Show("dev1");
DialogResult dr = frmCust.ShowDialog();
if (dr == DialogResult.OK)
{
MessageBox.Show("dev2");
if (frmCust.ReadInput())
{
MessageBox.Show("dev3");
customerMngr.AddCustomer(frmCust.CustomerData);
}
else
MessageBox.Show("Please supply all necessary fields with the correct information");
}
UpdateCustomerList();
}
Don't understand what I'm doing wrong here, I want to execute the conditional statements if the user hits OK in the Form that appears at frmCust.ShowDialog()
.
At the moment I can only get to "dev1".
Solución
Perhaps your dialog isn't setting the dialog result. Make sure your OK and Cancel buttons have their DialogResult
properties set to what you expect.
Otros consejos
Be sure correctly assign DialogResult property of the Form
before it closed.
So it will be returned like a return value of ShowDilaog()
call.
There is another option too, is use AcceptButton and CancelButton, in order to handle corresponding Enter
and Cancel
keypress.
Place a Breakpoint (F9) on the line:
if (dr == DialogResult.OK)
When the dialog closes you will have chance to examine what dr
is set to.
To get the Dialog to return DialogResult.OK you can either set it in the OK button of the Dialog:
void buttonOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
Close();
}
Or you can do the option in Tigran's answer.