- •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
ADD EVENT HANDLERS FOR THE VIEW MENU (continued)
|
Action |
Result |
|
|
|
9 |
Add Click event handlers |
private void menuLargeIcons_Click |
|
for each of the four menus |
(object sender, System.EventArgs e) |
|
in the View menu. |
{ |
|
listViewMain.View = View.LargeIcon; |
|
|
|
|
|
Note: These handlers set |
} |
|
the View property value in |
private void menuSmallIcons_Click( . . . ) |
|
the listViewMain control, |
|
|
{ |
|
|
which alters how the con- |
listViewMain.View = View.SmallIcon; |
|
tents of the control appear |
} |
|
to the user. |
private void menuList_Click( . . . ) |
|
|
|
|
|
{ |
|
|
listViewMain.View = View.List; |
|
|
} |
|
|
private void menuDetails_Click( . . . ) |
|
|
{ |
|
|
listViewMain.View = View.Details; |
|
|
} |
|
|
|
Your program will work just fine here. It doesn’t do very much, but it does work. Our final step for this section is to populate the list control with the available albums.
14.2.3Populating a ListView
Our final task here is to populate the ListView control. As we said earlier in the chapter, a ListView control contains a collection of ListViewItem objects. As indicated in .NET Table 14.3, the ListViewItem object inherits directly from the Sys-
tem.Object class. All of the painting and other management of list items are performed by the ListView class itself. This painting behavior is consistent with other container controls we have seen such as the StatusBar control containing StatusBarPanel objects, and the ListBox control containing a set of object instances.
Our use of the ListViewItem object here will be fairly modest. We will get more complicated later in the chapter. For now, we simply wish to create an item for each album with the file name as the label and an appropriate image icon assigned. This requires that we create an ImageList for both the small and large icons to display in the view, and populate the Items property for the list with a ListViewItem for each album.
448 |
CHAPTER 14 LIST VIEWS |
.NET Table 14.3 ListViewItem class
The ListViewItem class is an object that can be displayed within a ListView control. It is part of the System.Windows.Forms namespace, and supports the IClonable and ISerializable interfaces.
|
ListViewItem |
Initializes a new ListViewItem instance. |
|
|
Overloads |
Public |
|
ListViewItem(string label); |
Constructors |
|
ListViewItem(string[] labelAndSubitems); |
|
ListViewItem(string label, int imageIndex); |
|
|
|
|
|
|
ListViewItem(ListViewItem item, |
|
|
ListViewSubItem[] subitems, |
|
|
int imageIndex); |
|
|
|
|
Bounds |
Gets the bounding rectangle of the item, including any |
|
|
displayed subitems. |
|
Focused |
Gets or sets whether the item has the focus within the |
|
|
containing view. Defaults to false. |
|
Font |
Gets or sets the Font for the item. If null, the containing |
|
|
ListView uses its font for this purpose. |
|
ForeColor |
Gets or sets the foreground Color for the item. |
|
ImageIndex |
Gets or sets the index used to retrieve the icon for this item. |
Public |
Index |
Gets the index corresponding to the current position of the |
|
item within the containing ListView. |
|
Properties |
ListView |
Gets the ListView control that contains this item. |
|
||
|
Selected |
Gets or sets whether the item is currently selected in the |
|
|
containing view. |
|
StateImageIndex |
Gets or sets the index for the state icon for this item. |
|
SubItems |
Gets the collection of list view subitems assigned to this |
|
|
item. Note that this includes the item label as the first |
|
|
element in this collection. |
|
Tag |
Gets or sets the object associated with this item. |
|
Text |
Gets or sets the text string for this item. This is the item label. |
|
|
|
|
BeginEdit |
Initiates an edit of this item’s label. |
Public |
EnsureVisible |
Ensures a given item is visible, scrolling the containing |
|
view as necessary. |
|
Methods |
|
|
|
|
|
|
Remove |
Removes the item from the collection of ListViewItem |
|
|
objects in the containing view. |
|
|
|
THE LISTVIEW CLASS |
449 |
We will begin with the ImageList components.
CREATE THE IMAGE LISTS FOR THE VIEW
|
|
|
|
|
Action |
Result |
|||||
|
|
|
|
|
|
|
|
|
|
|
|
1 |
In the MainForm.cs [Design] window, add |
The objects appear in the component tray area |
|||||||||
|
two new ImageList objects to the form. |
below the form. |
|||||||||
|
|
|
|
|
Settings |
|
Note: The first list will contain the large |
||||
|
|
|
List |
|
Property |
Value |
icons for the View.LargeIcon display |
||||
|
|
|
List 1 |
|
(Name) |
imageListLarge |
mode, and the second the small icons for |
||||
|
|
|
|
all other modes. Since the icons provided |
|||||||
|
|
|
|
|
Size |
32. 32 |
|
|
|
||
|
|
|
|
|
|
|
|
in the common image area define both |
|||
|
|
|
List 2 |
|
(Name) |
imageListSmall |
image types, each list will use the same |
||||
|
|
|
|
|
Size |
16, 16 |
|
|
|
set of files. The Size property defines the |
|
|
|
|
|
|
|
|
|
|
|
|
actual image to use by each list. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
Define the images from the common |
The icons are stored in the image list and |
|||||||||
|
image area required for the |
available to the application. |
|||||||||
|
imageListLarge object. |
Note: The first icon will be used for Pho- |
|||||||||
|
How-to |
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
tograph objects later in this chapter. The |
|||
|
Use the Image Collection Editor, as |
next two are for a “good” and “bad” |
|||||||||
|
discussed in chapter 13. |
album, respectively. The final three will be |
|||||||||
|
|
|
|
|
Settings |
used in chapter 15 when discussing the |
|||||
|
|
|
|
|
TreeView class. |
||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Image |
|
|
|
File |
||||
|
|
|
|
|
|
|
|||||
|
0 |
|
icons/Misc/FACE01.ico |
|
|||||||
|
1 |
|
icons/Writing/BOOK01A.ico |
|
|||||||
|
2 |
|
icons/Misc/MISC02.ico |
|
|||||||
|
3 |
|
icons/Misc/FACE02.ico |
|
|||||||
|
4 |
|
icons/Writing/BOOK02.ico |
|
|||||||
|
5 |
|
icons/Writing/BOOKS04.ico |
|
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
3 |
Similarly, define the same set of images |
The same set of icons, albeit in different sizes, |
|||||||||
|
for the imageListSmall object. |
is now available from both image lists. |
|||||||||
|
Note: You might be tempted to create |
|
|||||||||
|
this image list by making a copy of the |
|
|||||||||
|
imageListLarge object and then apply- |
|
|||||||||
|
ing the settings from step 1. While this |
|
|||||||||
|
works, the small images are scaled from |
|
|||||||||
|
the larger size stored in the image- |
|
|||||||||
|
ListLarge object, resulting in poorer |
|
|||||||||
|
quality icons. |
|
|
|
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
4 |
Assign the two image lists to the |
Images from each list can now be displayed for |
|||||||||
|
corresponding property in the ListView |
items in the ListView control. |
|||||||||
|
control. |
|
|
|
|
|
|
|
|
||
|
|
|
|
|
Settings |
|
|||||
|
|
|
|
|
|
|
|||||
|
|
|
Property |
|
Value |
|
|||||
|
|
|
LargeImageList |
|
imageListLarge |
|
|||||
|
|
|
SmallImageList |
|
imageListSmall |
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
450 |
CHAPTER 14 LIST VIEWS |
The code generated by these changes is similar to examples we have seen before. The images are stored in a .resx file for the MainForm object, and loaded into the application using the ResourceManager class.
Now that we have the image lists defined, the form containing a ListView control, and the View menu primed and ready, we have nothing to do but add our photo albums to the list. We do this in the OnLoad method, which is called just before the Form displays the first time. We could instead add these items in the MainForm constructor, but the OnLoad method is preferred for such actions to ensure that the Form is fully initialized.
Let’s see how this code looks.
ADD EACH ALBUM TO THE VIEW
|
Action |
Result |
|
|
|
5 |
In the MainForm.cs source code |
using System.IO; |
|
window, indicate that this file will |
using Manning.MyPhotoAlbum; |
|
use members of the System.IO |
|
|
and the Manning.MyPhotoAlbum |
|
|
namespaces. |
|
|
|
|
6 |
Add a set of constant fields for the |
private const int PhotoIndex = 0; |
|
image list indices required. |
private const int AlbumIndex = 1; |
|
Note |
private const int ErrorIndex = 2; |
|
|
|
|
Using constants in this manner is a |
|
|
good idea in case our values ever |
|
|
change in the future. |
|
|
|
|
7 |
Modify the OnLoad method to load |
protected override void OnLoad(EventArgs e) |
|
the default set of albums through a |
{ |
|
private method. |
. . . |
|
LoadAlbumData(PhotoAlbum.DefaultDir); |
|
|
|
|
|
|
} |
|
|
|
8 |
Create a private OpenAlbum method |
private PhotoAlbum OpenAlbum(string fileName) |
|
to open an album. |
{ |
|
|
PhotoAlbum album = new PhotoAlbum(); |
|
|
try |
|
|
{ |
|
|
album.Open(fileName); |
|
|
} |
|
|
catch (Exception) |
|
|
{ |
|
|
return null; |
|
|
} |
|
|
return album; |
|
|
} |
|
|
|
THE LISTVIEW CLASS |
451 |
ADD EACH ALBUM TO THE VIEW (continued)
|
Action |
Result |
|
|
|
9 |
Implement the LoadAlbumData |
private void LoadAlbumData(string dir) |
|
method by iterating over the set of |
{ |
|
album files in the given album |
string[] albumFiles |
|
= Directory.GetFiles(dir, "*.abm"); |
|
|
directory. |
|
|
foreach (string s in albumFiles) |
|
|
Note: Accepting the directory from |
{ |
|
|
|
|
which to load the albums may |
|
|
come in useful if we ever want to |
|
|
support multiple directories. |
|
|
|
|
10 |
Try to open the album file. |
// See if we can open this album |
|
|
PhotoAlbum album = OpenAlbum(s); |
|
|
Note: Of course, if the album requires a pass- |
|
|
word, then the user must enter it here, which is |
|
|
not the best user interface. See the TRY IT! para- |
|
|
graph later in this section for a discussion on an |
|
|
alternative approach. |
|
|
|
11 |
Initialize a new ListViewItem |
// Create a new list view item |
|
based on whether the album was |
ListViewItem item = new ListViewItem(); |
|
opened successfully. |
item.Text |
|
|
|
|
|
= Path.GetFileNameWithoutExtension(s); |
|
|
if (album != null) |
|
|
item.ImageIndex = MainForm.AlbumIndex; |
|
|
else |
|
|
item.ImageIndex = MainForm.ErrorIndex; |
|
|
|
12 |
Add the new item to the ListView |
listViewMain.Items.Add(item); |
|
control. |
} |
|
|
} |
|
|
|
This creates a list item for each album found, using the base file name as the text for the album. If an album fails to open, then an error image is assigned as its icon. If any of your albums happen to have a nonempty password set, then the PhotoAlbum class will prompt you for this password before opening the album.
As you progress through this chapter, you will note that the album password is required repeatedly as the album is opened, which is not of course the nicest interface one could ask for. We could fix this by only requiring the password in the
Album class when the user wants to examine the photos or modify the album We will not actually do this, but we could.
Compile and run this program to see our new list view at work. Also alter the display setting using the items in the View menu. Assuming you have some album files in the album directory, you should find that the Large Icons, Small Icons, and List menus work just fine. Curiously, the Details setting displays nothing at all.
This is because the Details view requires a set of columns to be assigned to the form. So far we have not done this, so we will make it our next topic.
452 |
CHAPTER 14 LIST VIEWS |