Building Custom Dialogs
The standard dialogs described in Lesson 8 make it easy to perform typical chores such as
picking files, folders, colors, and fonts. Those dialogs can get you pretty far, but sometimes
you may want a dialog that is customized for your application.
For example, you might want to display a dialog where the user can enter a new customer’s con-
tact information (name, address, phone number, and hat size). Its unlikely that any predefined
standard dialog could ever handle that situation.
Fortunately, its easy to build custom dialogs. All you need to do is build a new form as described
in Lesson 9, add a few buttons, and set a few properties.
In this lesson you learn how to build custom dialogs and make them as easy to use as the
standard dialogs that come with Visual C#.
MAKING CUSTOM DIALOGS
Building a custom dialog is pretty easy. Simply add a new form to your project as described in
Lesson 9 and give it whatever controls you need.
To allow the user to finish using the dialog, add one or more buttons. Some dialogs have a
single OK button. Others have OK and Cancel buttons, or some other combination of buttons.
Because you’re creating the dialog, you can give it whatever buttons you like.
By convention, the buttons go at the bottom of the dialog in the right corner. Figure 10-1
shows a very simple dialog that contains a single textbox where the user can enter a name.
To make using the dialog easier, you can set the forms
AcceptButton and CancelButton properties. These deter-
mine which button is triggered if the user presses [Enter] and
[Esc], respectively. Typically the
AcceptButton triggers the
dialog’s OK or Yes button and the
CancelButton triggers
the Cancel or No button.
FIGURE 101
10
596906c10.indd 115 4/7/10 12:32:23 PM
116
LESSON 10 Building Custom dialogs
Often dialogs set other properties to make them behave more like standard
dialogs. Some of these include:
Setting
FormBorderStyle to FixedDialog so the user cannot resize
the dialog.
Setting
MinimumSize and MaxiumSize to keep the dialog a reasonable size.
Setting
MinimizeBox and MaximizeBox to False so the user cannot
maximize or minimize the dialog.
Setting
ShowInTaskbar to False so the dialog doesn’t clutter up the
taskbar.
You can make the dialog even easier to use if you set the tab order so the focus
starts at the top of the form and works its way down. For example, if the dialog
contains Name, Street, City, State, and ZIP textboxes, the focus should move
through them in that order.
The user can press [Tab] to move between fields and can press [Enter] or [Esc]
when all of the values are filled in. An experienced user can fill in this kind of
dialog very quickly.
SETTING THE DIALOG RESULT
A program uses the ShowDialog method to display a dialog. This method returns a value that indi-
cates which button the user clicked. As explained in Lesson 8, the program can check that return
value to see what it should do with the dialogs results. The examples in Lesson 8 checked that
ShowDialog returned the value DialogResult.OK before processing the user’s selections.
The dialog form’s
DialogResult property determines what value the call to ShowDialog returns.
For example, you could use the following code to make the dialog’s OK
Button set the form’s
DialogResult property to DialogResult.OK to tell the calling program that the user clicked the
OK button:
// Return OK to ShowDialog.
private void okButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
Setting the form’s DialogResult property not only determines the return result, but it also closes
the dialog so the call to
ShowDialog returns and the calling code can continue.
596906c10.indd 116 4/7/10 12:32:23 PM
Using Custom Dialogs
117
That means you can set the dialog’s return result and close the dialog in a single line of code.
Typing one line of code should be no real hardship, but believe it or not, there’s an even easier
way to close the dialog.
If you set a
Button’s DialogResult property, the Button automatically sets the form’s DialogResult
property when it is clicked. For example, suppose you set the
cancelButton’s DialogResult prop-
erty to
DialogResult.Cancel. When the user clicks the Button, it automatically sets the forms
DialogResult property to DialogResult.Cancel so the form automatically closes. That lets you set
the return value and close the form without typing any code at all.
If you think setting one
Button property is still too much work, you can even avoid that, at least for
the Cancel button. When you set a form’s
CancelButton property, Visual C# automatically sets that
Button’s DialogResult property to DialogResult.Cancel.
Note that when you set the form’s
AcceptButton property, Visual C# does not automatically set the
Button’s DialogResult property. The assumption is that the OK Button might need to validate the
data the user entered on the form before it decides whether to close the dialog. For example, if the user
doesnt fill in all required fields, the OK
Button might display a message asking the user to fill in the
remaining fields instead of closing the dialog.
If you don’t want to perform any validation, you can simply set the OK
Button’s DialogResult
property to
DialogResult.OK.
USING CUSTOM DIALOGS
A program uses a custom dialog in exactly the same way that it uses a standard dialog. It creates, ini-
tializes, and displays the dialog. It checks the return result and takes whatever action is appropriate.
There’s a slight difference in how the program creates the dialog because you can add standard
dialogs to a form at run time and you can’t do that with custom dialogs. To use a custom dialog,
the code needs to create a new instance of the dialog’s form as described in Lesson 9.
The following code shows how a program might display a new customer dialog:
// Let the user create a new customer.
private void newCustomerButton_Click(object sender, EventArgs e)
{
// Create and display a NewCustomerForm dialog.
NewCustomerForm newCustomerDialog;
newCustomerDialog = new NewCustomerForm();
if (newCustomerDialog.ShowDialog() == DialogResult.OK)
{
// ... Create the new customer here ...
}
}
The code declares a variable to refer to the dialog and makes a new dialog. It displays the dialog by
using its
ShowDialog method and checks the return result. If the user clicks OK, the program takes
whatever steps are needed to create the new customer, such as adding a record to a database.
596906c10.indd 117 4/7/10 12:32:24 PM
118
LESSON 10 Building Custom dialogs
TRY IT
In this Try It, you build and use a simple custom dialog. The dialog lets you enter a name. If you
enter a non-blank value and click OK, the main form adds the name you entered to a
ListBox.
This Try It also gives you a little practice using the
ListBox control, showing how to add and
remove items.
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find
them in the Lesson10 folder in the download.
Lesson Requirements
In this lesson, you:
Create the main form shown on the bottom in Figure 10-2. Make the New Comedian
Button
be the form’s
AcceptButton and the Delete Comedian Button be the form’s CancelButton.
Create the dialog shown on the top in Figure 10-2. Set the
AcceptButton and CancelButton
properties in the obvious way.
FIGURE 102
Make the New Comedian
Button display the dialog. If the dialog returns DialogResult.OK,
add the new comedian’s name to the
ListBox.
Make the Delete Comedian
Button remove the currently selected comedian from
the
ListBox.
When the user clicks the dialog’s Cancel button, close the form and return
DialogResult.Cancel.
When the user clicks the dialog’s OK
Button, check the entered name’s length. If the length
is 0, display a message asking the user to enter a name. If the length is greater than 0, close
the form and return
DialogResult.OK.
596906c10.indd 118 4/7/10 12:32:24 PM
Try It
119
Hints
Use the
ListBox’s Items.Add method to add a new item.
Use the
ListBox’s Items.Remove method to remove the selected item (identified by the
SelectedItem property).
Check
nameTextBox.Text.Length == 0 to see whether the name entered on the dialog
is blank. You can use code similar to the following to take one action if the length is 0 and
another if it is not. Notice the new
else part of the if statement. If the condition is true,
then the statements after the
if clause are executed. If the condition is false, then the state-
ments after the
else clause are executed. (Lesson 18 covers if and else in more detail.)
if (nameTextBox.Text.Length == 0)
{
... Display a message here ...
}
else
{
... Return DialogResult.OK here ...
}
Don’t forget to set the
nameTextBox control’s Modifiers property to Public so the main
form’s code can use it.
Step-by-Step
Create the main form shown on the bottom in Figure 10-2. Make the New Comedian
Button
be the form’s
AcceptButton and the Delete Comedian Button be the form’s CancelButton.
1. Start a new project and add a Label, ListBox, and two Buttons roughly as shown in
Figure 10-2.
2. Set the ListBox’s Anchor property to Top, Bottom, Left, Right. Set the Buttons
Anchor properties to Top, Right.
3. Set the form’s AcceptButton property to the New Comedian Button. Set its
CancelButton property to the Delete Comedian Button.
Create the dialog shown on the top in Figure 10-2. Set the
AcceptButton and CancelButton
properties in the obvious way.
1. Open the Project menu and select Add Windows Form. Enter the name
NewComedianForm and click Add.
2. Add a Label, TextBox, and two Buttons roughly as shown in Figure 10-2.
3. Set the TextBox’s Anchor property to Top, Left, Right. Set the ButtonsAnchor
properties to
Bottom, Right.
4. Set the form’s AcceptButton property to the OK Button. Set its CancelButton prop-
erty to the Cancel
Button.
596906c10.indd 119 4/7/10 12:32:25 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.31.125