I am trying to run a simple code. I have created a Form with a ComboBox (DropDownStyle = DropDown), which is empty in the beginning, it's filled with two when the DropDown is opened.
This Form also has 2 Buttons, one of them is the proceedButton Button sown in code.
In this block of code I am checking if the ComboBox is empty and if it, I am prompting user to select an Item from the ComboBox.
When the item is selected, I want to click proceedButton and the Form should close, but it this action takes a second click.
private void exitButton_Click(object sender, EventArgs e)
{
exitButton.DialogResult = DialogResult.Cancel;
Debug.WriteLine("Cancel was clicked");
Close();
}
private void proceedButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(comboBox1.Text))
{
MessageBox.Show("Nothing was selected, please try again!");
}
else
{
proceedButton.DialogResult = DialogResult.OK;
Debug.WriteLine("Proceed was clicked");
}
}
Do you know why is that?
Button Controls have a DialogResult property you can set to one of the DialogResult values.
Clicking a Button that has its
DialogResultproperty set to a value that is notDialogResult.None, causes the Dialog to Close, also setting the value returned byForm.ShowDialog():The
DialogResultproperty of Buttons is usually set in the Designer or in the Form Constructor (same thing).DialogResult = DialogResult.Cancelis clicked, the Dialog is closed. The same value is returned byForm.ShowDiaog().DialogResult = DialogResult.OK.So, set these properties in the Designer, remove
Close()from theexitButtonButtonClickhandler (this action is already performed automatically).OKtwice because the first click just sets theDialogResultproperty, which is then acknowledged and performs its default action only in the second click.In the error condition of the
proceedButtonButtonClickhandler, setthis.DialogResult = DialogResult.None;to cancel the action, before you show the MessageBox.The code can then be changed in: