Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Windows Forms in Action
Nội dung xem thử
Mô tả chi tiết
Windows Forms in Action
Windows Forms
in Action
Second Edition of
Windows Forms Programming with C#
ERIK BROWN
MANNING
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: [email protected]
©2006 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: Liz Welch
209 Bruce Park Avenue Typesetter: Dottie Marsico
Greenwich, CT 06830 Cover designer: Leslie Haimes
ISBN 1932394-65-6
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – VHG – 10 09 08 07 06
In memory of Thelma Rose Wilson,
and for her beautiful daughter, whom I still love
vii
brief contents
Part 1 Hello Windows Forms 1
1 Getting started with Windows Forms 3
2 Getting started with Visual Studio 33
Part 2 Basic Windows Forms 63
3 Menus 67
4 Context menu and status strips 92
5 Reusable libraries 116
6 Files and common dialog boxes 148
7 Dialog boxes 180
8 Text boxes 212
9 Buttons 240
10 Handling user input and encryption 268
11 List boxes 299
12 Combo boxes 327
13 Tab controls and pages 356
14 Dates, calendars, and progress bars 383
15 Bells and whistles 415
16 Tool strips 443
viii
Part 3 Advanced Windows Forms 475
17 Custom controls 477
18 Explorer interfaces and tree views 507
19 List views 541
20 Multiple document interfaces 575
21 Data binding 608
22 Two-way binding and binding sources 637
23 Odds and ends .NET 665
appendix A C# primer 701
appendix B .NET namespaces 735
appendix C Visual index 741
appendix D For more information 758
ix
contents
preface xix
preface to the first edition xxi
acknowledgments xxiii
about this book xxv
what’s new in 2.0 xxxiii
about the cover illustration xxxv
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 8 ✦ The Main method 11 ✦ The Application
class 12 ✦ Program execution 13
1.2 Windows Forms controls 14
The using directive 15 ✦ The Control class 17
The Controls property 18
1.3 Loading files 19
Events 21 ✦ The OpenFileDialog class 23 ✦ Bitmap
images 26
1.4 Resizing forms 26
The Anchor property 29 ✦ The Dock property 30
1.5 Recap 32
2 Getting started with Visual Studio 33
2.1 Programming with Visual Studio 34
Creating a project 35 ✦ Executing a program 37
Viewing the source code 38
x
2.2 Windows Forms controls 43
The AssemblyInfo file 43 ✦ Renaming a form 46
The Toolbox window 46
2.3 Loading files 52
Handling events in Visual Studio 52 ✦ Handling exceptions 56
2.4 Resizing forms 58
Assigning the Anchor property 58 ✦ Assigning the Dock
property 60
2.5 Recap 61
Part 2 Basic Windows Forms 63
3 Menus 67
3.1 Controls and containers 68
Control classes 68 ✦ Container classes 70
3.2 The nature of menus 72
Menu terminology 72 ✦ Menus in .NET 73
3.3 Menu bars 75
Adding a menu strip 75 ✦ Adding a menu item 78
Adding drop-down menu items 81
3.4 Menu handling 87
Adding handlers via the designer window 87
Adding handlers via the properties window 88
3.5 Recap 90
4 Context menu and status strips 92
4.1 Context menu strips 93
Creating a context menu 94 ✦ Adding items to a context
menu 95 ✦ Sharing a context menu 97
4.2 Drop-down events and event arguments 99
Handling a submenu item click 100 ✦ Altering a submenu before
it appears 105
4.3 Status strips 108
Creating a status strip 108 ✦ Adding status strip labels 109
4.4 Recap 115
xi
5 Reusable libraries 116
5.1 Class libraries 117
Creating a class library 118 ✦ Using the command-line
tools 121 ✦ Creating the Photograph class 122
Defining class properties 125
5.2 The object class 128
Comparing object and Object 128 ✦ Overriding object
methods 128
5.3 Interfaces 131
Interfaces and collections 132 ✦ Appreciating collection
classes 133
5.4 Generics 138
Generic classes 138 ✦ Appreciating generic collections 140
Disposing of resources 144
5.5 Recap 146
6 Files and common dialog boxes 148
6.1 Standard menus 149
Changing the menu bar 149 ✦ Managing an album 152
6.2 Common file dialog boxes 159
Creating a new album 159 ✦ Opening an album 161
Saving an album 163 ✦ Printing and exiting 166
6.3 Streams and writers 167
Stream classes 167 ✦ Writing an album to disk 169
Reading an album from disk 173
6.4 Album management 174
Adding and removing images 174 ✦ Navigating an album 176
Displaying album status 178
6.5 Recap 178
7 Dialog boxes 180
7.1 Message boxes 181
Creating an OK message box 183 ✦ Creating a YesNo message
box 184 ✦ Creating a YesNoCancel message box 185
7.2 The Form.Close method 188
Comparing Close and Dispose 188
Intercepting the Form.Close method 190
xii
7.3 Forms and panels 192
The Form class 193 ✦ Creating a dialog box 195
7.4 Modeless dialog boxes 196
Creating a modeless dialog box 197 ✦ Adding panels and labels to
a form 199 ✦ Updating the PixelDialog controls 203
Displaying a modeless dialog box 206 ✦ Updating the PixelDialog
form 208 ✦ Tracking mouse movement 209
7.5 Recap 211
8 Text boxes 212
8.1 Form inheritance 213
Creating a base form 214 ✦ Creating a derived form 217
8.2 Standard text boxes 219
Adding text boxes 220 ✦ Adding a multiline text box 225
Hooking up a dialog box 229
8.3 Masked text boxes 231
Handling key presses 231 ✦ Masking text 233
8.4 Recap 239
9 Buttons 240
9.1 Push buttons 241
The Button class 241 ✦ Expanding the PhotoAlbum class 243
Storing album data 245
9.2 Radio buttons 246
The RadioButton class 247 ✦ Creating an album dialog
box 248
9.3 Check box buttons 250
The CheckBox class 250 ✦ Using check box buttons 251
Completing the AlbumEditDialog form 253 ✦ Hooking up a
dialog box (again) 255
9.4 Tooltips and error providers 256
The ToolTip class 256 ✦ Displaying tooltips 258
Building extender providers 261 ✦ The ErrorProvider class 263
9.5 Recap 266
10 Handling user input and encryption 268
10.1 Keyboard events 269
The three events 269 ✦ Handling the KeyPress event 269
Handling the KeyDown and KeyUp events 271
Preprocessing command keys 272
xiii
10.2 Mouse events 274
The MouseEventArgs class 274 ✦ Implementing
FlybyTextProvider using mouse events 277 ✦ Using the
FlybyTextProvider class 280
10.3 Encryption 282
The Cryptography namespace 282 ✦ Encrypting data 284
Writing encrypted data 286 ✦ Reading encrypted data 287
Storing encrypted albums 288
10.4 Link labels 291
The LinkLabel class 292 ✦ Saving encrypted albums 293
Opening encrypted albums 294
10.5 Recap 297
11 List boxes 299
11.1 List controls 300
The ListControl classes 300 ✦ Creating a new solution 302
Refactoring application code 303
11.2 Simple list boxes 308
The ListBox class 310 ✦ Displaying a data source 311
Selecting list items 315
11.3 Multiselection list boxes 317
Enabling multiple selection 318 ✦ Reordering list items 321
Removing list items 324
11.4 Recap 325
12 Combo boxes 327
12.1 Standard combo boxes 328
The ComboBox class 328 ✦ Creating a combo box 330
Selecting combo box items 332 ✦ Modifying the data
source 333
12.2 Editable combo boxes 335
Creating a combo box (again) 336 ✦ Updating a combo box
dynamically 339
12.3 Automatic completion 341
Understanding automatic completion 341
Using auto-completion in a combo box 344
12.4 Item formatting 345
The formatting interfaces 345 ✦ Implementing an IFormattable
interface 346 ✦ Using formatting in list controls 350
Implementing custom formatters 352
12.5 Recap 355
xiv
13 Tab controls and pages 356
13.1 Tab control fundamentals 357
The TabControl class 357 ✦ Creating tab controls 358
13.2 Tab page fundamentals 362
The TabPage class 362 ✦ Creating tab pages 363
Enclosing existing controls in a tab page 366
13.3 Owner-drawn tabs 369
The DrawItem event 370 ✦ Drawing page tabs 371
Using a custom tab control 375
13.4 Owner-drawn list boxes 376
Custom list items 377 ✦ Assigning item size 378 ✦ Drawing
list items 380
13.5 Recap 381
14 Dates, calendars, and progress bars 383
14.1 Date and time display 384
The DateTimePicker class 384 ✦ Creating a DateTimePicker
control 387
14.2 Date and time formats 389
Formatting date and time values 390 ✦ Customizing a
DateTimePicker control 393
14.3 Calendars 394
The MonthCalendar class 395 ✦ The UserControl class 396
Creating a calendar control 397 ✦ Initializing a calendar 399
Handling mouse clicks in a calendar control 401
14.4 Progress bars 405
Creating an images user control 405 ✦ The ProgressBar
class 408 ✦ Performing a background task 410
14.5 Recap 413
15 Bells and whistles 415
15.1 Images and cursors 416
Bitmaps 416 ✦ Icons 420 ✦ Cursors 425
15.2 Embedded resources and sounds 426
Embedding a resource 427 ✦ Using the project resource file 428
Playing a sound 431
15.3 Track bars 433
The TrackBar class 434 ✦ Using track bars 435