Almost all applications need to ask users to open or save a file. You may want to start building your own form to allow it but this feature is directly available from Windows. It is accessible through a single Common Dialog control from VB5/6 or through specialized controls from Visual Studio .Net. The best thing among all from these standard controls is that users already know how to use them because these controls are the same that they already use from Word, Excel and a great majority of applications.
This month column will introduce you to four of the common dialogs available from the .Net framework. Namely, we will explore the Open File dialog, the Save File dialog, the Color dialog and finally the Font dialog. These controls won't do the action for you (for example, the file won't be saved after the dialog is closed). You still have to write the code to save the file but you don't have to write the code to select the filename and path.
To demonstrate these four dialogs in action, why not create our own low-featured WordPad-like text editor?
Interface creation
The first step we will do is to create the interface itself. To demonstrate the four dialogs, we need a RichTextBox control and four Buttons. Change the caption (the Text property) of the buttons to Open, Save, Font, and Color). Also change the name of controls to btnOpen, btnSave, btnFont and btnColor respectively. You can also empty the Text property of the RichTextBox control. While at it, you can anchor the four buttons to the bottom of the form (if you used the same layout as shown here). The RichTextBox control should be anchor top, bottom, left and right. Lastly, why not set the Form's MinimumSize property to be sure that all the controls will be available all the time?
Figure 1: Our simple Notepad replacement
There are two ways of working with these dialogs. The first way, probably the most intuitive way is to drag a dialog instance (ColorDialog, FontDialog, OpenFileDialog, or SaveFileDialog control) from the Toolbox to the form. You can then set dialog properties from the Property Window just like any other control. You can also set the properties by code. The other way is the way I prefer the most is create it by code. Surprisingly, it is not more difficult. It is just a matter of declaring an object variable. You have to set dialog properties by code. This is the way I like best because it is easy to copy to any other project (simply copying some lines and you are all set!). This is also how I will show you how to use the controls.
The Save File dialog
I know that we usually start with the Open dialog but, when you will first test the application, you will surely write a couple of lines yourself and want to save them right away to see if its working! That's why I like to start with the Save feature! Here is the complete code required to give the user the possibility to save the content of the RichTextBox control as either a rich text file (.rtf) or as a plain text file (.txt).
Private Sub btnSave_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnSave.Click Dim o As New SaveFileDialog() o.Title = "Save your file" o.Filter = "Text file (*.txt)|*.txt|Rich text file (*.rtf)|*.rtf" o.InitialDirectory = "c:\temp" If o.ShowDialog = DialogResult.OK Then If o.FileName <> "" Then Select Case o.FilterIndex Case 1 RichTextBox1.SaveFile(o.FileName, _ RichTextBoxStreamType.PlainText) Case 2 RichTextBox1.SaveFile(o.FileName, _ RichTextBoxStreamType.RichText) End Select End If End If o.Dispose() o = Nothing End Sub
After setting the properties, you are ready to display the dialog to the user. You normally use the ShowDialog method that returns a Ok or a Cancel result. Do you remember in VB6 we need to trap the error to know when the user cancelled the dialog?
The FileName property contains the complete path and filename the user just select. The FilterIndex indicates the selected value of the "Save as type" combo box. Be warned that the first index is 1 (instead of 0 like almost anywhere else!). It is interesting to have this property instead of trying to figure the type of file by the extension!
And that's it! For the demo, I use the SaveFile method of the RichTextBox control.
The Open File dialog
This dialog is pretty much like the previous in both appearance and features. Some of the neat features the dialog gives you are that it can validate for path and/or file existence with the use of the CheckPathExists (default is True) and CheckFileExists (default is False) Boolean properties. Another property you need to know is the MultiSelect (default is False) Boolean property that allows, as you surely guessed, the users to select multiple files. You will then be able to retrieve selected files from the FileNames array.
Back to our sample, the code to open a file is the following:
Private Sub btnOpen_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnOpen.Click Dim o As New OpenFileDialog() o.Title = "Select the file to open" o.Filter = "Text file|*.txt|Rich text file|*.rtf" o.InitialDirectory = "c:\temp" If o.ShowDialog = DialogResult.OK Then If o.FileName <> "" Then Select Case o.FilterIndex Case 1 RichTextBox1.LoadFile(o.FileName, _ RichTextBoxStreamType.PlainText) Case 2 RichTextBox1.LoadFile(o.FileName, _ RichTextBoxStreamType.RichText) End Select End If End If o.Dispose() o = Nothing End Sub
The Font dialog
This dialog lets the user select a font from a list and also some of its other attributes. You have some control on these other attributes. For example, you can limit the size of the font (using the MinSize and MaxSize properties), you can allow limit the use of effects like strikethrough and underline (using the ShowEffects property – default is True), you can show or hide the color selection (using the ShowColor property – default is False) and so on.
In our sample, I use this code:
Private Sub btnFont_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnFont.Click Dim o As New FontDialog() If o.ShowDialog = DialogResult.OK Then RichTextBox1.SelectionFont = o.Font End If o.Dispose() o = Nothing End Sub
The Color dialog
You can allow users to use custom colors (using the AllowFullOpen property – default is True). You can also limit the user to solid colors (using the SolidColorOnly property – default is False).
In our sample, the code is:
Private Sub btnColor_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnColor.Click Dim o As New ColorDialog() If o.ShowDialog = DialogResult.OK Then RichTextBox1.SelectionColor = o.Color End If o.Dispose() o = Nothing End Sub
Conclusion
As you can see, it is pretty easy to use these dialogs (Open File, Save File, Font and Color) into your applications. Much easier then creating your own!
I invite you to use the link below to download the source code of this project in which I have added printing and previewing features which I haven't explained here. David Foderick wrote a good article on this in the January 2003 edition of the UT Magazine.