Make a dialog with standard dialog features in C#
Making a dialog is easy. Just add a new form to the project. Making a dialog that provides standard features (so it acts more or less like a standard dialog) only takes a little more work.
To do it right, you need to know a little about dialogs:
- A form's AcceptButton property determines the button that fires when the user presses Enter.
- A form's CancelButton property determines the button that fires when the user presses Enter.
- A form's DialogResult property determines the value that is returned when you call ShowDialog for the dialog. This can have values such as OK, Cancel, Retry, etc.
- If you set the form's DialogResult property at run time, the form automatically hides and returns the appropriate value to ShowDialog.
- If you set a button's DialogResult property to a value, then when the user clicks that button, the button sets the form's DialogResult value (which hides the form).
- If you make a button the CancelButton, Visual Studio sets that button's DialogResult property to Cancel.
- If you make a button the AcceptButton, Visual Studio does not automatically set that button's DialogResult property to OK. The presumption is that you will want to validate the form before hiding it. If you don't need to validate anything and just want to return OK, you can set the button's DialogResult property yourself.
- Add a new form to the project. Give it OK and Cancel buttons.
- Set the form's AcceptButton property to the OK button. Set its CancelButton property to the Cancel button.
- If you don't need to validate the user's selections, set the OK button's DialogResult property to OK and you're done!
- If you do need to validate the user's selections, give the OK button a Click event handler that validates the user's selections. If everything is okay, make the code set the form's DialogResult property to OK. If there's something wrong with the selections, complain to the user and exit the event handler.
// Validate the user's entries.This dialog also hides the dialog's default Show method so the main program cannot use it.
private void btnOk_Click(object sender, EventArgs e)
{
if (txtFirstName.Text.Length < 1)
{
MessageBox.Show("You must enter a First Name.",
"Invalid Name", MessageBoxButtons.OK,
MessageBoxIcon.Error);
txtFirstName.Focus();
}
else if (txtLastName.Text.Length < 1)
{
MessageBox.Show("You must enter a Last Name.",
"Invalid Name", MessageBoxButtons.OK,
MessageBoxIcon.Error);
txtLastName.Focus();
}
else
{
// It's ok. Close the dialog.
DialogResult = DialogResult.OK;
}
}
// Replace Show so the program cannot use it.The following code shows how the main program uses the dialog.
public new void Show()
{
throw new InvalidOperationException(
"Use ShowDialog not Show to display this dialog");
}
// Display the dialog.
private void btnSetName_Click(object sender, EventArgs e)
{
NameDialog dlg = new NameDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
// The user clicked OK. Save the values.
txtFirstName.Text = dlg.txtFirstName.Text;
txtLastName.Text = dlg.txtLastName.Text;
}
}



Comments