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.
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 |
Listing 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.
Listing 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.
• Table of Contents
• Index
Sams Teach Yourself Visual Studio® .NET 2003 in 21 Days
By
Jason Beres
Publisher
: Sams Publishing
Pub Date
: January 14, 2003
ISBN
: 0-672-32421-0
Pages
: 696
Sams Teach Yourself Visual Studio .NET in 21 Days will help developers that are new to application development and experienced developers understand how to use the .NET Framework and Visual Studio .NET to rapidly develop any type of computer application. The Visual Studio .NET development environment is the most comprehensive developer tool ever created, putting that together with the .NET Frameworks' Class Libraries, the developer has everything he or she needs to get up-to-speed on Microsoft's latest revolution in application development. This book will guide the developer through using the VS .NET IDE, the Visual Basic .NET and C# language, and the supporting tools available from Microsoft to create Windows and Web-based applications. The market is full of books that pretty much say the same thing, which is already available in the help files, the author of this book has written and deployed over a dozen successful applications using Visual Studio .NET and the .NET Framework. All of his expertise and experience is used to give you the most comprehensive title on using Visual Studio .NET.