Try It
99
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 ButtonsAnchor properties to Top, Right. Set the TextBoxesAnchor 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
100
LESSON 8 Using standard dialogs
When the user clicks the Font button, display the
FontDialog, allowing the user to select the
font’s color. If the user clicks OK, set the form’s
Font property to the dialog’s Font value and
its
ForeColor property to the dialog’s Color property.
1. To allow the user to select the font’s color, set the dialog’s ShowColor property to True.
2. Use code similar to the following:
private void fontButton_Click(object sender, EventArgs e)
{
formFontDialog.Color = this.ForeColor;
formFontDialog.Font = this.Font;
if (formFontDialog.ShowDialog() == DialogResult.OK)
{
this.Font = formFontDialog.Font;
this.ForeColor = formFontDialog.Color;
}
}
When the user clicks the Folder button, display the
FolderBrowserDialog. Make the dialog
start browsing at MyComputer. If the user clicks OK, make the Folder
TextBox display the
dialog’s
SelectedPath property.
1. To start browsing at MyComputer, set the dialog’s RootFolder property to
MyComputer.
2. Use code similar to the following:
private void folderButton_Click(object sender, EventArgs e)
{
if (testFolderBrowserDialog.ShowDialog() == DialogResult.OK)
{
folderTextBox.Text = testFolderBrowserDialog.SelectedPath;
}
}
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.
1. Use the filter:
Text Files|*.txt|RTF Files|*.rtf|All Files|*.*
2. Use code similar to the following:
private void openFileButton_Click(object sender, EventArgs e)
{
if (testOpenFileDialog.ShowDialog() == DialogResult.OK)
{
openFileTextBox.Text = testOpenFileDialog.FileName;
testSaveFileDialog.FilterIndex = testOpenFileDialog.FilterIndex;
}
}
596906c08.indd 100 4/7/10 12:32:14 PM
Exercises
101
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.
1. Use the filter:
Text Files|*.txt|RTF Files|*.rtf|All Files|*.*
2. Use code similar to the following:
private void saveFileDialog_Click(object sender, EventArgs e)
{
if (testSaveFileDialog.ShowDialog() == DialogResult.OK)
{
saveFileTextBox.Text = testSaveFileDialog.FileName;
testOpenFileDialog.FilterIndex = testSaveFileDialog.FilterIndex;
}
}
When the user clicks the Print button, display the
PrintDialog. Ignore the return result.
1. Use code similar to the following:
private void printDialog_Click(object sender, EventArgs e)
{
testPrintDialog.ShowDialog();
}
When the user clicks the Print Preview button, display the
PrintPreviewDialog. Ignore the
return result.
1. Use code similar to the following:
private void printPreviewDialog_Click(object sender, EventArgs e)
{
testPrintPreviewDialog.ShowDialog();
}
Please select Lesson 8 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. (SimpleEdit) Copy the SimpleEdit program you built in Lesson 7, Exercise 2 (or download
Lesson 7’s version from the book’s web site) and add the file open and save dialogs for the File
menu’s Open and Save As commands. Use
Filter properties that let the user select RTF files,
text files, or all files. Continue using the
RichTextBox’s LoadFile and SaveFile methods even
though they won’t really work properly for non-RTF files.
596906c08.indd 101 4/7/10 12:32:14 PM
102
LESSON 8 Using standard dialogs
2. (SimpleEdit) Copy the SimpleEdit program you built for Exercise 1 and add a font selection
dialog for the Format menu’s Font item, and the font tool strip button. If the user selects a
font and clicks OK, set the form’s font to the selected font.
3. (SimpleEdit) Copy the SimpleEdit program you built for Exercise 2 and add color selection dia-
logs for the Format menu’s Text Color and Background Color items. If the user selects a color
and clicks OK for these, set the corresponding menu items’
Image properties to the special value
null, so they don’t display any sample. (Displaying a sample of an arbitrary color is outside the
scope of this lesson.)
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find those
solutions in the SimpleEdit folder within the Lesson08 folder.
596906c08.indd 102 4/7/10 12:32:15 PM
Click here to Play
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.122.124