Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Manning - Windows Forms Programming With CSharp.pdf
Скачиваний:
72
Добавлен:
24.05.2014
Размер:
14.98 Mб
Скачать

char c = e.KeyChar;

// Ignore all non-alphanumerics – not our approach e.Handled = !(Char.IsLetter(c) || Char.IsDigit(c));

}

This implementation uses members of the System.Char class to see if the category of the character is a letter or number. It may look good, but it also causes all other characters to be ignored by the control, such as spaces and backspaces. Clearly, this is not what we want.

Instead, we will allow all control and white space characters past our event handler. This will permit the keyboard shortcuts to work, and also allows spaces in our captions.

ADD KEYPRESS EVENT HANDLER FOR TXTCAPTION CONTROL

 

Action

Result

 

 

 

1

In the PhotoEditDlg.cs Design

private void txtCaption_KeyPress

 

window, add a KeyPress event

(object sender, KeyPressEventArgs e)

 

for the txtCaption text box

{

 

 

 

control.

 

 

 

 

2

Implement this handler to only

char c = e.KeyChar;

 

permit letters and numbers to

e.Handled = !(Char.IsLetterOrDigit(c)

 

appear in captions.

 

|| Char.IsWhiteSpace(c)

 

 

 

 

|| Char.IsControl(c));

 

 

}

 

 

 

The caption text box will only receive letters, digits, white space, and all control characters. This may or may not be a good idea, by the way, since a caption such as “oneway street” is now not permitted, since the dash ‘-’ is a punctuation character. Feel free to remove this handler if you do not want this behavior in your program.

Another feature we could add to our dialog is to display the caption for the photograph in the title bar. Of course, this caption can be edited, and we would not want the text box and the title bar to display different values.

The TextChanged event occurs as text is entered, and can be used here to update the title bar while the user is typing. We could also implement this feature using the KeyPress event we just saw, but would have to deal with the delete and backspace keys as well as some text-editing controls. The TextChanged approach is a bit more straightforward.

Let’s continue our previous steps and make this change.

LABELS AND TEXT BOXES

289

UPDATE TITLE BAR DURING TXTCAPTION MODIFICATION

 

Action

Result

 

 

 

3

Add a TextChanged event

private void txtCaption_TextChanged

 

for the txtCaption text

(object sender, System.EventArgs e)

 

box control.

{

 

 

 

How-to

 

 

This is the default event for

 

 

text boxes, so you can just

 

 

double-click the control.

 

 

 

 

4

Modify the title bar to

this.Text = String.Format(

 

include the modified text

"{0} - Photo Properties",

 

from the control.

txtCaption.Text);

 

}

 

 

 

 

 

Compile and run your application to view these new changes. Verify that the caption can contain only letters and numbers, and that the title updates automatically as the caption is modified.

TRY IT! As an exercise in using some of the methods available to TextBox controls, see if you can create the standard context menu for text boxes manually and assign it to the Notes control. You will need to add a ContextMenu object to the form and assign it to the txtNotes.ContextMenu property. Assigning this property automatically disables the default context menu. Add the eight menu items to the menu, namely Undo, a separator, Copy, Cut, Paste, Delete, another separator, and Select All. To make your menu different than the standard one, also add a Clear menu item at the end of the context menu to clear the text in the box.

To process this menu, you will need a Popup event handler for the menu itself to enable or disable the menu items as appropriate. You will need to use the CanUndo, SelectedText, SelectionLength, and SelectionStart properties, and the Copy, Cut, Paste, SelectAll, and

Undo methods as part of your implementation.

If you run into difficulties, visit the book’s web site and download the code required for this change.

This ends our discussion of Label and TextBox objects for now. We will see these objects again in the next section and elsewhere in the book. Our next topic will create the Album Properties dialog box as a way to introduce the button classes in the .NET Framework.

9.3BUTTON CLASSES

So just what is a button, exactly? For graphical interfaces, a button is a control that establishes a specific state, typically some form of on or off. Buttons are used to perform immediate actions in an interface, define the behavior for a specific feature, or

290

CHAPTER 9 BASIC CONTROLS

turn a setting on or off. Figure 9.4 shows various styles of buttons in Windows Forms. More generally, the various types of buttons are as follows.

A push button—is a button that performs some immediate action, such as displaying or deactivating a dialog, or modifying the values in the window. In Windows Forms, the Button class represents a push button.

A check box button—allows a user to turn a specific option on or off, such as whether a file should be saved as read-only or not. In .NET, the CheckBox class can represent either a check box button or a toggle button. A toggle button appears as a normal button, but preserves an up or down state to represent a checked or unchecked mode, respectively.

A radio button—sometimes called an option button, is used to select from a set of mutually exclusive options. When one of a group of radio buttons is selected, the other radio buttons in the group are automatically deselected. Radio buttons can be displayed normally or as toggle buttons. Windows Forms provides the RadioButton class for the creation of these objects. All radio buttons in the same container are automatically part of the same group. Use container classes such as GroupBox and Panel to support multiple groups of radio buttons on your forms.

Figure 9.4

The three types of buttons in various styles. Note how both check boxes and radio buttons can appear as toggle buttons.

In figure 9.4, note how each button supports a normal three-dimensional style as well as a flat style. In addition, note that toggle buttons appear identical to regular push buttons. Unlike push buttons, a toggle button preserves an in or out state when they are pressed.

All buttons in .NET inherit from the ButtonBase class. This class provides common functionality for all buttons, including the flat style setting and whether to display an image on the button. An overview of this class appears in .NET Table 9.4.

BUTTON CLASSES

291

.NET Table 9.4 ButtonBase class

The ButtonBase class represents a control that can be displayed as a button. It is an abstract class in the System.Windows.Forms namespace, and inherits from the Control class. The Button, CheckBox, and RadioButton classes all inherit from this class. See .NET Table 4.1 on page 104 for a list of members inherited from the Control class.

 

FlatStyle

Gets or sets the flat style appearance of the button.

 

Image

Gets or sets an image to display on the button.

 

ImageAlign

Gets or sets the alignment of an image on the button.

Public

ImageIndex

Gets or sets an image to display on the button as an

Properties

 

index into the ImageList property.

 

 

 

ImageList

Gets or sets an ImageList object to associate with

 

 

the button control.

 

TextAlign

Gets or sets the alignment of text on the button.

 

 

 

In the MyPhotos application, we have already used a number of push buttons in our application, and we’ve seen how the DialogResult property can be used to automatically exit a modal dialog when a button is clicked. An overview of the Button class appears in .NET Table 9.5. In this section we build a dialog window for editing album properties to permit modification of internal album settings by the user. Our hidden agenda, of course, is to demonstrate the various types of buttons.

.NET Table 9.5 Button class

The Button class represents a standard push button. A button may display text, an image, or both text and an image. This class is part of the System.Windows.Forms namespace, and inherits from the ButtonBase class. See .NET Table 9.4 for details on this base class.

Public Properties

DialogResult

Gets or sets a value that is returned to the parent

 

form when the button is clicked.

 

 

 

 

 

Public Methods

PerformClick

Generates a Click event for the button.

 

 

 

We will illustrate various styles of buttons in a dialog box for editing the properties of a photo album. To do this, we need to start with some reasonable properties for our PhotoAlbum class that will lend themselves to button objects. The following features will serve our purposes rather well.

Title—a title or name for the album. As you may guess, this will be a TextBox control.

Photo display name—which Photograph setting should be used as the short display name for the photo. This will be either the base file name, the caption, or the date assigned to the photo. This property has three possible values, making it perfect as a RadioButton example.

292

CHAPTER 9 BASIC CONTROLS