Lesson 15 - Windows Forms - Dialogs
In the previous lesson, Other Windows Forms Controls, we took a look at the advanced WinForms
controls. In today's tutorial, we're going to take a look at creating menus in
windows form applications and opening files/folders using dialogs. And also how
to use MessageBox
. All this using C# .NET.
MenuStrip
This control provides a menu bar at the top of the application form. It's very easy to use. After adding it to the project, we just set the names and number of items, as shown in the picture. There's no need to set anything in the properties. Then we just double-click each option to set up an action that will be triggered when the user clicks on the specific option.
ToolStrip
ToolStrip
is very similar to the previous one,
MenuStrip
. However, it does nothing by itself as it only serves for
adding several other controls, e.g. Button
, Label
,
DropDownButton
(which is MenuStrip
version of
ComboBox
), Separator
(used to separate the controls),
and several others. Setting up those controls is very simple, too. We just
double-click the buttons again to write the handler code.
ContextMenuStrip
This control can be found in the tray icon article, it's here just for completeness' sake. It's basically a menu that appears after clicking on a certain component.
FolderBrowserDialog
If you want to work with folders in your app, this component is a necessity.
It provides simple and intuitive folder selection. The most important property
is RootFolder
, which specifies the default folder when the dialog
is opened. Moreover, if you set e.g. the Documents/
folder as the
RootFolder
, it's possible to select only subfolders of the
Documents/
folder, but you'll never be able to go up from the
folder. The ShowNewFolderButton
then specifies whether it's allowed
to create new folders.
We open the dialog from the code using the ShowDialog()
method,
then we get the selected folder from the SelectedPath
property.
After opening the dialog, it's necessary to ask whether it was closed by
confirmation, otherwise we'd perform the action even if the user closed the
dialog using the Close or the Cancel buttons. The ShowDialog()
method always returns the button by which the dialog was closed.
if (folderBrowseDialog1.ShowDialog() == DialogResult.OK) { string text = folderBrowserDialog1.SelectedPath; }
OpenFileDialog
So we've discussed opening folders, now it's time to discuss files. The
procedure is almost the same - we just add the component to the form again. The
InitialDirectory
property is worth mentioning, it's practically the
same property as RootFolder
of FolderBrowserDialog
.
Multiselect
then specifies whether multiple files can be selected.
Again, we open the dialog using the ShowDialog()
method, then we
get the selected file using the FileName
property.
SaveFileDialog
We use this dialog to save files. Among the properties, it's worth mentioning
FileName
, which is the chosen filename, and
InitialDirectory
, which is the directory where the dialog will be
located when opened. DefaultExt
then specifies the file extension.
OverwritePrompt
asks the user whether the file should be
overwritten if it already exists. Title
then sets the dialog title.
We open the dialog using the ShowDialog()
method again.
In case you want to perform other actions, use events. Specifically, the
FileOk
event, that is triggered when the file has been selected
successfully. We get the filename using the FileName
property.
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e) { File.Create(saveFileDialog1.FileName); }
ColorDialog
As you can see from the title, this control has something to do with colors.
This dialog allows the user to pick a color. Of all the properties, the most
important one is AllowFullOpen
, which determines whether it's
possible to "create" a custom color. The Color
property then
determines what color will be the default one when the dialog is opened. As
always, the dialog is opened using the ShowDialog()
method.
We then get the color from the Color
property. The code below
will fill the form background with the selected color.
if (colorDialog1.ShowDialog() == DialogResult.OK) { this.BackColor = colorDialog1.Color; }
FontDialog
This dialog is very similar to the one that selects a color. Except here the
user selects a font, as the control name suggests. Just for completeness' sake
let me say that we open it using the ShowDialog()
method. The
MinSize
and MaxSize
properties are important as these
limits the font size the user can choose for the text (0
is
unlimited). ShowEffect
then allows the user to toggle the underline
or strikethrough, and ShowColor
to select the font color.
We get the font using the Font
property. In case we want to get
the color as well, we must get it separately by using the Color
property. Here's an sample:
if (fontDialog1.ShowDialog() == DialogResult.OK) { label1.Font = fontDialog1.Font; label1.ForeColor = fontDialog1.Color; label1.Text = "This is the selected font"; }
MessageBox
Although this isn't a form control, it still deserves its place here. It's the pop-up dialog that shows up, for example, when you perform some non-standard operation, such as emptying the Recycle Bin.
The following code:
MessageBox.Show("This is the main text of the message box", "Window title", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
Results in showing the dialogue below:
The first text is the message itself, the second one is the window title, and
MessageBoxButtons
specifies the window buttons (we can choose from
OK, OK and Cancel, Yes and No, etc.). MessageBoxIcon
specifies the
icon on the left side of the text. Again, there are several options to choose
from. Some parameters can be omitted.
Surely it'd be good to be able to respond to the specific buttons of the
MessageBox
, right? Let's show how to do that.
It's not a rocket science. We just wrap the code into if
with
the == DialogResult.OK ` condition (where `OK
is the desired
button). The code in the body of the condition is then performed only when the
specific button is clicked.
Below is an example:
DialogResult dr = MessageBox.Show("Do you really want to delete this item?", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dr == DialogResult.OK) { // ... }
In the next lesson, Windows Forms Controls For The Fourth Time, we'll introduce advanced TextBoxes, a timer, and progressbar.