Use standard dialogs in C#
To use a standard dialog, first place the dialog on a form and set any properties that you want to customize. For example, you may want to set a file dialog's Filter property. You may also want to set CheckFileExists for the OpenFileDialog and OverwritePrompt or CreatePrompt for the SaveFileDialog. (Although the defaults for those properties are sensible.)
You can create the dialogs in code at run time instead of placing them on a form but it's more convenient to use a form.
Using a dialog at run time is a three step process.
- Initialize the dialog. Set dialog properties to show the current settings. For example, set a FontDialog's Font property to the current font.
- Display the dialog and check the return result. Call the dialog's ShowDialog method and see if it returns OK, indicating that the user clicked OK.
- If the user clicked OK, use the dialog's values to do something. For example, make the form use the font selected in a FontDialog.
// Select the font.
private void btnFont_Click(object sender, EventArgs e)
{
// Initialize.
fdFont.Font = this.Font;
// Display and check result.
if (fdFont.ShowDialog() == DialogResult.OK)
{
// Take action.
this.Font = fdFont.Font;
}
}


Comments