- •brief contents
- •about this book
- •The Windows Forms namespace
- •Part 1: Hello Windows Forms
- •Part 2: Basic Windows Forms
- •Part 3: Advanced Windows Forms
- •Who should read this book?
- •Conventions
- •Action
- •Result
- •Source code downloads
- •Author online
- •acknowledgments
- •about .NET
- •Casting the .NET
- •Windows Forms overview
- •about the cover illustration
- •Hello Windows Forms
- •1.1 Programming in C#
- •1.1.1 Namespaces and classes
- •1.1.2 Constructors and methods
- •1.1.3 C# types
- •1.1.4 The entry point
- •1.1.5 The Application class
- •1.1.6 Program execution
- •1.2 Adding controls
- •1.2.1 Shortcuts and fully qualified names
- •1.2.2 Fields and properties
- •1.2.3 The Controls property
- •1.3 Loading files
- •1.3.1 Events
- •1.3.2 The OpenFileDialog class
- •1.3.3 Bitmap images
- •1.4 Resizing forms
- •1.4.1 Desktop layout properties
- •1.4.2 The Anchor property
- •1.4.3 The Dock property
- •1.5 Recap
- •2.1 Programming with Visual Studio .NET
- •2.1.1 Creating a project
- •Action
- •Result
- •2.1.2 Executing a program
- •Action
- •Result
- •2.1.3 Viewing the source code
- •View the code generated by Visual Studio .NET
- •Action
- •Result
- •2.2 Adding controls
- •2.2.1 The AssemblyInfo file
- •Action
- •Results
- •2.2.2 Renaming a form
- •Action
- •Result
- •2.2.3 The Toolbox window
- •Action
- •Result
- •2.3 Loading files
- •2.3.1 Event handlers in Visual Studio .NET
- •Action
- •Result
- •2.3.2 Exception handling
- •Action
- •Result
- •Action
- •Results and Comments
- •2.4 Resizing forms
- •2.4.1 Assign the Anchor property
- •Action
- •Result
- •2.4.2 Assign the MinimumSize property
- •Action
- •Result
- •2.5 Recap
- •Basic Windows Forms
- •Menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.3 Click events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.5 Context menus
- •Action
- •Result
- •Action
- •Result
- •3.6 Recap
- •Status bars
- •4.1 The Control class
- •4.2 The StatusBar class
- •Action
- •Result
- •Action
- •Result
- •4.3.1 Adding panels to a status bar
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •4.5 Recap
- •Reusable libraries
- •5.1 C# classes and interfaces
- •5.2 Class libraries
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.3 Interfaces revisited
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.4 Robustness issues
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Common file dialogs
- •Action
- •Results
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.3 Paint events
- •Action
- •Result
- •Action
- •Result
- •6.4 Context menus revisited
- •Action
- •Result
- •Action
- •Result
- •6.5 Files and paths
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.6 Save file dialogs
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.7 Open file dialogs
- •Action
- •Result
- •Action
- •Result
- •6.8 Recap
- •Drawing and scrolling
- •7.1 Form class hierarchy
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •7.4 Panels
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Dialog boxes
- •8.1 Message boxes
- •Action
- •Result
- •Action
- •Result
- •8.1.4 Creating A YesNoCancel dialog
- •Action
- •Result
- •Action
- •Result
- •8.2 The Form.Close method
- •8.2.1 The relationship between Close and Dispose
- •Action
- •Result
- •8.3 Modal dialog boxes
- •Action
- •Result
- •Action
- •Result
- •8.3.2 Preserving caption values
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Basic controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.1.2 Creating a derived form
- •Action
- •Result
- •9.2 Labels and text boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.3.6 Adding AlbumEditDlg to our main form
- •Action
- •Result
- •Action
- •Result
- •9.4 Recap
- •List controls
- •10.1 List boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.2 Multiselection list boxes
- •10.2.1 Enabling multiple selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.3 Combo boxes
- •Action
- •Result
- •Action
- •Result
- •10.4 Combo box edits
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.5 Owner-drawn lists
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •More controls
- •Action
- •Result
- •Action
- •Result
- •11.2 Tab pages
- •Action
- •Result
- •Action
- •Result
- •11.3.1 Dates and times
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •11.5 Recap
- •A .NET assortment
- •12.1 Keyboard events
- •Action
- •Result
- •Action
- •Result
- •12.2 Mouse events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.3 Image buttons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.4 Icons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.5 Recap
- •Toolbars and tips
- •13.1 Toolbars
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •13.4.2 Creating tool tips
- •Action
- •Result
- •Action
- •Result
- •Advanced Windows Forms
- •List views
- •14.2 The ListView class
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.2.3 Populating a ListView
- •Action
- •Result
- •Action
- •14.3 ListView columns
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.6 Recap
- •Tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.3 Dynamic tree nodes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.4 Node selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.5 Fun with tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Multiple document interfaces
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.3 Merged menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.4 MDI children
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.5 MDI child window management
- •Action
- •Result
- •Action
- •Result
- •16.6 Recap
- •Data binding
- •17.1 Data grids
- •Action
- •Result
- •Action
- •Result
- •17.2 Data grid customization
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Odds and ends .NET
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.2 Timers
- •Action
- •Result
- •Action
- •Result
- •18.3 Drag and drop
- •Action
- •Result
- •Action
- •Result
- •18.4 ActiveX controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.5 Recap
- •C# primer
- •A.1 C# programs
- •A.1.1 Assemblies
- •A.1.2 Namespaces
- •A.2 Types
- •A.2.1 Classes
- •A.2.2 Structures
- •A.2.3 Interfaces
- •A.2.4 Enumerations
- •A.2.5 Delegates
- •A.3 Language elements
- •A.3.1 Built-in types
- •A.3.2 Operators
- •A.3.3 Keywords
- •A.4 Special features
- •A.4.1 Exceptions
- •A.4.2 Arrays
- •A.4.3 Main
- •A.4.4 Boxing
- •A.4.5 Documentation
- •.NET namespaces
- •B.1 System.Collections
- •B.2 System.ComponentModel
- •B.3 System.Data
- •B.4 System.Drawing
- •B.5 System.Globalization
- •B.6 System.IO
- •B.7 System.Net
- •B.8 System.Reflection
- •B.9 System.Resources
- •B.10 System.Security
- •B.11 System.Threading
- •B.12 System.Web
- •B.13 System.Windows.Forms
- •B.14 System.XML
- •Visual index
- •C.1 Objects
- •C.2 Marshal by reference objects
- •C.3 Components
- •C.4 Common dialogs
- •C.7 Event data
- •C.8 Enumerations
- •For more information
- •bibliography
- •Symbols
- •Index
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 |