user interfaces in vb .net: windows forms and custom controls978-1-4302-0844-0/1.pdf · user...

24
User Interfaces in VB .NET: Windows Forms and Custom Controls MATTHEW MACDONALD APress Media, LLC

Upload: truongcong

Post on 17-Aug-2019

248 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

User Interfaces in VB .NET: Windows Forms

and Custom Controls MATTHEW MACDONALD

APress Media, LLC

Page 2: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

User Interfaces in VB .NET: Windows Forms and Custom Controls Copyright ©2002 by Matthew MacDonald Originally published by Apress in 2002 AlI rights reserved. No part of this work may be reproduced ar transmitted in any form ar by any means, electronic ar mechanical, including photocopying, recording, ar by any information storage ar retrieval system, without the prior wrltten permission of the copyright owner and the publisher.

ISBN 978-1-59059-044-7 ISBN 978-1-4302-0844-0 (eBook) DOI 10.1007/978-1-4302-0844-0

Trademarked names may appear in this boole. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and ta the benefit ofthe trademark owner, with no intention ofinfringement ofthe trademark.

Technical Reviewer: Gordon WIlmot

Editorial Directors: Dan Appleman, Peter Blackburn, Gary Cornell, Iason Gilmore, Karen Watterson, lohn Zukowski

Managing Editor: Grace Wong

Project Manager: Sofia Marchant

Copy Editor: Anne Friedman

Production Editor: Kari Brooks

Compositor: Susan Glinert Stevens

Artist: Kurt Krames

Indexer: Nancy Guenther

Cover Designer: Kurt Krames

Manufacturing Manager: Tom Debolski

Marketing Manager: Stephanie Rodriguez

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 nor Apress shall have any liability ta any person ar entity with respect ta any loss ar damage caused or alleged ta be caused direcdy ar indirectly by the information contained in this worle.

Page 3: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

For Nora and Paul

Page 4: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Contents at a Glance

Preface ................................................................................................................. xvii

About the Author ................................................................................................. xix

About the Technical Reviewer ....................................................................... xxi

Acknowledgments ................................................................................................. xxiii

Introduction .......................................................................................................... xxv

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6

Chapter 7

Chapter 8

Chapter 9

Chapter 10

Chapter 11

Chapter 12

Chapter 13 Chapter 14

Creating Usable Interfaces ............................................... 1

Designing with Classes and Tiers ................................ 21

Control Class Basics .......................................................... 55

Classic Controls ................................................................... 81

Forms ......................................................................................... 139

Modern Controls ................................................................... 197

Custom Controls ................................................................... 255

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

Data Controls ........................................................................ 339

MDI Interfaces and Workspaces ..................................... 389

Dynamic User Interface .................................................... 425

GDI+ Basics ............................................................................ 455

GDI+ Controls ........................................................................ 491

Help and Application-Embedded Support ................... 519

Index ...................................................................................................................... 549

v

Page 5: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Contents

Preface ............................................................................................................. xvii

About the Author ........................................................................................... xix

About the Technical Reviewer ......................................................... xxi

Acknowledgments ......................................................................................... xxiii

Introduction .................................................................................................. xxv

About This Book ................................................................................................. xxv Chapter Overview ........................................................................................... xxviii

Chapter 1 Creating Usable Interfaces .................................... 1

Why Worry About the Interface? ................................................................... 2 A Brief History of User Interfaces .......................................................... 3

The Command-Line Era ...................................................................................... 3 The Question-Answer Model .............................................................................. 5 The Menu-Driven Model .................................................................................... 7 The GUI Era .......................................................................................................... 8

Creativity vs. Convention .............................................................................. 9 Consistency in .NET .......................................................................................... 10 The "Act-Like-Microsoft" Principle .................................................................. 11 Administrative Utilities ..................................................................................... 11 Know Your Application Type ............................................................................ 13 KnowYourUser ................................................................................................. 13

Handling Complexity ......................................................................................... 14 Segmenting Information ................................................................................... 15 Inductive User Interface ................................................................................... 16

Helpful Restrictions ....................................................................................... 16 Restricting the User's Ability to Make a Mistake ............................................. 16 Restricting the User's Choices .......................................................................... 17 Restricting the User's Imagination ................................................................... 18

Programming User Interface for the Web ................................................ 19 The Last Word ...................................................................................................... 19

vii

Page 6: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Contents

Chapter 2 Designing with Classes and Tiers ................... 21

Classes and Objects .......................................................................................... 22 The Roles of Classes ........................................................................................... 22 Classes and Types ............................................................................................... 23 More About Objects ........................................................................................... 26

User Interface Classes in • NET .................................................................. 26 Controls Are Classes ........................................................................................... 27 Controls Contain Other Controls ...................................................................... 28 Controls Derive from Other Controls ............................................................... 30 Inheritance and the Form Class ........................................................................ 32 The Controls Collection ..................................................................................... 34

Generating Code with Visual Studio .NET ....................................................... 35 Interacting with a Control ........................................................................... 37

The View-Mediator Pattern ............................................................................... 37 Smart Controls .................................................................................................... 39 Smart Forms ....................................................................................................... 40

Encapsulation ....................................................................................................... 41 Use Enumerations and Resource Classes ........................................................ 42 Use Collections ................................................................................................... 42 Restrain from Sharing Control References .................................................. .... .42 Define a Data Transfer Plan ............................................................................... 42

Use a Central Switchboard ................................................................................ 43

Create Data-Driven User Interfaces ................................................................. 43

Developing in Tiers .......................................................................................... 44 Problems with Three-Tier Design ..................................................................... 45

Three-Tier Design Consistency ......................................................................... 47 Fixing Three- Tier Design ............................................................................... 48

The DataSet to the Rescue ................................................................................. 50 Validation and Business Objects ....................................................................... 51 Other Types of Application ................................................................................ 51

The Last Word ....................................................................................................... 52

Chapter 3 Control Class Basics .................................................. 55

The Windows Forms Package ............................................................................. 55 The .NET Solution .............................................................................................. 56

The Control Class .............................................................................................. 58 Position and Size .............................................................................................. 62 Color ........................................................................................................................ 64

Fonts and Text ..................................................................................................... 67 Access Keys ......................................................................................................... 68

Control Relations .............................................................................................. 69

viii

Page 7: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Contents

Focus and the Tab Sequence .......................................................................... 70 Responding to the Mouse and Keyboard .................................................... 73

A Mouse/Keyboard Example ............................................................................ 76 Mouse Cursors ................................................................................................... 76

Graphics and Painting ..................................................................................... 78 Low-Level Members .............................................................................................. 79 The Last Word ...................................................................................................... 80

Chapter 4 Classic Controls ............................................................ 81

Types of Controls .............................................................................................. 81 Invisible Controls ............................................................................................... 81 Provider Controls ............................................................................................... 83

ActiveX Controls ................................................................................................ 84 Should You Import ActiveX Controls? .............................................................. 86

The Classic Control Gallery ........................................................................ 88 Labels .................................................................................................................. 88 UnkLabel ............................................................................................................ 89 Button ................................................................................................................. 91 TextBox ............................................................................................................... 92 CheckBox and RadioButton .............................................................................. 94 PictureBox .......................................................................................................... 94 list Controls ....................................................................................................... 95 Other Domain Controls ..................................................................................... 99 Organizational Controls .................................................................................. 102

The Date Controls ............................................................................................ 102 The DateTimePicker ........................................................................................ 103 MonthCalendar ................................................................................................ 106

Menus ...................................................................................................................... 108 The Menu Class ................................................................................................ 110 The MainMenu and ContextMenu Classes ................................................... 111 The Menultem Class ........................................................................................ 112 The Visual Studio .NET Menu Designer ........................................................ 114 Attaching a Menu ............................................................................................. 116

Menu Events .................................................................................................... 117 Copying and Cloning a Menu ......................................................................... 118 Merging a Menu ............................................................................................... 118

Owner-Drawn Menus ............................................................................................ 119 An Owner-Drawn Menu Control .................................................................... 122

Drag-and-Drop .................................................................................................... 126 "Fake" Drag-and-Drop .................................................................................... 126 Authentic Drag-and-Drop .............................................................................. 128

ix

Page 8: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Contents

Validation ............................................................................................................ 131 A Validation Example ....................................................................................... 133 Validating with the ErrorProvider ................................................................... 134 Validating with Regular Expressions .............................................................. 136

The Last Word ..................................................................................................... 138

Chapter 5 Forms ....................................................................................... 139

The Form Class ................................................................................................... 139 Form Size and Position .................................................................................... 144 Scrollable Forms ............................................................................................... 148 Showing a Form ................................................................................................ 149 Custom Dialog Windows ................................................................................. 150 IgnoreForm Interaction ................................................................................... 153 Form Ownership ............................................................................................... 156 Windows XP Styles ........................................................................................... 158

Prebuilt Dialogs .............................................................................................. 160 Resizable Forms ................................................................................................. 165

The Problem of Size .......................................................................................... 165 A Traditional Solution ...................................................................................... 166 Minimum and Maximum Size ........................................................................ 169 Anchoring .......................................................................................................... 170 Docking ............................................................................................................. 174

Splitting Windows ............................................................................................ 176 Docking with Panels ......................................................................................... 177 Other Split Windows ........................................................................................ 179

Irregularly Shaped Forms ............................................................................. 183 Shaped Form Content ...................................................................................... 185 Moving Shaped Forms ..................................................................................... 187 Forms with Holes ............................................................................................. 189

Visual Inheritance .......................................................................................... 190 Making an Ancestor Control Available ........................................................... 192 Adding an Ancestor Property .......................................................................... 192 Overriding an Event Handler .......................................................................... 193

The Last Word ..................................................................................................... 195

Chapter 6 Modern Controls ............................................................. 197

The ImageList ..................................................................................................... 197 Dealing with the lmageUst in Code ................................................................ 199

ListView and TreeView .................................................................................... 201 Basic ListView ................................................................................................... 202

X

Page 9: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Contents

Advanced ListView Tricks ............................................................................ 208 Label Editing .................................................................................................... 212 Adding Information to a ListView .................................................................. 213

Basic TreeView .................................................................................................. 213 TreeView Structure .......................................................................................... 214 TreeViewNavigation ....................................................................................... 216 Manipulating Nodes ........................................................................................ 219 Selecting Nodes ................................................................................................ 222

Advanced TreeView Tricks ............................................................................ 224 Node Pictures ................................................................................................... 225 Expanding and Collapsing Levels ................................................................... 226 TreeViewDrag-and-Drop ............................................................................... 227

Taming the TreeView ....................................................................................... 231 A Project Tree ................................................................................................... 231 A Data-Aware Tree View .................................................................................. 233 Unusual Trees .................................................................................................. 235 Design-Time Support for the Custom Tree View ........................................... 236

The ToolBar ......................................................................................................... 237 Synchronizing the ToolBar .......................................................................... 241 The StatusBar .................................................................................................... 245 Basic StatusBar ................................................................................................ 246

Synchronizing the StatusBar to a Menu ........................................................ 248 The TabControl .................................................................................................. 250 The Notifyicon .................................................................................................. 252 The Last Word .................................................................................................... 253

Chapter 7 Custom Controls ............................................................ 255

Types of Custom Controls ............................................................................ 256 User Controls .................................................................................................... 256

Creating User Controls .................................................................................... 257 The Progress User Control .............................................................................. 259 The Bitmap Thumbnail Viewer ...................................................................... 264 Testing the Bitmap Viewer ............................................................................... 270 Bitmap Viewer Events ...................................................................................... 270 Bitmap Viewer Enhancements and Threading .............................................. 272

Inherited Controls ......................................................................................... 275 Inherited Controls or User Controls? ............................................................. 276 The DirectoryTree Control .............................................................................. 276 Testing the DirectoryTree ............................................................................... 279 A Masked TextBox Control .............................................................................. 280

xi

Page 10: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Contents

Custom Extender Providers ........................................................................... 283 The Menu Text Provider .................................................................................. 284 The Help Icon Provider .................................................................................... 288

The Last Word ..................................................................................................... 291

Chapter 8 Design-Time Support for Custom Controls ................................................... 293

Control Projects .............................................................................................. 294 The Class Library Project ................................................................................. 294 Referencing a Custom Control ........................................................................ 296 TheGAC ............................................................................................................ 298

Control Designer Basics ............................................................................... 300 Attributes ........................................................................................................... 300 Basic Serialization ............................................................................................ 305 The Toolbox Bitmap ......................................................................................... 306 Resource Files ................................................................................................... 308

Testing Custom Controls ............................................................................... 312 Debugging Design-Time Support ................................................................... 313 Testing for Design Mode .................................................................................. 314 The PropertyGrid Control ................................................................................ 315

Custom Designers .............................................................................................. 316 Filtering Properties and Events ....................................................................... 318 Designer Verbs .................................................................................................. 319 Control Designer Notifications ....................................................................... 325

Data Types and UITypeEditors .................................................................... 326 Using Prebuilt UITypeEditors ......................................................................... 328 Custom UITypeEditors .................................................................................... 330

Licensing Custom Controls ........................................................................... 332 Simple UC File Licensing ................................................................................ 332 Custom UC File Licensing ............................................................................... 334 Advanced License Providers ............................................................................ 334

The Last Word ..................................................................................................... 337

Chapter 9 Data Controls .................................................................. 339

Introducing Data Binding ............................................................................. 339 Basic Data Binding .......................................................................................... 340

Simple List Binding .......................................................................................... 342 Binding Lists to Complex Objects ................................................................... 343 Single-Value Binding ........................................................................................ 346

xii

Page 11: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Contents

ADO. NET Data Binding ..................................................................................... 349 Multiple Control Binding ................................................................................ 353 Updating with Data Binding ........................................................................... 354 Formatting Data Before Binding .................................................................... 356

Advanced Conversions .................................................................................... 359

Row Validation and Changes .......................................................................... 362

Data Binding Exposed ..................................................................................... 363 Navigation with Data Binding ........................................................................ 364

Reacting to Record Navigation ....................................................................... 366

Creating Master-Detail Forms ........................................................................ 367

Creating a New Binding Context .................................................................... 369

The DataGrid Control ..................................................................................... 370 DataGrid Relations .......................................................................................... 371

DataGrid Column Mapping ............................................................................ 372

Creating Custom DataGrid Column Styles .................................................... 373

Encapsulation with Data Controls ........................................................... 377 Validating Bound Data .................................................................................... 378

Data-Aware Controls ....................................................................................... 380 ADecoupled TreeViewwithJust-in-Time Nodes ......................................... 382

Can There Be a Data-Bound UstView Control? ............................................ 387

The Last Word .................................................................................................... 387

Chapter 10 MDI Interfaces and Workspaces ........................ 389

The Story of MDI .............................................................................................. 389 Types of MDI Applications ............................................................................. 390

MDI Essentials .................................................................................................. 390 Finding Your Relatives .................................................................................... 392 Synchronizing MDI Children .......................................................................... 393

MDI Layout ...................................................................................................... 395

Merging Menus ................................................................................................ 396 Managing Interface State ................................................................................ 399

Document-View Architecture ........................................................................ 401 A Document-View Ordering Program ............................................................ 402

Floating Windows and Docking .................................................................... 415 Floating Toolbars ............................................................................................. 415

Dockable Windows .......................................................................................... 418

The Last Word .................................................................................................... 424

xiii

Page 12: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Contents

Chapter 11 Dynamic User Interface ........................................... 425

The Case for Dynamic User Interface ..................................................... 425

Creating Controls at Runtime .................................................................... 426 A System Tray Application ............................................................................... 428

Using Controls in a Drawing Program ............................................................ 432 Dynamic Content ................................................................................................. 438

Localization ....................................................................................................... 439 A Dynamic Menu Example .............................................................................. 442

Data-Driven Programming ............................................................................... 446 A Data-Driven Pricing Application ................................................................. 446

Control Layout Engines ................................................................................. 448 The SingleLineFlow Layout Manager ............................................................. 449 Control Layout Engines As Extender Providers ............................................. 452

The Last Word ..................................................................................................... 452

Chapter 12 GDI+ Basics ....................................................................... .455

Paint Sessions with GDI+ ............................................................................. 456 Accessing the Graphics Object ........................................................................ 456

Painting and Refreshing ................................................................................... 457

Optimizing GDI+ Painting ............................................................................. 460 Painting and Resizing ....................................................................................... 460

Painting Portions of a Window ........................................................................ 462 Rendering Mode and Antialiasing .................................................................. 465

Double Buffering .............................................................................................. 467 Painting and Debugging .................................................................................. 470

The Graphics Class .......................................................................................... 471 Coordinate Systems and Transformations ..................................................... 476 Pens ................................................................................................................... 479 Brushes .............................................................................................................. 482

Hit Testing ......................................................................................................... 485 Hit Testing Nonrectangular Shapes ................................................................ 487

The Control Paint Class ................................................................................. 488 The Last Word ..................................................................................................... 490

Chapter 13 GDI+ Controls .................................................................. 491

Simple GDI + Controls ...................................................................................... 491 A Gradient Label ............................................................................................... 492

Improving the GradientLabel' s Design-Time Support ................................. 495 A Marquee Label ............................................................................................... 498

xiv

Page 13: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Contents

Creating Button Controls ............................................................................ 501 A Hot Tracking Button ..................................................................................... 502

Reconsidering the Vector Drawing Program ......................................... 507 Solving the Bounding Problem ....................................................................... 508

A Simple Graphing Control .......................................................................... 513 The Last Word .................................................................................................... 517

Chapter 14 Help and Application-Embedded Support .... 519

The Case for Help ............................................................................................ 519 Types of Help .................................................................................................... 520

WinHelp ............................................................................................................ 520 WinHelp 95 ....................................................................................................... 521 HTML Help ....................................................................................................... 522 MS Help 2 ......................................................................................................... 524 Some Help Authoring Tools ............................................................................ 525

Classic Bad Help .............................................................................................. 526 Basic Help with the HelpProvider ........................................................... 527

Control-Based and Form-Based Help ............................................................ 531 Invoking Help Programmatically ............................................................... 532

Help Without the HelpProvider ...................................................................... 533 Using Database-Based Help ........................................................................... 534 UsingTask-BasedHelp ................................................................................... 535 CreatingYourOwnHelp ................................................................................. 537

Application-Embedded Support .................................................................... 539 Affordances ...................................................................................................... 540 Agents ............................................................................................................... 541

The Last Word .................................................................................................... 547

Index ................................................................................................................... 549

XV

Page 14: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Preface

IN THE PAST FEW MONTHS, a deluge of .NET books has hit store shelves, each one eager to explain the new programming philosophy of the .NET world. In the excitement, many of these books have left out the tricks and insights needed to really master .NET programming. Part of the problem is that no single work can cover the entire .NET platform-a sprawling, ambitious framework that revolutionizes everything from Internet applications to data access technology. Many .NET books provide a good overview of essential concepts, but they can't deal with the subtleties needed for all types of development.

This book represents the start of the second wave of .NET books: closely focused works that give you the insight of experienced developers about a single aspect of .NET programming. User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the ingredients you can use to design state-of-the-art application interfaces. It also delves into entirely new topics like custom-control design and GDI+, the next -generation painting framework for Wmdows. You won't just learn about anchoring and docking, you'll work with examples that show document­view architecture, custom -control layout engines, dockable windows, and hit testing with owner-drawn controls. You also learn how to design irregularly shaped forms, unshackle data binding, and build an integrated help system.

In short, this is the sort of .NET book that I would want to read as a professional developer. It's a book that goes beyond the basics and combines user interface design principles with practical guidelines for creating the next generation of software applications. And seeing as you are reading this introduction, you've probably already realized that this next generation will be built using the .NET Framework.

xvii

Page 15: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

About the Author

MATTHEW MAcDONALD is an author, educator, and MCSD developer. He's a regular contributor to programming journals such as Inside Visual Basic and C# Today, and the author of several books about .NET programming, including The Book of VB .NET (No Starch) andASP.NET: The Complete Reference (Osborne/McGraw-Hill). In a dimly remembered past life, he studied English literature and theoretical physics.

xix

Page 16: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

About the Technical Reviewer

GoRDON WILMOT is a director ofiCEnetware Ltd., a company specializing in Internet and network management and monitoring software. He has held positions ranging

from software engineer to systems architect and has been developing software using Microsoft products and architectures for over 20 years. Over this time he has designed and developed many products and systems for various industries such as finance, manufacturing, and telecommunications. All his spare time is eaten up by making cakes (badly) for his three-year-old twins, Charlotte and Georgina, and being beaten continuously by his seven-year-old son, Andrew, on the PS2. When he grows up, he'd still like to be an astronaut.

xxi

Page 17: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Acknowledgments

THIS BOOK HAS BEEN, comparatively speaking, a lot of fun to write. As a result, I have a long list of people to thank.

Gary Cornell never ceases to amaze me with his ability to respond to emails mere seconds after they've departed from my outbox. I'm indebted to him for quickly and painlessly signing me on for this project. I also owe a sincere thanks to a number of other individuals at Apress who helped everything move swiftly and smoothly. They include Sofia Marchant, Kari Brooks, Grace Wong, Stephanie Rodriguez, and doubtless many others I never interacted with directly.

Gordon Wilmot performed the technical review for this book. Besides being quicker than most, he offered helpful feedback, including an invaluable tip about sorting items in the ListView control. Anne Friedman performed the copy editing, and her unerring light touch helped guarantee the final polished product. I owe a heartfelt thanks to both.

Finally, I'd never write any book without the support of my loving wife, her parents, and my parents (who started this whole mess with two gametes at the right place and the right time). Thanks everyone!

xxiii

Page 18: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Introduction

THE .NET REVOLUTION is in full swing, and the confusion that has surrounded it for the last year is finally lifting. Developers are no longer wondering whether .NET is designed for web services, distributed applications, object -oriented development, cross-language interoperability, painless deployment, or a new way to access data. Instead, we now realize that .NET is built for all of that, and much more. With .NET, Microsoft has bundled almost a dozen miniature revolutions into one marketing term, along with a class library stocked with hundreds of pieces of prebuilt functionality.

Unfortunately, you can't come to terms with the amazing breadth of the .NET framework by reading a single book. To become an expert .NET programmer, you need an in-depth exploration of the areas of development that interest you the most. In other words, it's time to forget about the broadly sweeping goals of .NET for a moment and focus on the nuts and bolts of how to design the next -generation of software applications.

This book explains how to program user interfaces applying the tools and tech­niques of the .NET world. You may already be familiar with some of the concepts that carry over from traditional development (like multiple-document interfaces, and the standard Windows controls and conventions). Other features are entirely new and will be unlike anything you have ever worked with before. But no matter what aspect of user interface design you're exploring, it all works through the .NET class library, which provides a new set of capabilities, subtleties, and quirks that every .NET programmer needs to master.

About This Book

User interface design deals with several aspects of programming. The tendency in a book about a topic like this is to pursue one of these themes exclusively. With .NET, however, the programming framework is entirely new. A reference that only explains controls and commands is dangerous, and without a proper discussion of best practices and design tips, programmers are likely to wind up in a great deal of trouble-with applications that are difficult to enhance, debug, or scale up. For that reason, I've made the decision in this book to focus on three distinct themes.

XXV

Page 19: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Introduction

xxvi

What This Book Teaches You

This book fills three roles. It provides the following:

• An overview of how to design elegant user interface the average user can understand. This is addressed directly in the first chapter and indirectly in Tips and Notes throughout the book.

• A comprehensive examination of the user interface controls and classes in .NET. Although this book is not a reference, it contains an exhaustive tour of almost every user interface element you'll want to use, including providers, components, and custom controls.

• A tutorial with best practices and design tips for coding user interface and integrating help. As a developer, you need to know more than how to add a control to a window. You also need to know how to create an entire user interface framework that's scalable, flexible, and reusable.

What This Book Doesn't Teach You

Of course, it's just as important to point out what this book doesn't contain:

• A description of core .NET concepts like namespaces, assemblies, exception handling and types. These fundamentals are an important basis for .NET design, and they are already explained well in several works, including a number of excellent C# and VB .NET books from Apress.

• A primer on object -oriented design. No .NET programmer can progress very far without a solid understanding of classes, interfaces, and other .NET types. In this book, many examples rely on these basics, using objects to encapsulate, organize, and transfer information.

• A reference for Visual Studio .NET. The new integrated design environment provides powerful customization, automation, and productivity features that deserve a book of their own. Though this book describes a few control designers, for the most part it assumes that you already know how to use IDE to create controls and set properties.

• A comparison between .NET and its predecessors (including Visual Basic, C++, and even Java). Examining the evolution of the .NET language is largely a historical interest and while fascinating, it won't help you master modern .NET development any faster. The best approach is to leave your past language allegiances behind.

Page 20: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

If you haven't learned the .NET fundamentals, you will probably still be able to work through this book. You will probably need to do so at a slower pace, and you may also need to refer to the MSDN help files to clear up a few issues along the way. On the other hand, if you have already read another, more general .NET book, you will benefit the most.

NOTE Tllis book targets experienced developers. If you have never programmed with a language like Visual Basic, C++IC#, or java before, tllis isn't the place to start. Instead, starr with an introductory book on object-oriented design or programmingftmdamentals.

Code Samples

It's a good idea to check the online site to download the most recent, up-to-date code samples. You'll need to do this to test most of the more sophisticated code examples described in this book, because the full code listing is often left out. Instead, I focus on the most important conceptual sections so that you don't need to wade through needless extra pages to understand an important concept.

You can download the code directly fromApress at http : //www.apress.com or from my own site at http : //www. prosetech. com.

Variable Naming

It seems that variable naming is about to become another religious issue where there is no clear standard, but developers take heated, uncompromising attitudes. Hungarian notation, which was the preferred standard for C++ and Visual Basic (in a slightly modified form), is showing its age. In the world of .NET, where memory management is handled automatically, it seems a little backward to refer to a variable by its data type, especially when the data type may change without any serious consequences, and the majority of variables are storing references to full­fledged objects.

To complicate matters, Microsoft recommends that objects use simple names for properties and methods, like COM components and controls. This system makes a good deal of sense, as data type considerations are becoming more and more transparent. Visual Studio .NET now takes care of some of the work of spotting the invalid use of data types, and its built-in IntelliSense automatically displays information about the data types used by a method.

Introduction

xxvii

Page 21: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Introduction

xxviii

In this book, data-type prefixes are not used for variables. The only significant exception is with control variables, where it is still a useful trick to distinguish between types of controls (like txtUserName and lstUserCountry), and with some data objects. Of course, when you create your programs you are free to follow whatever variable naming convention you prefer, provided you make the effort to adopt complete consistency across all your projects (and ideally across all the projects in your organization).

Feedback

This book has the ambitious goal of being the best tutorial and reference for .NET user interface design. Toward that end, your comments and suggestions are extremely helpful. You can send complaints, adulation, and everything in between directly to apress@prosetech. com. I can't solve your .NET problems or critique your code, but I will benefit from information about what this book did right and wrong (and what it may have done in an utterly confusing way). You can also send comments about the web site support for this book.

Chapter Overview

The following overview describes what each chapter covers. If you have some .NET experience, feel free to skip from chapter to chapter and read everything in the order you prefer. If, however, you're relatively new to .NET development it's probably easiest to read through the book sequentially, to make sure you learn the basics before encountering more advanced topics.

Chapter 1: Creating Usable Interfaces

User interface design is about more than just knowing how to program the latest trendy interface element-it's also about conventions, consistency, and the best way to guide a user into unfamiliar territory. In this chapter, you learn the basics of interface design theory, and the principles that support every good design.

Chapter 2: Designing with Classes and Tiers

For several years, programming books and articles have advocated a three-layered approach to application design that rigorously separates user interface from appli­cation code. Despite this emphasis, real-world applications rarely follow these best

Page 22: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

practices, and programmers usually discover and rediscover that they are far more time-consuming and awkward than most computer writers promise. In this chapter, you learn how a modern layered design becomes dramatically easier with .NET­and how it might work for you.

Chapter 3: Control Class Basics

This chapter delves into the details of one of .NET's most feature-rich classes: the Control. In this chapter, you learn how the Control class defines the basic features for responding to key presses and mouse movements, defining control relations, and handling Wmdows messages. You also learn about some of the basic System.Drawing ingredients for points, rectangles, colors, and fonts.

Chapter 4: Classic Controls

The classic controls include basic tools for input, selection, and display that have been used since the ancient days of 16-bit Windows programming. This chapter also includes a few .NET twists, like the owner-drawn menus, date controls, and the hyperlink label. It rounds up with demonstrations of control validation and drag-and-drop techniques.

Chapter 5: Forms

The Form class is the basis for every application window in a .NET program. To use forms effectively, you need to understand how forms interact, scroll, and take own­ership of each other. This chapter explains the basics, and considers exciting new techniques like visual inheritance, Windows XP styles, and irregularly shaped forms. It also explains how to make multi-paned, resizable windows that work.

Chapter 6: Modern Controls

This chapter dissects everyone's favorite Windows controls, including Tree View, ListView, ToolBar and StatusBar. As these controls are introduced, you see some innovative ways to extend them with custom classes that provide useful higher­level features or are tailored for a specific type of data.

Introduction

xxix

Page 23: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Introduction

XXX

Chapter 7: Custom Controls

Custom control development is one of the key themes of this book, and a remarkable feature of the .NET platform. This chapter considers the basic types of controls you can create, and introduces examples like a bitmap thumbnail viewer, a progress user control, and a directory tree. It also considers advanced topics like asynchronous control programming, and custom extender providers, which allow you to develop enhancements that can be latched onto any .NET control.

Chapter 8: Design-Time Support for Custom Controls

Creating a custom control is easy, but making it behave well in the design-time environment often takes a little extra wizardry. In this chapter, you see how custom control designers, UITypeEditors, and context-menu verbs can equip your controls for Visual Studio .NET. You also tackle different models of custom control licensing.

Chapter 9: Data Controls

Most applications need to deal with data at some point. This chapter considers how you can integrate data into your user interfaces without creating an interface that's tightly coupled to a specific data access strategy or data source. In other words, you learn how you can create user interface code that doesn't directly refer to field names or assume that data is retrieved all at once. The solutions lead you through an exhaustive look at .NET data binding-and how to extend it-and show how you can create data-aware custom controls.

Chapter 10: MDI Interfaces and Workspaces

MDI interfaces are a hallmark of modern application design .. NET makes MDI as easy as setting a few simple Form properties. This chapter explores some of the best design practices behind MDI development, including synchronization and document-view architecture. It also looks at how you can create floating toolbars and design dockable windows.

Page 24: User Interfaces in VB .NET: Windows Forms and Custom Controls978-1-4302-0844-0/1.pdf · User Interfaces in VB .NET: Windows Forms and Custom Controls takes a close look at all the

Chapter 11: Dynamic User Interface

Dynamic user interface-creating controls at runtime rather than at design-time­isn't a philosophy that can help you break free of some ofVisual Studio .NET's most significant limitations. In this chapter you consider ways to implement dynamic content, show an example of dynamic control creation with a vector-based drawing framework, and consider how to create a custom layout engine.

Chapter 12: GDI+ Basics

GDI+ is .NET's next-generation painting framework. In this chapter, you explore GDI+ from the ground up. You learn how you can draw inside and outside paint event handlers, how to handle form resizing, and how to use basic ingredients like brushes and pens. You also take a look at more advanced topics, including double-buffering to eliminate flicker, hit testing with rectangles and paths, and drawing standard UI elements like focus cues and disabled images.

Chapter 13: GDI+ Controls

This chapter puts the GDI+ platform to work with examples that show a gradient label, marquee text, and a button control from scratch. You develop a bar-graphing control, and take another look at the vector based drawing example from Chapter 11 with GDI+ owner-drawn controls.

Chapter 14: Help and Application-Embedded Support

The final chapter of this book examines how you can weave help into your user interfaces. You'll learn basic techniques for context-sensitive help, techniques to provide task -based help, and tricks that allow you to integrate help windows into your applications. You'll also learn about one of Microsoft's best-kept secrets: the free Microsoft Agent control, which provides an animated character that can guide the user.

Introduction

xxxi