- •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
Components |
The Component class represents an object that is marshaled by reference and can exist within a container. This figure shows the complete set of Windows Forms classes derived from the System.ComponentModel.Component class.
Common dialogs
The CommonDialog class represents a component that provides a standard interface for common functionality required by Windows Forms applications. This figure shows the complete set of Windows Forms classes derived from the System.Windows.Forms.CommonDialog class.
Windows Forms Programming with C#
ERIK BROWN
M A N N I N G
Greenwich (74° w. long.)
For online information and ordering of this and other Manning books, go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact:
Special Sales Department |
|
Manning Publications Co. |
|
209 Bruce Park Avenue |
Fax: (203) 661-9018 |
Greenwich, CT 06830 |
email: orders@manning.com |
©2002 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Manning Publications Co. |
Copyeditor: |
Lois Patterson |
209 Bruce Park Avenue |
Typesetter: |
Syd Brown |
Greenwich, CT 06830 |
Cover designer: |
Leslie Haimes |
ISBN 1930110-28-6
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – VHG – 06 05 04 03 02
In memory of Thelma Rose Wilson, and for her beautiful daughter whom I love
brief contents
Part 1 Hello Windows Forms 1
1 |
Getting started with Windows Forms 3 |
2 |
Getting started with Visual Studio .NET 34 |
Part 2 Basic Windows Forms 67
3 |
Menus 69 |
|
|
|
4 |
Status bars |
102 |
|
|
5 |
Reusable libraries |
126 |
||
6 |
Common file dialogs |
161 |
||
7 |
Drawing and scrolling |
194 |
||
8 |
Dialog boxes |
224 |
|
|
9 |
Basic controls |
263 |
|
|
10 |
List controls |
314 |
|
|
11 |
More controls |
353 |
|
|
12 |
A .NET assortment |
383 |
||
13 |
Toolbars and tips |
410 |
Part 3 Advanced Windows Forms 437
14List views 439
15Tree views 485
16 |
Multiple document interfaces 525 |
17 |
Data binding 564 |
18 |
Odds and ends .NET 603 |
vii
contents
brief contents |
vii |
||
contents |
ix |
|
|
preface |
xix |
|
|
about this book |
xxi |
||
acknowledgments |
xxviii |
||
about .NET |
xxx |
about the cover illustration xxxiv
Part 1 Hello Windows Forms 1
1 Getting started with Windows Forms |
3 |
|
||||||
1.1 |
Programming in C# |
4 |
|
|
|
|
||
|
Namespaces and classes |
6 Constructors and |
|
|||||
|
methods |
8 C# types |
9 The entry point |
11 |
||||
|
The Application class |
11 |
Program execution |
13 |
||||
1.2 |
Adding controls 13 |
|
|
|
|
|
||
|
Shortcuts and fully qualified names |
15 |
|
|
||||
|
Fields and properties 16 |
The Controls property 18 |
||||||
1.3 |
Loading files |
18 |
|
|
|
|
|
|
|
Events |
20 The OpenFileDialog class |
22 |
|
||||
|
Bitmap images |
24 |
|
|
|
|
|
|
1.4 |
Resizing forms |
26 |
|
|
|
|
|
|
|
Desktop layout properties |
28 The Anchor |
|
|||||
|
property |
29 The Dock property |
31 |
|
|
|||
1.5 |
Recap |
33 |
|
|
|
|
|
|
ix
2 Getting started with Visual Studio .NET |
34 |
||||||
2.1 |
Programming with Visual Studio .NET |
35 |
|
||||
|
Creating a project |
36 |
Executing a program |
39 |
|||
|
Viewing the source code |
39 |
|
|
|||
2.2 |
Adding controls |
43 |
|
|
|
|
|
|
The AssemblyInfo file |
43 |
Renaming a form |
46 |
|||
|
The Toolbox window |
48 |
|
|
|
||
2.3 |
Loading files |
54 |
|
|
|
|
|
|
Event handlers in Visual Studio .NET 54 |
|
|
||||
|
Exception handling |
58 |
|
|
|
||
2.4 |
Resizing forms |
61 |
|
|
|
|
|
|
Assign the Anchor property |
63 |
|
|
|||
|
Assign the MinimumSize property 64 |
|
|
||||
2.5 |
Recap 65 |
|
|
|
|
|
|
Part 2 Basic Windows Forms 67
3 Menus 69
3.1 |
The nature of menus |
70 |
|
|
|
|
||
|
The Menu class |
71 The Menu class hierarchy 71 |
|
|
||||
|
Derived classes |
73 |
|
|
|
|
|
|
3.2 |
Menu bars |
74 |
|
|
|
|
|
|
|
Adding the Main menu |
74 Adding the File menu |
77 |
|
||||
|
Adding the dropdown menu |
79 Adding a View menu |
83 |
|||||
3.3 |
Click events |
85 |
|
|
|
|
|
|
|
Adding handlers via the designer window |
85 |
|
|
||||
|
Adding handlers via the properties window |
86 |
|
|
||||
3.4 |
Popup events and shared handlers 88 |
|
|
|
||||
|
Defining a shared handler 89 |
Handling Popup events |
93 |
|||||
3.5 |
Context menus |
97 |
|
|
|
|
|
|
|
Creating a context menu |
98 Adding menu items |
100 |
|
||||
3.6 |
Recap 101 |
|
|
|
|
|
|
|
4 Status bars 102
4.1 |
The Control class |
103 |
4.2 |
The StatusBar class |
105 |
|
Adding a status bar |
106 Assigning status bar text 109 |
4.3 |
Status bar panels |
110 |
Adding panels to a status bar 111 Assigning panel text 116
x |
CONTENTS |
4.4 |
Owner-drawn panels |
118 |
|
The DrawItem event |
118 Drawing a panel 121 |
4.5 |
Recap 125 |
|
5 Reusable libraries |
126 |
|
|
|
|
|
||||
5.1 |
C# classes and interfaces |
127 |
|
|
|
|||||
|
Interfaces |
128 |
Data collection classes |
129 |
|
|||||
5.2 |
Class libraries |
133 |
|
|
|
|
|
|
||
|
Creating the class library 134 |
Using the command-line |
||||||||
|
tools |
138 |
Creating the PhotoAlbum class |
139 |
||||||
|
Creating the Photograph class |
141 |
|
|
|
|||||
5.3 |
Interfaces revisited |
145 |
|
|
|
|
|
|||
|
Supporting the ICollection interface |
146 |
|
|
||||||
|
Supporting the IList interface |
|
146 |
|
|
|
||||
|
Implementing album position operations |
148 |
|
|||||||
5.4 |
Robustness issues 151 |
|
|
|
|
|
||||
|
Handling an invalid bitmap |
151 Overriding methods in |
||||||||
|
the Object class |
154 |
Disposing of resources |
157 |
||||||
|
Associating a file name with an album |
159 |
|
|||||||
5.5 |
Recap |
160 |
|
|
|
|
|
|
|
6 Common file dialogs |
161 |
|
|
|
|||
6.1 |
Design issues |
162 |
|
|
|
|
|
|
Changing the menu bar 162 Adding class variables |
165 |
|||||
6.2 |
Multiple file selection |
166 |
|
|
|
||
|
Adding images to an album 166 |
|
|
||||
|
Removing images from an album |
169 |
|
||||
6.3 |
Paint events 169 |
|
|
|
|
||
|
Drawing the current photograph |
170 |
|
||||
|
Displaying the current position |
171 |
|
||||
6.4 |
Context menus revisited |
173 |
|
|
|
||
|
Displaying the next photograph |
174 |
|
||||
|
Displaying the previous photograph |
174 |
|
||||
6.5 |
Files and paths |
175 |
|
|
|
|
|
|
Creating a default album directory |
175 |
|
||||
|
Setting the title bar |
179 |
Handling the New menu |
180 |
|||
6.6 |
Save file dialogs |
|
181 |
|
|
|
|
|
Writing album data |
182 |
Saving an album as a new file 186 |
||||
|
Saving an existing album |
188 |
|
|
|
||
6.7 |
Open file dialogs |
189 |
|
|
|
|
|
|
Reading album data |
190 |
Opening an album file 191 |
||||
6.8 |
Recap 193 |
|
|
|
|
|
|
CONTENTS |
xi |
7 Drawing and scrolling |
194 |
|
|
|
|
||||
7.1 |
Form class hierarchy |
195 |
|
|
|
|
|||
|
The ScrollableControl class |
196 |
The Form class |
196 |
|||||
7.2 |
Image drawing |
198 |
|
|
|
|
|||
|
Deleting the PictureBox control |
198 Handling the |
|||||||
|
Image menu |
199 Implementing the Stretch to |
|
||||||
|
Fit option |
202 |
Implementing a Scale to Fit option |
205 |
|||||
|
Repainting when the form is resized |
210 |
|
||||||
7.3 |
Automated scrolling |
212 |
|
|
|
|
|||
|
Properties for scrolling |
213 |
|
|
|
|
|||
|
Implementing automated scrolling |
213 |
|
||||||
7.4 |
Panels |
215 |
|
|
|
|
|
|
|
|
Adding a panel |
217 |
Updating the menu handlers |
218 |
|||||
|
Drawing the status bar panel |
219 |
Drawing the image 220 |
||||||
7.5 |
Recap |
222 |
|
|
|
|
|
|
8 Dialog boxes |
224 |
|
|
|
|
|
|
|
8.1 |
Message boxes |
225 |
|
|
|
|
|
|
|
The MessageBox.Show method |
227 Creating an |
||||||
|
OK dialog 227 Creating a YesNo dialog |
229 |
||||||
|
Creating A YesNoCancel dialog |
230 |
|
|
||||
8.2 |
The Form.Close method |
233 |
|
|
||||
|
The relationship between Close and Dispose |
233 |
||||||
|
Intercepting the Form.Close method |
235 |
|
|||||
8.3 |
Modal dialog boxes |
237 |
|
|
|
|
||
|
Adding captions to photos |
238 |
Preserving caption values 239 |
|||||
|
Creating the CaptionDlg form |
240 Adding properties to the |
||||||
|
CaptionDlg form |
247 |
Displaying the dialog in the |
|||||
|
MainForm class |
249 |
|
|
|
|
|
|
8.4 |
Modeless dialogs |
252 |
|
|
|
|
||
|
Creating the PixelDlg form |
253 Adding class members to |
||||||
|
PixelDlg |
255 Displaying the modeless PixelDlg form 256 |
||||||
|
Updating the PixelDlg form |
259 |
|
|
||||
|
Updating PixelDlg as the mouse moves |
260 |
|
|||||
8.5 |
Recap |
262 |
|
|
|
|
|
|
9 Basic controls 263
9.1 |
Form inheritance 264 |
|
|
Creating a base form |
265 Creating a derived form 269 |
9.2 |
Labels and text boxes |
271 |
Expanding the Photograph class 272
xii |
CONTENTS |
Creating the PhotoEditDlg panel area |
277 |
Creating the multiline text box 281 |
Adding PhotoEditDlg |
to our main form 285 Using TextBox controls 287 |
9.3 |
Button classes 290 |
|
|
|
Expanding the PhotoAlbum class 293 Using the new album |
||
|
settings |
296 Creating the AlbumEditDlg panel area |
298 |
|
Using radio buttons 300 Using check box buttons |
304 |
|
|
Adding AlbumEditDlg to our main form 310 |
|
|
9.4 |
Recap |
313 |
|
10 List controls |
314 |
|
|
|
|
|
|
|
10.1 |
List boxes |
315 |
|
|
|
|
|
|
|
Creating a list box |
315 |
Handling selected items 322 |
|
||||
10.2 |
Multiselection list boxes |
325 |
|
|
||||
|
Enabling multiple selection |
325 Handling the Move Up and |
||||||
|
Move Down buttons |
328 Handling the Remove button |
331 |
|||||
10.3 |
Combo boxes 333 |
|
|
|
|
|
||
|
Creating a combo box |
|
333 |
Handling the selected item |
336 |
|||
10.4 |
Combo box edits |
339 |
|
|
|
|||
|
Replacing the photographer control |
340 |
|
|||||
|
Updating the combo box dynamically |
341 |
|
|||||
10.5 |
Owner-drawn lists |
|
343 |
|
|
|
||
|
Adding a context menu |
344 Setting the item height 346 |
||||||
|
Drawing the list items |
|
348 |
|
|
|
||
10.6 |
Recap 352 |
|
|
|
|
|
|
11 More controls |
353 |
|
|
||
11.1 |
Tab controls 354 |
|
|
||
|
The TabControl class |
355 Creating a tab control 356 |
|||
11.2 |
Tab pages |
359 |
|
|
|
|
Creating tab pages dynamically 360 |
||||
|
Creating tab pages in Visual Studio |
363 |
|||
11.3 |
Dates and Times |
366 |
|
||
|
Dates and times |
367 |
|
|
|
|
Customizing a DateTimePicker control 369 |
||||
11.4 |
Calendars |
372 |
|
|
|
|
Adding a MonthCalendar control |
372 |
|||
|
Initializing a calendar |
374 |
|
||
|
Handling mouse clicks in a calendar control 376 |
||||
11.5 |
Recap 381 |
|
|
|
CONTENTS |
xiii |
12 A .NET assortment |
383 |
|
||
12.1 |
Keyboard events |
384 |
|
|
|
Handling the KeyPress event |
384 |
||
|
Handling other keyboard events 386 |
|||
12.2 |
Mouse events 387 |
|
||
|
The MouseEventArgs class 388 Handling mouse events 388 |
|||
12.3 |
Image buttons |
393 |
|
|
|
Implementing Next and Prev buttons 393 Drawing bitmaps for |
|||
|
our buttons 399 |
Placing images on our buttons 402 |
||
12.4 |
Icons |
405 |
|
|
|
Replacing the icon on a form |
406 |
||
|
Replacing the application icon |
408 |
||
12.5 |
Recap |
409 |
|
|
13 Toolbars and tips 410
13.1 |
Toolbars |
411 |
|
|
|
|
The ToolBar class |
411 |
Adding a toolbar |
412 |
|
|
The ToolBarButton class |
413 |
|
||
13.2 |
Image lists |
416 |
|
|
|
|
The ImageList class |
416 Creating an image list 417 |
|||
13.3 |
Toolbar buttons |
420 |
|
|
|
|
Adding a push button 420 Adding a dropdown button 424 |
||||
|
Adding a toggle button |
426 |
|
||
13.4 |
Tool tips |
430 |
|
|
|
|
The ToolTip class |
431 |
Creating tool tips |
431 |
|
13.5 |
Recap 434 |
|
|
|
Part 3 Advanced Windows Forms 437
14 List views 439
14.1 |
The nature of list views |
440 |
|
|
14.2 |
The ListView class |
443 |
|
|
|
Creating the MyAlbumExplorer project 443 |
|
||
|
Creating a list view |
445 |
Populating a ListView 448 |
|
14.3 |
ListView columns |
453 |
|
|
|
Creating the columns |
454 Populating the columns |
456 |
|
|
Sorting a column 458 |
|
|
|
14.4 |
Selection and editing |
464 |
|
|
|
Supporting item selection |
464 Supporting label edits |
468 |
xiv |
CONTENTS |
|
14.5 |
Item activation |
472 |
|
|
|
|
|
|
|
|||
|
|
Handling item activation |
473 |
Defining new columns |
474 |
||||||||
|
|
Populating the ListView 476 |
Sorting a column (again) |
477 |
|||||||||
|
|
Updating the properties menu |
480 |
Updating label |
|
||||||||
|
|
editing |
481 |
Redisplaying the albums 483 |
|
|
|||||||
|
14.6 |
Recap |
483 |
|
|
|
|
|
|
|
|
|
|
15 |
Tree views |
485 |
|
|
|
|
|
|
|
|
|
||
|
15.1 |
Tree view basics |
486 |
|
|
|
|
|
|
|
|||
|
15.2 |
The TreeView class |
486 |
|
|
|
|
|
|
||||
|
|
Creating a tree view |
488 |
Using the Splitter class |
489 |
|
|||||||
|
|
Using the TreeNode class |
492 |
|
|
|
|
||||||
|
15.3 |
Dynamic tree nodes |
497 |
|
|
|
|
|
|
||||
|
|
Assigning index constants |
497 |
Creating the album nodes 498 |
|||||||||
|
|
Creating the photograph nodes |
|
501 |
|
|
|
||||||
|
15.4 |
Node selection |
505 |
|
|
|
|
|
|
|
|||
|
|
Supporting node selection |
506 |
Revisiting the list view |
509 |
||||||||
|
15.5 |
Fun with tree views |
513 |
|
|
|
|
|
|
||||
|
|
Displaying the photograph |
|
514 |
Supporting label edits |
516 |
|||||||
|
|
Updating the properties menu |
520 |
|
|
|
|||||||
|
15.6 |
Recap |
524 |
|
|
|
|
|
|
|
|
|
|
16 |
Multiple document interfaces |
525 |
|
|
|
||||||||
|
16.1 |
Interface styles |
526 |
|
|
|
|
|
|
|
|||
|
|
Single document interfaces |
|
526 |
Explorer interfaces 526 |
||||||||
|
|
Multiple document interfaces |
527 |
|
|
|
|||||||
|
|
Support in Windows Forms |
529 |
|
|
|
|||||||
|
16.2 |
MDI forms |
530 |
|
|
|
|
|
|
|
|
||
|
|
Creating an MDI container form |
531 Creating an MDI |
||||||||||
|
|
child form 532 |
Adding a new entry point 533 |
|
|
||||||||
|
16.3 |
Merged menus |
535 |
|
|
|
|
|
|
|
|||
|
|
Assigning merge types |
535 |
Assigning merge order |
537 |
||||||||
|
|
Opening a child form |
541 |
|
|
|
|
|
|
||||
|
16.4 |
MDI children |
543 |
|
|
|
|
|
|
|
|||
|
|
Replacing the toolbar |
543 |
Displaying pixel data |
548 |
|
|||||||
|
|
Opening an album twice |
551 |
Updating the title bar |
553 |
||||||||
|
|
Revisiting the activation events |
|
556 |
|
|
|
||||||
|
16.5 |
MDI child window management |
557 |
|
|
||||||||
|
|
Arranging MDI forms |
558 |
Creating an MDI child list |
561 |
16.6 Recap 563
CONTENTS |
xv |
17 Data binding |
564 |
|
|
|
|
|
|
||
17.1 |
Data grids |
565 |
|
|
|
|
|
|
|
|
Creating the MyAlbumData project |
568 |
|||||||
|
Displaying data in a data grid |
569 |
|
|
|||||
17.2 |
Data grid customization |
573 |
|
|
|
||||
|
Customizing table styles |
574 |
Customizing column styles 576 |
||||||
17.3 |
Editable objects 580 |
|
|
|
|
|
|
||
|
The IEditableObject interface |
580 |
Supporting the |
||||||
|
IEditableObject interface |
582 |
Using editable objects 584 |
||||||
17.4 |
Simple data binding |
586 |
|
|
|
|
|||
|
Altering the MyAlbumData application |
587 |
|||||||
|
Performing simple binding |
590 |
Updating data bound |
||||||
|
controls |
594 Displaying the image |
599 |
||||||
|
Saving changes to bound controls |
601 |
|
||||||
17.5 |
Recap |
602 |
|
|
|
|
|
|
|
18 |
Odds and ends .NET 603 |
|
||||
|
18.1 |
Printing |
604 |
|
|
|
|
|
Using the print classes |
605 |
Drawing a print page 607 |
||
|
18.2 |
Timers |
611 |
|
|
|
|
|
Creating a slide show form |
612 |
|||
|
|
Implementing the slide show behavior 615 |
||||
|
18.3 |
Drag and drop |
618 |
|
|
|
|
|
Initiating drag and drop |
620 Receiving drag and drop 622 |
|||
|
18.4 |
ActiveX controls |
625 |
|
||
|
|
Creating the About box |
626 Wrapping the web browser |
|||
|
|
control |
629 Using the web browser control 631 |
|||
|
18.5 |
Recap |
635 |
|
|
|
A |
C# primer |
637 |
|
|
|
|
B |
.NET namespaces |
674 |
|
|||
C |
Visual index |
680 |
|
|
|
|
D |
For more information |
690 |
|
|||
|
bibliography |
692 |
|
|
|
|
|
index |
695 |
|
|
|
|
xvi |
CONTENTS |
preface
In early 2001 I began using Microsoft’s .NET Framework for a project I was working on with a small startup company. Unfortunately, the winds changed and I found myself with more free time than I would normally hope for. So when Manning Publications asked me if I would contribute to a book on programming with the .NET Framework, I welcomed the idea.
As events unfolded, I found myself with some fairly strong opinions about how such a book should be organized, and offered up a proposal to write a solo book on programming Windows Forms applications. I have always enjoyed the book Programming Windows 95 with MFC by Jeff Prosise, so a book about developing Windowsbased applications with the .NET Framework seemed like an obvious subject.
The core idea behind my proposal was to build a single application over the course of the book. The application would evolve to introduce each topic, so that by the end of the manuscript readers would have a robust application they had built from scratch. Manning Publications seemed to like the idea as well, and thus I suddenly found myself writing this book.
In approaching the task, I set out to achieve two objectives. The first was to provide ample coverage of most of the classes in the namespace. I have been frustrated by many books that do not provide robust examples for a topic. So I try to provide detailed examples that demonstrate how Windows Forms classes can be used and manipulated in real applications.
A second objective was to present advanced user interface topics such as tree views and drag and drop. While the book spends a good deal of time on fundamental classes, such as menus and buttons, more than a cursory glance is given to some of the more complex controls available for Windows-based programming.
The result of my proposal, these objectives, and a number of late nights is the book you see before you. I take a tutorial approach to application development by creating a common application throughout the book, and provide summaries of relevant classes and other topics that might be of further interest. Hopefully, this approach provides enough detail to demonstrate how Windows-based applications are put together with the .NET Framework, and yet offers additional information that should prove helpful as you develop and expand your own .NET projects.
xvii
While the book is not specifically about C# and Visual Studio .NET, the text does attempt to introduce and explain the syntax and usage of C# as well as the features and functionality of Visual Studio .NET. These topics are presented “along-the-way” by introducing relevant concepts and features as they are used in the examples. An overview of C# is also provided in appendix A at the back of the book.
xviii |
PREFACE |