- •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
Compile and run the application to see the Remove button and the rest of the interface in action. Note that you can remove photographs and move them around and still decide not to save these changes when the album is closed.
If you look at our application so far, there is still some space available in the Albums group box. This space is intended for a ComboBox control holding the list of available albums. Now that we have seen different ways to use the ListBox control, it’s time to take a look at the other .NET list control: the ComboBox class.
10.3COMBO BOXES
A list box is quite useful for presenting a list of strings, such as the photographs in an album. There are times when only one item will ever be selected, or when the extra space necessary to display a list box is problematic or unnecessary. The ComboBox class is a type of ListControl object that displays a single item in a text box and permits selection from an associated list box. Since a user can enter new values into the text box control directly, a ComboBox allows additional items to be added much more simply than a ListBox control.
Features specific to the ComboBox class are shown in .NET Table 10.4. As you can see, a number of members are reminiscent of members from both the ListBox class and the TextBox class. The TextBox area of the control is sometimes called the editable portion of the control, even though it is not always editable, and the ListBox portion may be called the dropdown portion, since the list drops down below the text box portion for some display styles.
10.3.1CREATING A COMBO BOX
In our MyAlbumEditor application, we will add a ComboBox control to permit quick and easy access to the list of albums stored in the default album directory. The entries for this control will be taken from the album file names discovered in this directory, and the user will not be able to add new entries by hand. Figure 10.4 shows how our application will look after this change, with the ComboBox dropdown list displayed.
Figure 10.4
The dropdown list for a ComboBox is hidden until the user clicks on the small down arrow to reduce the amount of space required for the control on the
COMBO BOXES |
333 |
.NET Table 10.4 ComboBox class
The ComboBox class is a ListControl object that combines a TextBox control with a ListBox object. A user can select an item from the list or enter an item manually. A ComboBox can be displayed with or without the list box portion shown and with or without the text box portion editable, depending on the setting of the DropDownStyle property. When the list box portion is hidden, a down arrow is provided to display the list of available items. This class is part of the System.Windows.Forms namespace, and inherits from the ListControl class. See .NET Table 10.1 on page 316 for a list of members inherited by this class.
|
DrawMode |
Gets or sets how elements in the list are drawn in a |
|
|
window. |
|
DropDownStyle |
Gets or sets the style used to display the edit and list |
|
|
box controls in the combo box. |
|
DropDownWidth |
Gets or sets the width of the list box portion of the |
|
|
control. |
|
DroppedDown |
Gets or sets whether the combo box is currently |
|
|
displaying its list box portion. |
|
Items |
Gets or sets the collection of items contained by this |
Public |
|
combo box. |
|
|
|
Properties |
MaxDropDownItems |
Gets or sets the maximum number of items |
|
||
|
|
permitted in the list box portion of the control. |
|
MaxLength |
Gets or sets the maximum number of characters |
|
|
permitted in the text box portion of the control. |
|
SelectedItem |
Gets or sets the currently selected item in the |
|
|
control. |
|
SelectedText |
Gets or sets any text that is selected in the text box |
|
|
portion of the control. |
|
Sorted |
Gets or sets whether the items in the control are |
|
|
sorted alphabetically. |
|
|
|
Public |
BeginUpdate |
Prevents the control from painting its contents while |
|
items are added to the list box. |
|
Methods |
SelectAll |
Selects all text in the text box portion of the control. |
|
||
|
|
|
|
DrawItem |
Occurs when an owner-drawn combo box requires |
|
|
repainting. |
Public |
DropDown |
Occurs just before the dropdown portion of a combo |
Events |
|
box is displayed. |
|
SelectionChange- |
Occurs when the selected item in the control has |
|
Committed |
changed and that change is confirmed. |
|
|
|
The steps required to create the combo box for our application are as follows:
334 |
CHAPTER 10 LIST CONTROLS |
Set the version number of the MyAlbumEditor application to 10.3.
REPLACE OPEN BUTTON WITH A COMBOBOX CONTROL
|
|
Action |
Result |
||
|
|
|
|
|
|
1 |
Delete the Open button in the |
The button and all related code added by Visual Studio |
|||
|
MainForm.cs [Design] window. |
are removed from the MainForm.cs source file. Any |
|||
|
|
|
|
|
nonempty event handlers, in this case btnOpen_Click, |
|
|
|
|
|
remain in the file and must be removed manually. |
|
|
|
|
|
|
2 |
Drag a ComboBox control into |
|
|||
|
the left side of the Albums group |
|
|||
|
box as shown in the graphic. |
|
|||
|
|
Settings |
|
||
|
|
|
|
|
|
|
|
Property |
Value |
|
|
|
|
(Name) |
cmbxAlbums |
|
|
|
|
Anchor |
Top, Left, Right |
|
|
|
|
DropDownStyle |
DropDownList |
|
|
|
|
Sorted |
True |
|
|
|
|
|
|
|
|
3 |
Replace the btnOpen_Click |
private void OpenAlbum(string fileName) |
|||
|
method in the MainForm.cs |
{ |
|||
|
source file with an OpenAlbum |
CloseAlbum(); |
|||
|
|
||||
|
method to open a given album |
// Open the given album file |
|||
|
file. |
|
|
_album.Open(fileName); |
|
|
Note: Most of the existing code |
this.Text = _album.FileName; |
|||
|
|
||||
|
for the btnOpen_Click method |
UpdateList(); |
|||
|
is removed. Any exception that |
} |
|||
|
occurs here will be the respon- |
|
|||
|
sibility of the caller. |
|
|||
|
|
|
|
|
|
4 |
Set the Enabled property for |
Note: We will enable this button when a valid |
|||
|
the Properties button in the |
album is selected in the combo box control. |
|||
|
Albums group box to false. |
|
|||
|
|
|
|
|
|
5 |
Initialize the contents of the |
protected override void |
|||
|
combo box in the OnLoad |
OnLoad(EventArgs e) |
|||
|
method. |
|
|
{ |
|
|
|
|
// Initialize the album |
||
|
|
|
|
|
|
|
How-to |
|
|
_album = new PhotoAlbum(); |
|
|
Use the static GetFiles |
// Initialize the combo box |
|||
|
method from the Directory |
||||
|
cmbxAlbums.DataSource |
||||
|
class to retrieve the set of album |
= Directory.GetFiles( |
|||
|
files in the default album |
PhotoAlbum.DefaultDir, "*.abm"); |
|||
|
directory. |
|
|
base.OnLoad(e); |
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
6 |
At the top of the file, indicate |
. . . |
|||
|
that we are using objects in the |
using System.IO; |
|||
|
System.IO namespace. |
|
|||
|
|
|
|
|
|
COMBO BOXES |
335 |
As we saw for our ListBox control, the DataSource property provides a quick and easy way to assign a collection of objects to the cmbxAlbums control. In this case, the Directory.GetFiles method returns an array of strings containing the set of file names in the given directory that match the given search string.
Our ComboBox is created with the DropDownStyle property set to DropDownList. This setting is taken from the ComboBoxStyle enumeration, and indicates that the list box associated with the combo box should not be displayed by default, and that the user cannot manually enter new values into the control. A complete list of values provided by the ComboBoxStyle enumeration is shown in .NET Table 10.5.
.NET Table 10.5 ComboBoxStyle enumeration
The ComboBoxStyle enumeration specifies the display behavior of a combo box control. This enumeration is part of the System.Windows.Forms namespace.
|
DropDown |
The text portion of the control is editable. The list |
|
|
portion is only displayed when the user clicks an |
|
|
arrow button on the control. This is the default. |
Enumeration |
DropDownList |
The text portion of the control is not editable. |
Values |
|
The list portion is only displayed when the user |
|
|
clicks an arrow button on the control. |
|
Simple |
The text portion of the control is editable, and |
|
|
the list portion of the control is always visible. |
|
|
|
Feel free to compile and run your program if you like. The combo box will display the available albums, without the ability to actually open an album. Opening an album requires that we handle the SelectedItemChanged event for our combo box, which is the topic of the next section.
10.3.2HANDLING THE SELECTED ITEM
Our ComboBox currently displays a selected album, but it doesn’t actually open it. The previous section replaced the Click handler for the now-deleted Open button with an OpenAlbum method, so all we need to do here is recognize when a new album is selected and open the corresponding album.
The one issue we must deal with is the case where an invalid album exists. While we initialized our control to contain only album files ending with “.abm,” it is still possible that one of these album files contains an invalid version number or other problem that prevents the album from loading. The following steps handle this case by disabling the Properties button and ListBox control when such a problem occurs. An appropriate error message is also displayed in the title bar.
336 |
CHAPTER 10 LIST CONTROLS |