Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Using the default .NET dialog boxes

See in this article the default dialog boxes of the .NET Framework, provided by the Visual Studio IDE.

It's common that many programmers that are beginning on .NET Framework experiment a hard time while using the standard dialog boxes available on Visual Studio. Many times, due lack of use, programmers with a little more experience don't know the functionaties of these components.

In the Visual Studio's Toolbox, in the Dialogs group, there're natively five components, as we can see on Figure 1: ColorDialog, FolderBrowserDialog, FontDialog, OpenFileDialog and SaveFileDialog.

ToolBox’s Dialogs Group

Figure 1: Toolbox's Dialogs group

On this article, we shall see some of the main properties of each one of these components and their use, giving a practical example in the languages C# and Visual Basic.

So, to get heated up, we'll create one Windows Forms Application and then add to the main form the following components, keeping their original names.

  • Button (Text = “Color Dialog”)
  • Button (Text = “Font Dialog”)
  • Button (Text = “Folder Browser Dialog”)
  • Button (Text = “Open File Dialog”)
  • Button (Text = “Save File Dialog”)
  • RichTextBox: Text = any content for test purposes
  • ColorDialog
  • FontDialog
  • FolderBrowserDialog
  • OpenFileDialog
  • SaveFileDialog

On this case, the form's appearance is the one in Figure 2. But the reader can organize the controls in any way.

Form's components' configuration

Figure 2: Form's components' configuration

Each one of the buttons will be used to demonstrate the functioning of one of the cited dialog components, as indicates its text. Then, the following Listings shows the code that must be added to the event Click of each button.

Color Dialog

This component exhibits a box color selection box. Some properties deserve to be noticed:

  • AllowFullOpen: indicates if the user will be able to open the personalized color selection palette, or just choose between one of the predefined ones.
  • Color: the color that must be selected by standard when the dialog box is open.
  • FullOpen: defines if the personalized color selection box will be automatically open.
  • ShowHelp: defines if the button "Help" will be exhibited. If defined as true, the click on the button "Help" must be treated by the event HelpRequest.

The Listings 1 and 2 shows us how to alter the color of the backgorund color of the Rich TextBox using this component. The code of the Listings must be inserted in the event Click of the first button.

Listing 1: Use of the ColorDialog in C#

if (colorDialog1.ShowDialog() == DialogResult.OK)
{
    richTextBox1.BackColor = colorDialog1.Color;
}

Listing 2: Use of ColorDialog in Visual Basic

If colorDialog1.ShowDialog = DialogResult.OK Then
richTextBox1.BackColor = colorDialog1.Color
End If

It's necessary to do the verification of the result of the method ShowDialog, because the color of the RichTextBox must only be changed if the user clicks OK. In case the button Cancelar (Cancel) was clicked on, the return of the method would be DialogResult.Cancel.

FontDialog

From the components' name we already know that it's used for exhibiting one window of fonts selection. The most used properties and that, thus the ones that deserve attention are:

  • Color: The font's color exhibited in the window (this color, however, does not define the color of the selected font, it must be used the property ForeColor of the controls to be modified).
  • Font: the standard selected font.
  • FontMustExists: when true, creates an error message in case the user picks one non existing font.
  • MaxSize: maximum size allowed for the selected font.
  • MinSize: minimum size allowed for the selected font.
  • ShowApply: when true, the button "Apply" will be exhibited in the window. In this case, the click on this button must be treated in the event Apply.
  • ShowHelp: defines if the button "Help" will be shown. If defines as true, the click on the button "Help" must be treated by the event HelpRequest.

The Listings 3 and 4 shows how to use this component to alter the font of the RichTextBox on runtime.

Listing 3: FontDialog's use in C#

if (fontDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.Font = fontDialog1.Font;
}

Listing 4: FontDialog's use in Visual Basic

If fontDialog1.ShowDialog = DialogResult.OK Then
richTextBox1.Font = fontDialog1.Font
End If

The same verification is done on the return of the method ShowDialog, this must be repeated for the other components, always for the same reason.

FolderBrowserDialog

The FowderBrowserDialog shows a directories selection window. Archives can't be selected by this component, only directories. The properties that deserve attention are:

  • Description: text to be exhibited in the dialog box.
  • RootFolder: Root diretory of selection, ergo, it's only possible to select this directory or its sub-directories.
  • SelectedPath: adress of the directory selected by default. It must obey the property above.
  • ShowNewFolderButton: defines when the button "create a new directory" will be exhibited, allowing that the user creates new directories using the dialog box.

The Listings 5 and 6 shows how to list the files of a directory using this component.

Listing 5: Use of the FolderBrowserDialog on C#

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.Text = String.Empty;
string folder = folderBrowserDialog1.SelectedPath;
       foreach (string s in System.IO.Directory.GetFiles(folder))
       {
       	richTextBox1.AppendText(s + Environment.NewLine);
}
}

Listing 6: use of the FolderBrowserDialog on Visual Basic.

If folderBrowserDialog1.ShowDialog = DialogResult.OK Then
richTextBox1.Text = String.Empty
       Dim folder As String = folderBrowserDialog1.SelectedPath
       For Each s As String In System.IO.Directory.GetFiles(folder)
       	richTextBox1.AppendText(s + Environment.NewLine)
Next
End If

On the codes above, it was used the method GetFiles of the class Directory, which explanation is not the focus of this article. However, the main part of this stretch is the dialog's execution and the obtaining of the selected directory through the property SelectedPath.

OpenFileDialog

This dialog box is used to select as existing file. The most used properties are the following:

  • CheckFileExists: defines if it'll be shown an error message in the selected file does not exist.
  • CheckPathExists: defines if it'll be shown an error message if the selected diretory does not exist.
  • DefaultExt: extension of file selected by default.
  • FileName: file's name selected by defalut.
  • Filter: extension filer to select of files in the format "Description of Filter|*.extension| All formats| *,*""
  • FilterIndex: filter selected by default.
  • MultiSelect: indicates when the user will be able to select more than one file.
  • ShowHelp: defines if the button "Help" will be exhibited. If defines as true, the click on the button "Help" musy be treated by the event HelpRequest.
  • Title: Dialog's box title.

The Listings 7 and 8 shows how to open a file and exhibit it on the RichTextBox using the OpenFileDialog.

Listing 7: use of the OpenFileDialog on C#

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName);
}

Listing 8: Use of the OpenFileDialog on Visual Basic

If openFileDialog1.ShowDialog = DialogResult.OK Then
richTextBox1.LoadFile(openFileDialog1.FileName)
End If

By default, the method LoadFile of the RichTextBox only allows the reading of files of RTF extension. As the focus of this article is not the loading and exhibition of text files, we'll use only this example.

In case the property MultiSelect was marked as true, we should use the property FileName to read the many files selected.

SaveFileDialog

This last component is used to work with the file recording (saving). It's properties are, in the major part, equals to the ones of the OpenFileDialog, just one deserves a differentiated attention.

OverwritePrompt: defines if it'll be shown a message of confirmation in case the user tries to save a file that has a name that already exists.

One example of this component's use to save the content of the RichTextBox is shown on the Listings 9 and 10.

Listing 9: Use of the SaveFileDialog on C#

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName);
}

Listing 10: Use of the SaveFileDialog on VisualBasic

If saveFileDialog1.ShowDialog = DialogResult.OK Then
richTextBox1.SaveFile(saveFileDialog1.FileName)
End If

To make more practical the execution of this example, we can define the property Filter of the SaveFileDialog as "File RTF|*.rtf". This way, the rtf extension will be selects as default.

Conclusion

We have seen on this article that's not a complex task to use the standard dialog boxes available on Visual Studio. We have met their main properties and practical examples of use.

I hope This content may be of use. Thanks for the reader's attention. See you next time.



Microsoft MVP, Bachelor in Science And Technology, Editor and Consultant at DevMedia with more than 7 years of experience in software development.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login