When the user clicks the Open File button, display the
OpenFileDialog. Use a filter
that lets the user select text files, RTF files, or all files. If the user clicks Open, make the
Open File
TextBox display the dialog’s FileName property and set the SaveFileDialog’s
FilterIndex equal to the OpenFileDialog’s FilterIndex.
When the user clicks the Save File button, display the
SaveFileDialog. Use a filter similar
to the one used by the
OpenFileDialog. If the user clicks Save, make the Save File TextBox
display the dialog’s
FileName property and set the OpenFileDialog’s FilterIndex equal to
the
SaveFileDialog’s FilterIndex.
When the user clicks the Print button, display the
PrintDialog. Ignore the return result.
When the user clicks the Print Preview button, display the
PrintPreviewDialog. Ignore the
return result.
Hints
Be sure to initialize each of the dialogs before displaying them.
Step-by-Step
Use
Labels, TextBoxes, and Buttons to make a form similar to the one shown in Figure 8-4.
1. Add and arrange the controls in whatever manner you find easiest.
2. Set the Buttons’ Anchor properties to Top, Right. Set the TextBoxes’ Anchor properties
to
Top, Left, Right.
Add
ColorDialog, FontDialog, FolderBrowserDialog, OpenFileDialog,
SaveFileDialog, PrintDialog, and PrintPreviewDialog components to the form.
1. Add the dialogs. They appear in the Component Tray, not on the form.
2. Give the dialogs good names.
When the user clicks the BackColor button, display the
ColorDialog but don’t allow the
user to define custom colors. If the user clicks OK, set the form’s
BackColor property to the
dialog’s
Color value.
1. To prevent the user from defining custom colors, set the ColorDialog’s
AllowFullOpen property to False.
2. Use code similar to the following:
private void backgroundColorButton_Click(object sender, EventArgs e)
{
backgroundColorDialog.Color = this.BackColor;
if (backgroundColorDialog.ShowDialog() == DialogResult.OK)
{
this.BackColor = backgroundColorDialog.Color;
}
}
596906c08.indd 99 4/7/10 12:32:14 PM