120
LESSON 10 Building Custom dialogs
Make the New Comedian
Button display the dialog. If the dialog returns DialogResult.OK,
add the new comedian’s name to the
ListBox.
1. Create an event handler for the New Comedian Button. Use code similar to the
following:
// Create a new comedian entry.
private void newComedianButton_Click(object sender, EventArgs e)
{
NewComedianForm newComedianDialog;
newComedianDialog = new NewComedianForm();
if (newComedianDialog.ShowDialog() == DialogResult.OK)
{
// Add the new comedian.
comedianListBox.Items.Add(
newComedianDialog.nameTextBox.Text);
}
}
Make the Delete Comedian
Button remove the currently selected comedian from the
ListBox.
1. Create an event handler for the Delete Comedian Button. Use code similar to the
following:
// Remove the currently selected comedian.
private void deleteComedianButton_Click(object sender, EventArgs e)
{
comedianListBox.Items.Remove(comedianListBox.SelectedItem);
}
This makes the ListBox remove the currently selected item. Fortunately if there is no
selected item, the
ListBox does nothing instead of crashing.
When the user clicks the dialog’s Cancel button, close the form and return
DialogResult.Cancel.
1. You don’t need to do anything else to make this work. When you set the dialog’s
CancelButton property to this Button, Visual C# sets the Button’s DialogResult
property to
DialogResult.Cancel so the button automatically sets the return result
and closes the dialog.
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.
1. Create an event handler for the dialog’s OK Button. Use code similar to the following:
// Make sure the comedian’s name isn’t blank.
private void okButton_Click(object sender, EventArgs e)
{
if (nameTextBox.Text.Length == 0)
{
MessageBox.Show(“Please enter a comedian’s name”);
}
596906c10.indd 120 4/7/10 12:32:25 PM
Exercises
121
else
{
this.DialogResult = DialogResult.OK;
}
}
Please select Lesson 10 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Make a program that has First Name, Last Name, Street, City, State, and ZIP Labels as
shown on the ContactInformation form in Figure 10-3. When the user clicks the Edit
Button,
the program should display the Edit Contact Information dialog shown in Figure 10-3 to
let the user change the values. If the user clicks OK, the program copies the new values back
into the main form’s
Labels.
FIGURE 103
Hint: As you would with a standard dialog, initialize the custom dialog before you display it.
2. Sometimes the standard message box given by MessageBox.Show is almost perfect but you’d
like to change the
Buttons’ text. Create a program that defines the message dialog shown in
Figure 10-4.
FIGURE 104
596906c10.indd 121 4/7/10 12:32:25 PM
122
LESSON 10 Building Custom dialogs
The main program should set the Labels text, the dialog’s title, and the buttons’ text.
Make the Accept
Button return DialogResult.OK and make the Decline Button return
DialogResult.Cancel. Make the main form display different messages depending on
whether the user clicked Accept or Decline.
Hints:
The light gray area at the bottom of the dialog is a blank label with
AutoSize =
False
and Dock = Bottom. It’s just for decoration.
The question mark icon is displayed in a
PictureBox.
To give the dialog the right borders and system buttons, set the dialog’s properties:
FormBorderStyle = FixedDialog, MinimizeBox = False, and MaximizeBox =
False.
3. Create a color selection dialog like the one shown in Figure 10-5. The main program’s
Buttons should display the same dialog to let the user select foreground and background col-
ors. Only update the main form’s colors if the user clicks OK. Don’t worry about initializing
the dialog to show the current values before displaying it. (Hint: You built a program that
lets the user select colors with scroll bars for Lesson 4’s Try It.)
FIGURE 105
4. Make a background selection dialog like the one shown in Figure 10-6. When the user
clicks the main form’s Select Background
Button, the form displays the dialog. When the
user clicks one of the thumbnail images, the dialog displays a border around that image’s
PictureBox. If the user clicks OK, the dialog closes and the main form displays the selected
image at full scale.
Hints:
When the user clicks an image, set the
BorderStyle property to Fixed3D for that
PictureBox and None for the others.
To remember which image was selected, place a hidden
PictureBox on the dialog
and set its
Image property equal to that of the clicked PictureBox.
Use the techniques described for Lesson 9, Exercise 5 to use a single event handler for
all four PictureBoxes.
596906c10.indd 122 4/7/10 12:32:25 PM
Exercises
123
FIGURE 106
You can download the solutions to these exercises 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.
596906c10.indd 123 4/7/10 12:32:26 PM
Click here to Play
596906c10.indd 124 4/7/10 12:32:26 PM
..................Content has been hidden....................

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