Tải bản đầy đủ (.pdf) (1,077 trang)

Pro NET windows forms and custom

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (22.89 MB, 1,077 trang )

CYAN
MAGENTA

YELLOW
BLACK
PANTONE 123 CV

BOOKS FOR PROFESSIONALS BY PROFESSIONALS ®

THE EXPERT’S VOICE ® IN .NET
Companion eBook Available

Pro .NET 2.0 Windows Forms
and Custom Controls in VB 2005

User Interfaces in VB .NET
(Apress)
User Interfaces in C#
(Apress)
The Book of VB 2005
Microsoft .NET Distributed
Applications

Pro .NET 2.0 Windows
Forms and Custom Controls
in C#

Join online discussions:

Matthew MacDonald (Microsoft MVP in Client Development, MCSD)


forums.apress.com
FOR PROFESSIONALS
BY PROFESSIONALS ™

THE APRESS ROADMAP
Pro VB 2005 and the
.NET 2.0 Platform

Companion eBook
An Introduction to
Object-Oriented Programming
with Visual Basic .NET

Visual Basic .NET
Class Design Handbook

Pro .NET 2.0 Windows
Forms And Custom Controls
In VB 2005

Data Entry and Validation
with C# and VB.NET
Windows Forms

See last page for details
on $10 eBook version

ISBN 1-59059-694-3

SOURCE CODE ONLINE


Controls in VB 2005

Microsoft Visual Basic .NET
Programmer’s Cookbook

.NET 2.0 Windows Forms

Author of

When the .NET Framework first appeared, many assumed it would usher in a
new world of web-only programming. Today, four years after the initial release
of .NET 1.0, Windows applications refuse to die—in fact, they’re thriving.
With .NET 2.0, Microsoft has rediscovered Windows applications. You’ll
find long-requested features, like a flexible web-like layout model, the ability
to display HTML pages, and support for Windows XP visual styles. Entirely
new controls fill major gaps, like the modern ToolStrip and the high-powered
DataGridView. There’s even a whole new deployment system, called ClickOnce,
that offers streamlined installations from a web page and automatic updates. In
short, .NET 2.0 has everything you need to build modern Windows applications.
In this book, you’ll explore the Windows Forms toolkit in detail and learn
how to create everything from a vector-drawing program to a document-view
framework. Best of all, you’ll learn how to take your applications to the next
step by extending the .NET Framework. For example, you’ll learn how to design
over a dozen custom controls that handle everything from Wizard systems to
skinned buttons. And when you discover some of the real limitations that still
exist in .NET—like its meager sound support—you’ll find out how you can solve
the problem with other components.
If you’re looking for the most thorough book about Windows Forms user
interfaces, welcome aboard!


and Custom

Dear Reader,

Pro

54999

www.apress.com

MacDonald

US $49.99
Shelve in Programming/
Microsoft/.NET

6

89253 59694

4

Pro

.NET 2.0 Windows
Forms and Custom
Controls in VB 2005
Create modern user interfaces for Windows applications.


Matthew MacDonald
Foreword by Shawn Burke
Development Manager, Windows Forms Team, Microsoft Corporation

9 781590 596944

User level:
Intermediate–Advanced

this print for content only—size & color not accurate

spine = 1.851" 1,800 page count


Macdonald_694-3FRONT.fm Page i Friday, July 28, 2006 6:48 AM

Pro .NET 2.0 Windows
Forms and Custom
Controls in VB 2005

■■■

Matthew MacDonald


Macdonald_694-3FRONT.fm Page ii Friday, July 28, 2006 6:48 AM

Pro .NET 2.0 Windows Forms and Custom Controls in VB 2005
Copyright © 2006 by Matthew MacDonald
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,

electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-694-4
ISBN-10 (pbk): 1-59059-694-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Jonathan Gennick
Technical Reviewer: Manish Jayaswal
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick,
Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser,
Keir Thomas, Matt Wade
Project Manager: Elizabeth Seymour
Copy Edit Manager: Nicole LeClerc
Copy Editor: Heather Lang
Assistant Production Director: Kari Brooks-Copony
Senior Production Editor: Laura Cheu
Compositor: Susan Glinert Stevens
Proofreader: Liz Berry
Indexer: Michael Brinkman
Artist: April Milne
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or
visit .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA
94710. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit .
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution

has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at in the Source Code section.


Macdonald_694-3FRONT.fm Page iii Friday, July 28, 2006 6:48 AM

For Hamid and Razia


Macdonald_694-3FRONT.fm Page iv Friday, July 28, 2006 6:48 AM


Macdonald_694-3FRONT.fm Page v Friday, July 28, 2006 6:48 AM

Contents at a Glance
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii

PART 1

■■■

Windows Forms Fundamentals

■CHAPTER 1


User Interface Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

■CHAPTER 2

Control Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

■CHAPTER 3

Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

■CHAPTER 4

The Classic Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

■CHAPTER 5

Images and Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

■CHAPTER 6

Lists and Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

■CHAPTER 7

Drawing with GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

■CHAPTER 8

Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263


PART 2

■■■

Custom Controls

■CHAPTER 9

Custom Control Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321

■CHAPTER 10

User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

■CHAPTER 11

Derived Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365

■CHAPTER 12

Owner-Drawn Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389

■CHAPTER 13

Design-Time Support for Custom Controls . . . . . . . . . . . . . . . . . . . . 425

v



Macdonald_694-3FRONT.fm Page vi Friday, July 28, 2006 6:48 AM

PART 3

■■■

Modern Controls

■CHAPTER 14

Tool, Menu, and Status Strips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

■CHAPTER 15

The DataGridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

■CHAPTER 16

Sound and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577

■CHAPTER 17

The WebBrowser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589

PART 4

■■■

Windows Forms Techniques


■CHAPTER 18

Validation and Masked Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611

■CHAPTER 19

Multiple and Single Document Interfaces . . . . . . . . . . . . . . . . . . . . 651

■CHAPTER 20

Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689

■CHAPTER 21

Dynamic Interfaces and Layout Engines . . . . . . . . . . . . . . . . . . . . . . 729

■CHAPTER 22

Help Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777

PART 5

■■■

Advanced Custom Controls

■CHAPTER 23

Skinned Forms and Animated Buttons . . . . . . . . . . . . . . . . . . . . . . . 811


■CHAPTER 24

Dynamic Drawing with a Design Surface . . . . . . . . . . . . . . . . . . . . . 839

■CHAPTER 25

Custom Extender Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871

■CHAPTER 26

Advanced Design-Time Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885

■APPENDIX A

Creating Usable Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927

■APPENDIX B

ClickOnce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963

vi


Macdonald_694-3FRONT.fm Page vii Friday, July 28, 2006 6:48 AM

Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii

PART 1

■■■

■CHAPTER 1

Windows Forms Fundamentals

User Interface Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
The Roles of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Classes and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
User Interface Classes in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Controls Are Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Controls Can Contain Other Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Controls Can Extend Other Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Inheritance and the Form Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Accessing Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Interacting with a Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Overriding Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
The View-Mediator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Smart Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Smart Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Generating User-Interface Code in Visual Studio . . . . . . . . . . . . . . . 24

The Component Tray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The Hidden Designer Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The Application Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Designing Windows Forms Applications . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Developing in Tiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
vii


Macdonald_694-3FRONT.fm Page viii Friday, July 28, 2006 6:48 AM

viii

■C O N T E N T S

■CHAPTER 2

Control Basics

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

The Windows Forms Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
The .NET Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The Control Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Control Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Windows XP Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Position and Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Overlapping Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

Alpha Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Fonts and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
System Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Large Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Access Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Focus and the Tab Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Responding to the Mouse and Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Handling the Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Handling the Mouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
A Mouse/Keyboard Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Mouse Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Low-Level Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

■CHAPTER 3

Forms

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

The Form Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Form Size and Position. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Scrollable Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Showing a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Custom Dialog Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Form Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Form Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Prebuilt Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Resizable Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The Problem of Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

Minimum and Maximum Form Size . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Anchoring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Docking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Autosizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100


Macdonald_694-3FRONT.fm Page ix Friday, July 28, 2006 6:48 AM

■C O N T E N T S

Splitting Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Building Split Windows with Panels . . . . . . . . . . . . . . . . . . . . . . . . . 105
Other Split Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

■CHAPTER 4

The Classic Controls

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

The Classic Control Gallery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
LinkLabel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
TextBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
RichTextBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
CheckBox and RadioButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
PictureBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

Other Domain Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
The Date Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
The DateTimePicker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
MonthCalendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Container Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
The TabControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
AutoComplete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Drag-and-Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
“Fake” Drag-and-Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Authentic Drag-and-Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Extender Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
The NotifyIcon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
ActiveX Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Should You Import ActiveX Controls? . . . . . . . . . . . . . . . . . . . . . . . . 148
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

■CHAPTER 5

Images and Resources

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

The Image Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Common Controls and Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
The ImageList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Adding a Type-Safe Resource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
How Type-Safe Resources Work. . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Form Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Creating Additional Resource Files . . . . . . . . . . . . . . . . . . . . . . . . . . 165


ix


Macdonald_694-3FRONT.fm Page x Friday, July 28, 2006 6:48 AM

x

■C O N T E N T S

Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Creating a Localizable Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
How Localization Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

■CHAPTER 6

Lists and Trees

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

ListView Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
View Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
More Advanced ListViews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
ListView Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Label Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
ListView Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Searching and Hit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
ListView Virtualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
TreeView Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

TreeView Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
TreeView Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Manipulating Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Selecting Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
More Advanced TreeViews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Node Pictures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Expanding and Collapsing Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
TreeView Drag-and-Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208

■CHAPTER 7

Drawing with GDI+

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

Understanding GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Paint Sessions with GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Accessing the Graphics Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Painting and Repainting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Refreshes and Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Painting and Resizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
The Graphics Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Rendering Mode and Antialiasing . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Pens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Drawing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
The GraphicsPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233



Macdonald_694-3FRONT.fm Page xi Friday, July 28, 2006 6:48 AM

■C O N T E N T S

More-Advanced GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Alpha Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Clipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Coordinate Systems and Transformations . . . . . . . . . . . . . . . . . . . . 239
Performing a Screen Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Optimizing GDI+ Painting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Painting and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Double Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Painting Portions of a Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Hit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Painting Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
The ControlPaint Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Visual Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Visual Style Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Drawing with the VisualStyleRenderer . . . . . . . . . . . . . . . . . . . . . . . 258
Using a Control Renderer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

■CHAPTER 8

Data Binding

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

Introducing Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
.NET Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264

Basic Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Data Consumers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Data Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
A Data Access Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Binding to a List (Complex Binding) . . . . . . . . . . . . . . . . . . . . . . . . . 270
Binding to a Grid (Complex Binding) . . . . . . . . . . . . . . . . . . . . . . . . . 272
Binding to Any Control (Simple Binding) . . . . . . . . . . . . . . . . . . . . . 273
Unusual Single-Value Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Common Data-Binding Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Updating with Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Formatting Data with a Format String . . . . . . . . . . . . . . . . . . . . . . . 277
Formatting Data with the Format and Parse Events . . . . . . . . . . . . 279
Advanced Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Creating a Lookup Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Row Validation and Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

xi


Macdonald_694-3FRONT.fm Page xii Friday, July 28, 2006 6:48 AM

xii

■C O N T E N T S

Data Binding Exposed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Navigation with Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Reacting to Record Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Creating Master-Detail Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Creating a New Binding Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

Validating Bound Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Binding to Custom Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Overriding ToString() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Supporting Grid Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Automatic Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Binding Directly to a Database (Table Adapters) . . . . . . . . . . . . . . . 303
Using a Strongly Typed DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Binding Directly to a Custom Object . . . . . . . . . . . . . . . . . . . . . . . . . 310
Data-Aware Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
A Decoupled TreeView with Just-in-Time Nodes . . . . . . . . . . . . . . 314
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

PART 2

■■■

■CHAPTER 9

Custom Controls

Custom Control Basics

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321

Understanding Custom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Types of Custom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Custom Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Control Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
The Library Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
The Disposable Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328

The Client Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Automatic Toolbox Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Customizing the Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
The GAC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Creating a Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Applying a Key to a Control Assembly . . . . . . . . . . . . . . . . . . . . . . . 334
Attaching Keys in Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Installing a Control in the GAC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336


Macdonald_694-3FRONT.fm Page xiii Friday, July 28, 2006 6:48 AM

■C O N T E N T S

■CHAPTER 10 User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Understanding User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
The Progress User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Creating the Progress User Control . . . . . . . . . . . . . . . . . . . . . . . . . 338
Testing the Progress User Control . . . . . . . . . . . . . . . . . . . . . . . . . . 340
The Back Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
User Control Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
An Automatic Progress Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
The Bitmap Thumbnail Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Creating the BitmapViewer User Control . . . . . . . . . . . . . . . . . . . . . 345
Testing the BitmapViewer Control . . . . . . . . . . . . . . . . . . . . . . . . . . 351
BitmapViewer Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Performance Enhancements and Threading . . . . . . . . . . . . . . . . . . 354
Simplifying Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
User Controls and Dynamic Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 356

The Wizard Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
The Wizard Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
The Wizard Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Testing the Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364

■CHAPTER 11 Derived Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Understanding Derived Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Extending Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Derived Controls or User Controls? . . . . . . . . . . . . . . . . . . . . . . . . . . 367
The ProjectTree Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
The Data Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Node Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Node Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Adding Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Project Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
A Custom TreeNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Design-Time Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
The DirectoryTree Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Filling the Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Directory Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380

xiii


Macdonald_694-3FRONT.fm Page xiv Friday, July 28, 2006 6:48 AM

xiv

■C O N T E N T S


Deriving Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
A Simple Derived Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Making an Ancestor Control Available . . . . . . . . . . . . . . . . . . . . . . . 383
Adding a Property in the Ancestor Form . . . . . . . . . . . . . . . . . . . . . 383
Dealing with Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387

■CHAPTER 12 Owner-Drawn Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Understanding Owner-Drawn Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
A Simple Owner-Drawn ListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
A More Advanced Owner-Drawn ListBox . . . . . . . . . . . . . . . . . . . . . 391
An Owner-Drawn TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Owner-Drawn Custom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Double Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
The MarqueeLabel Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
The GradientPanel Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
The SimpleChart Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
The CollapsiblePanel Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423

■CHAPTER 13 Design-Time Support for Custom Controls . . . . . . . . . . . . . . 425
Design-Time Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
The Key Players . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Basic Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Attributes and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
The Toolbox Bitmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Debugging Design-Time Support . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Code Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Basic Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Making Serialization Decisions Programmatically . . . . . . . . . . . . . 440
Serialization Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Batch Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Localizable Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445


Macdonald_694-3FRONT.fm Page xv Friday, July 28, 2006 6:48 AM

■C O N T E N T S

Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Dealing with Nested Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Creating a Type Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Attaching a Type Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
The ExpandableObjectConverter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Creating a Nested Object with a Constructor . . . . . . . . . . . . . . . . . 457
Custom Serialization with CodeDOM . . . . . . . . . . . . . . . . . . . . . . . . 459
Providing Standard Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Type Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Using Prebuilt Type Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Using Custom Type Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474

PART 3

■■■

Modern Controls


■CHAPTER 14 Tool, Menu, and Status Strips . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
ToolStrip Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
The ToolStripItem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
The ToolStripContainer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
The StatusStrip and MenuStrip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Creating a Status Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
ToolStrip Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Main Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Context Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
ToolStrip Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Hosting Other Controls in the ToolStrip . . . . . . . . . . . . . . . . . . . . . . 503
Taking Control of Overflow Menus . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Allowing Runtime Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Customizing the ToolStrip Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
The ToolStripManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Customizing a Renderer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Changing the Colors of the ProfessionalToolStripRenderer . . . . . . 517
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518

xv


Macdonald_694-3FRONT.fm Page xvi Friday, July 28, 2006 6:48 AM

xvi

■C O N T E N T S

■CHAPTER 15 The DataGridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
The DataGrid Legacy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

Introducing the DataGridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
The DataGridView and Very Large Data Sources . . . . . . . . . . . . . . 521
Bare-Bones Data-Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
The DataGridView Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Column Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Creating an Unbound Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
Cell Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Navigation Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Column-Based Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Formatting the DataGridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Column and Row Resizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
DataGridView Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Custom Cell Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Hiding, Moving, and Freezing Columns . . . . . . . . . . . . . . . . . . . . . . 548
Using Image Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Using Button Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Editing and Validation with the DataGridView . . . . . . . . . . . . . . . . . . . . . 555
Editing Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Default Values for New Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
Validating Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Constraining Choices with a List Column. . . . . . . . . . . . . . . . . . . . . 562
DataGridView Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Custom Cell Painting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Custom Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Custom Cell Edit Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575

■CHAPTER 16 Sound and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
The SoundPlayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577

Synchronous and Asynchronous Playback . . . . . . . . . . . . . . . . . . . 578
System Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
Advanced Media with DirectShow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Using Quart.dll Through Interop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Playing MP3, MIDI, WMA, and More . . . . . . . . . . . . . . . . . . . . . . . . . 582
Showing MPEG and Other Video Types . . . . . . . . . . . . . . . . . . . . . . 586
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588


Macdonald_694-3FRONT.fm Page xvii Friday, July 28, 2006 6:48 AM

■C O N T E N T S

■CHAPTER 17 The WebBrowser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
WebBrowser Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
Navigating to a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
WebBrowser Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
A WebBrowser Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Printing, Saving, and Fine-Tuning. . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Blending Web and Windows Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . 597
Build a DOM Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
Extract All Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
Scripting a Web Page with .NET Code . . . . . . . . . . . . . . . . . . . . . . . 602
Scripting an HTML Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607

PART 4

■■■


Windows Forms Techniques

■CHAPTER 18 Validation and Masked Editing . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Validating at the Right Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Validation Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
The Validation Event Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Handling Validation Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
Closing a Form with Validating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
The ErrorProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
Showing Error Icons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
Customizing Error Icons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Regular Expression Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Validating with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 624
Custom Validation Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Understanding the ASP.NET Validation Controls . . . . . . . . . . . . . . . 626
Building the BaseValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
Building Three Custom Validators . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Using the Custom Validators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Masked Edit Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
Creating a Mask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
The MaskedTextBox Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
MaskedTextBox Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Registering a Custom Mask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Creating Custom Masked Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 646
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650

xvii



Macdonald_694-3FRONT.fm Page xviii Friday, July 28, 2006 6:48 AM

xviii

■C O N T E N T S

■CHAPTER 19 Multiple and Single Document Interfaces . . . . . . . . . . . . . . . 651
The Evolution of Document Interface Models . . . . . . . . . . . . . . . . . . . . . 651
MDI Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654
Finding Your Relatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
Synchronizing MDI Children . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
MDI Window List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
MDI Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
Merging Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Managing Interface State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
Document-View Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
A Document-View Ordering Program . . . . . . . . . . . . . . . . . . . . . . . . 667
Multiple-Document SDI Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
Gaps in the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687

■CHAPTER 20 Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
Multithreading Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
The Goals of Multithreading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
Options for Asynchronous Programming . . . . . . . . . . . . . . . . . . . . . 691
Asynchronous Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
Polling and Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
Multithreading in a Windows Application . . . . . . . . . . . . . . . . . . . . . . . . . 696
The Worker Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
The Asynchronous Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699

Marshalling Calls to the Right Thread . . . . . . . . . . . . . . . . . . . . . . . 700
Using a Delayed Update . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
The BackgroundWorker Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
A Simple BackgroundWorker Test . . . . . . . . . . . . . . . . . . . . . . . . . . 707
Tracking Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
Supporting a Cancel Feature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
The Thread Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712
Locking and Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
Creating a ThreadWrapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
Creating the Derived Task Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Creating and Tracking Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
Improving the Thread Wrapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
Task Queuing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727


Macdonald_694-3FRONT.fm Page xix Friday, July 28, 2006 6:48 AM

■C O N T E N T S

■CHAPTER 21 Dynamic Interfaces and Layout Engines . . . . . . . . . . . . . . . . 729
The Case for Dynamic User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
Dynamic Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730
An Adaptable Menu Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
A Database-Driven Adaptable Menu . . . . . . . . . . . . . . . . . . . . . . . . . 733
Creating Controls at Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
Managing Control Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
The Layout Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
A Simple Handmade Layout Manager . . . . . . . . . . . . . . . . . . . . . . . 739
Problems with the Simple Layout Manager . . . . . . . . . . . . . . . . . . . 743

Layout Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
Creating a Custom Layout Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
The FlowLayoutPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
The FlowBreak Extended Property . . . . . . . . . . . . . . . . . . . . . . . . . . 748
Margins and Padding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
Automatic Scrolling and Sizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
The TableLayoutPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
Row and Column Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
Generating New Columns and Rows . . . . . . . . . . . . . . . . . . . . . . . . 754
Positioning Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
Extended Properties with the TableLayoutPanel . . . . . . . . . . . . . . . 756
Layout Panel Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757
TableLayoutPanel: A Localizable Dialog Box . . . . . . . . . . . . . . . . . . 757
TableLayoutPanel: Bi-Pane Proportional Resizing . . . . . . . . . . . . . 759
TableLayoutPanel: A List of Settings . . . . . . . . . . . . . . . . . . . . . . . . 760
TableLayoutPanel: Forms from a File . . . . . . . . . . . . . . . . . . . . . . . . 762
FlowLayoutPanel: A Modular Interface . . . . . . . . . . . . . . . . . . . . . . . 771
Markup-Based User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774
WFML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775

■CHAPTER 22 Help Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Understanding Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Classic “Bad Help” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
Types of Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
Help-Authoring Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783

xix



Macdonald_694-3FRONT.fm Page xx Friday, July 28, 2006 6:48 AM

xx

■C O N T E N T S

Basic Help with the HelpProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
Simple Pop-Ups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786
External Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
Compiled Help Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
HTML Help with the HelpProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788
Creating a Basic HTML Help File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788
Using Context-Sensitive Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
Control-Based and Form-Based Help . . . . . . . . . . . . . . . . . . . . . . . . 794
Invoking Help Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795
Using Database-Based Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796
Using Task-Based Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
Creating Your Own Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799
Application-Embedded Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800
Affordances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801
Agents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807

PART 5

■■■

Advanced Custom Controls


■CHAPTER 23 Skinned Forms and Animated Buttons . . . . . . . . . . . . . . . . . . 811
Shaped Forms and Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
A Simple Shaped Form. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812
Creating a Background for Shaped Forms . . . . . . . . . . . . . . . . . . . . 813
Moving Shaped Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817
Shaped Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818
Animated Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
Basic Animated Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
A Base Class for Animated Buttons . . . . . . . . . . . . . . . . . . . . . . . . . 819
Improving the Performance of Owner-Drawn Controls . . . . . . . . . . . . . 834
Caching Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
Reusing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838

■CHAPTER 24 Dynamic Drawing with a Design Surface . . . . . . . . . . . . . . . 839
A Drawing Program with Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
The Shape Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840
The Drawing Surface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843


Macdonald_694-3FRONT.fm Page xxi Friday, July 28, 2006 6:48 AM

■C O N T E N T S

A Drawing Program with Shape Objects . . . . . . . . . . . . . . . . . . . . . . . . . 848
The Shape Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849
The Shape Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
The Drawing Surface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869


■CHAPTER 25 Custom Extender Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871
Understanding Extender Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871
The StatusStripHelpLabel Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872
Choosing a Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873
Choosing the Control to Extend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873
Providing the Extended Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874
Implementing the SetXxx() and GetXxx() Methods . . . . . . . . . . . . . 875
Testing the Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877
Changing How Extended Properties Appear . . . . . . . . . . . . . . . . . . 877
The HelpIconProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 878
Choosing a Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 878
Providing the Extended Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883

■CHAPTER 26 Advanced Design-Time Support . . . . . . . . . . . . . . . . . . . . . . . . . 885
Control Designers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
Filtering Properties and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
Interacting with the Mouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 893
Selection and Resize Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894
Designer Verbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895
Designer Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899
Smart Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 904
The Action List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 905
The DesignerActionItem Collection . . . . . . . . . . . . . . . . . . . . . . . . . . 908
The Control Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 910
Container and Collection Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 910
Collection Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911
Container Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918
Licensing Custom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
Simple LIC File Licensing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921

Custom LIC File Licensing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922
More-Advanced License Providers . . . . . . . . . . . . . . . . . . . . . . . . . . 923
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926

xxi


Macdonald_694-3FRONT.fm Page xxii Friday, July 28, 2006 6:48 AM

xxii

■C O N T E N T S

■APPENDIX A Creating Usable Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
Why Worry About the Interface? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
A Brief History of User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
The Command-Line Era . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929
The Question-Answer Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
The Menu-Driven Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932
The GUI Era . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932
Creativity vs. Convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
Consistency in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
The “Act Like Microsoft Office” Principle. . . . . . . . . . . . . . . . . . . . . 935
Administrative Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935
Know Your Application Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
Know Your User. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
Handling Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
Segmenting Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938
Inductive User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939
Helpful Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939

Restricting the User’s Ability to Make a Mistake . . . . . . . . . . . . . . . 939
Restricting the User’s Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
Restricting the User’s Imagination . . . . . . . . . . . . . . . . . . . . . . . . . . 941
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941

■APPENDIX B ClickOnce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
The Ground Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944
The ClickOnce Installation Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 944
ClickOnce Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
ClickOnce Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
A Simple ClickOnce Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
Choosing a Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
Deployed Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951
Installing a ClickOnce Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 952
Updating a ClickOnce Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 953
ClickOnce Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954
Publish Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954
Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956
Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957


Macdonald_694-3FRONT.fm Page xxiii Friday, July 28, 2006 6:48 AM

■C O N T E N T S

ClickOnce Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
ClickOnce Security Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
Partial Trust and ClickOnce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961


■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963

xxiii


Macdonald_694-3FRONT.fm Page xxiv Friday, July 28, 2006 6:48 AM


×