Sams Teach Yourself .Net in 21 Days
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You
- Introduction
- Week 1: At a Glance
- Day 1. Introduction to the Microsoft .NET Framework
- Day 2. Introduction to Visual Studio .NET
- Day 3. Writing Windows Forms Applications
- Day 4. Deploying Windows Forms Applications
- Day 5. Writing ASP.NET Applications
- Day 6. Deploying ASP.NET Applications
- Day 7. Exceptions, Debugging, and Tracing
- Week 1. In Review
- Week 2: At a Glance
- Day 8. Core Language Concepts in Visual Basic .NET and C#
- Day 9. Using Namespaces in .NET
- Day 10. Accessing Data with ADO.NET
- Day 11. Understanding Visual Database Tools
- Day 12. Accessing XML in .NET
- Day 13. XML Web Services in .NET
- Day 14. Components and .NET
- Week 2. In Review
- Week 3: At a Glance
- Day 15. Writing International Applications
- Day 16. Using Macros in Visual Studio .NET
- Day 17. Automating Visual Studio .NET
- Day 18. Using Crystal Reports
- Day 19. Understanding Microsoft Application Center Test
- Day 20. Using Visual SourceSafe
- Day 21. Object Role Modeling with Visio
- Week 3. In Review
Using Common Dialog Controls
The Common Dialog controls in Windows Forms enable you to perform dialog-related tasks. Table 3.6 lists the available Common Dialog controls.
Table 3.6. Common Dialog Controls
|
Control |
Description |
|
ColorDialog |
Displays the color picker dialog box that enables users to set the color of an interface element |
|
FontDialog |
Displays a dialog box that enables users to set a font and its attributes |
|
OpenFileDialog |
Displays a dialog box that enables users to navigate to and select a file |
|
PrintDialog |
Displays a dialog box that enables users to select a printer and set its attributes |
|
PrintPreviewDialog |
Displays a dialog box that shows how a PrintDocument object appears when printed |
|
SaveFileDialog |
Displays a dialog box that allows users to save a file |
The Common Dialog controls are nonvisual controls that are added to a form. After you add a control, you can use its properties and methods to display the dialog. Listing 3.6 uses the Filter property of OpenFileDialog to prompt the user for cursor files.
Example 3.6. Using OpenFileDialog
' Display an OpenFileDialog so the user can select a Cursor.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Cursor Files|*.cur"
openFileDialog1.Title = "Select a Cursor File"
' Show the Dialog.
' If the user clicked OK in the dialog and
' a .CUR file was selected, open it.
If openFileDialog1.ShowDialog() = DialogResult.OK Then
If openFileDialog1.FileName <> "" Then
' Assign the cursor in the Stream to the Form's Cursor property.
Me.Cursor = New Cursor(openFileDialog1.OpenFile())
End If
End If
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Cursor Files|*.cur";
openFileDialog.Title = "Select a Cursor File";
if (openFileDialog.ShowDialog() == DialogResult.OK &&
StringType.StrCmp(openFileDialog.FileName, "", false) != 0)
{
base.Cursor = new Cursor(openFileDialog.OpenFile());
}
You can also read files using a combination of the System.IO classes (you'll learn about them next week) and OpenFileDialog, as Listing 3.7 demonstrates.
Example 3.7. Opening a File with a StreamReader and OpenFileDialog
' Simple Open
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
MessageBox.Show(sr.ReadToEnd)
sr.Close()
End If
In the Downloads section for the day, I've included a sample application that uses each of the dialogs in different ways.
More About Controls | Next Section

Account Sign In
View your cart