Windows Forms 2.0 Programming

Paperback | May 16, 2006

byChris Sells, Michael Weinhardt

not yet rated|write a review
Preface Windows 1.0 programmers had a straightforward life. They had almost no choices about how to do things; either there was an application programming interface API , or there wasn't, and most of the time there wasn't. This meant that developers had to build almost everything by hand. At the time, this wasn't a problem. All Windows programmers had the same limitations, so everyone's apps more or less operated with the same limited set of functionality. A modern Windows developer, on the other hand, is inundated with choices. The invention of the web alone gives us static Hypertext Markup Language HTML , server-based user interface UI interaction via ASP.NET, and client-side UI interaction via ActiveX controls or AJAX to name a few .1On the client side, we have native frameworks like Microsoft Foundation Classes MFC , Active Template Library ATL , and the next-generation managed framework known as the Windows Presentation Foundation WPF is part of WinFX, the follow-on to the .NET Framework . How does a developer choose? Where does Windows Forms 2.0, the topic of this book, fit into this pantheon? 1 AJAX stands for Asynchronous JavaScript and XML, as defined by Jesse James Garrett, Ajax: A New Approach to Web Applications, [a href="http://www.adaptivepath.com/publications/essays/archives/000385.php" target=_new>http://www.adaptivepath.com/publications/essays/archives/000385.phphttp://tinysells.com/38 .Client technologies on Windows provide a range of reach and richness in inverse proportion to each other. Reach is the ability of a user interface UI to work across operating systems, whereas richness is the ability for a UI to take advantage of the operating system on which it runs. So even though static HTML works almost everywhere, to the extent that your HTML uses client-side JScript, reach is diminished for browsers that don't support the scripting constructs being used. Even further down the reach scale are ActiveX controls, which work only on Windows but allow full access to its richness.2
2 At one point, the Component Object Model COM and ActiveX were made to work across platforms, but they have long since been relegated to Windows only.
At the other extreme we have WPF, a completely new managed framework built on top of DirectX. WPF encompasses documents, layout, animation, 2-D, 3-D, text, and graphics primitives in a single, cohesive whole. This framework provides the richest client technology that Microsoft has ever built, and it takes maximum advantage of the platform on which it runs, including any hardware 3-D graphics acceleration that may be available. However, at the time of this writing, WPF has not yet shipped and works only under Windows XP, Windows Server 2003, and Windows Vista.3
3 WinFX which inclues WPF and Windows Vista are scheduled to ship in 2006.
Where does that leave Windows Forms 2.0? It doesn't quite offer the richness of WPF, but it's far richer than the HTML-based technologies. Windows Forms 2.0 also is much richer than Windows Forms 1.0, with greatly enhanced support for flexible layout, modern menu strip and tool strip controls, data binding, multithreading, typed resources and settings, and, most importantly, ClickOnce deployment. With ClickOnce, for the first time, users can deploy Windows applications as easily as web applications. Even better, ClickOnce allows Windows applications to be installed locally, integrated with the Start menu, and accessed even if no network connection is available. These options are not available for users of Web applications. So Windows Forms 2.0 is high on the richness scale, but what about the reach scale? Unlike web applications, Windows Forms requires that users' machines operate on Windows, specifically Windows 98 or later for PCs. However, it also supports other devices, such as Pocket PCs or Smartphones, via the .NET Compact Framework. This means that you can write a Windows Forms 2.0 application and run it on your Windows smartphone. Based on this level of richness and reach, what kinds of applications should you build with Windows Forms? As it turns out, folks have used Windows Forms to build almost every kind of application, including productivity applications, utilities, enterprise tools, and more-even games. Windows Forms can be used to build smaller things, such as Microsoft's own internal human resources application called HeadTrax , or larger things, such as most of the UI enhancements that have been built on top of Visual Studio since the 2002 edition. This range of applications can be built because of Windows Forms' range of features and tools, its reach, and, as of this version, the maturity of the implementation based on community usage and feedback. In fact, the .NET developer community has embraced Windows Forms to a degree that defies the early .NET focus on web-based applications. Based on talking to my friends in the training industry and on the sales of the first edition of this book, I conclude that a large and growing number of developers write Windows Forms applications. It's for those developers that we've written this book. Who Should Read This Book? When writing this book, we wanted to provide real-world Windows Forms coverage for programmers who've programmed in .NET-and those who haven't. To that end, we briefly introduce core .NET topics as they come up. However, we don't pretend to cover the .NET Framework completely. It's too large a topic. Instead, when we think more information would be useful, we reference another work that provides the full details. In particular, you'll find that we've referenced Essential .NET, by Don Box, with Chris Sells, a great deal, making it a good companion to this book. In the same category, we also recommend Advanced .NET Remoting, by Ingo Rammer, and Applied Microsoft .NET Framework Programming, by Jeffrey Richter. For more details on these books, see the Bibliography. One core .NET topic is of special importance to Windows Forms programmers, and we cover it in more detail in Appendix C: Delegates and Events. This coverage is particularly important if you're new to .NET, although we don't recommend diving into that topic until you've got a Windows Forms-specific frame of reference which is provided about one-third of the way through Chapter 1: Hello, Windows Forms . Also of particular importance to former or soon-to-be former MFC programmers, we've provided Appendix B: Moving from MFC, and Appendix F: Document Management. Although Windows Forms doesn't provide all the features of MFC, we've worked hard on the material and code samples in this book to plug most of the gaps. One other note: Many years ago, one of the authors wrote his first five-day training course. The topic, Windows 95, included a few hours of coverage on the new controls: what they looked like, what their properties, methods, and events were, and how to program against them. Those hours seemed like days for everyone involved. The details of a particular control are interesting only when you're putting it to use, and when that time comes, the control-specific documentation and Visual Studio's IntelliSense help support do a marvelous job of giving you the information you need. To that end, this book covers none of the standard controls completely. Instead, when a control is interesting in the context of the current topic-such as the DataGridView control in Chapter 17: Applied Data Binding-we cover that control appropriately. Also, to give you a list of all the components and controls and to introduce you to each one's major functionality, Appendix D: Component and Control Survey provides a list of the standard controls and components. We wouldn't think of wasting your time by attempting to be more thorough than the reference documentation that comes with the .NET Framework software development kit SDK and Visual Studio 2005. Instead, this book focuses on the real-world scenarios that aren't covered in detail elsewhere. Finally, to help you understand the differences between components and controls and how to build both, we've provided Chapter 9: Components and Chapter 10: Controls. Conventions If you've decided to take the plunge with this book, we'd like to thank you for your faith and express our hope that we live up to it. To aid you in reading the text, we want to let you in on some conventions we use. First, there is the mode of address that we use throughout the book except for this preface . Because Chris and Michael worked so closely and so hard on every word in this book, the use of I really means both authors, whereas the use of we means the authors and you. Although we sometimes use you directly, Michael and Chris lean toward the inclusive. The wonderful thing about Windows Forms is how visual it is, and that's why we use a lot of figures to illustrate its features. Some of those pictures really need to be in color to make the point, so be sure to check the color pages at the center of this book for those color plates. As useful as figures are, we both think primarily in code. Code is shown in monospace type: System.Console.WriteLine "Hello, Windows Forms." ; re>Console application activation is also shown in monospace type: C:\> csc.exe hello.cs re>When a part of a code snippet or a command line activation is of particular interest, we mark it in bold and often provide a comment: // Notice the use of the .NET System namespace System. Console.WriteLine "Hello, Windows Forms." ; re>When we want to direct your attention to a piece of code more fully, we replace superfluous code with ellipses: class MyForm : System.Windows.Forms.Form { ... // fields private void MyForm_Load object sender, System.ComponentModel.EventArgs e { MessageBox.Show "Hello from MyForm" ; }} re>Furthermore, to make the printed code more readable, we often drop namespaces and protection keywords when they don't provide additional information: // Shortened "System.Windows.Forms.Form" base class class MyForm : Form { ... // fields // Removed "private" specifier and "System.ComponentModel" namespace void MyForm_Load object sender, EventArgs e { MessageBox.Show "Hello from MyForm" ; }} re>When showing .NET attributes, we use their abbreviated name: Serializable // Instead of SerializableAttributeclass MyCustomType {...} re>C# lets you drop the Attribute suffix for convenience, but remember to append this suffix when you look up the details of the attribute class in the online documentation. For clarity, we sometimes omit error checking from the printed code, but we try to leave it in the sample code that you'll find at our web site. In the text, we often put a word or phrase in italics to indicate a new term that we're about to define. As an example of this kind of term and its definition, hegemonyis a preponderant influence or authority, as well as a potent business practice. Sometimes, we mention keyboard shortcuts because we find them convenient. The ones I mention are the default Visual Studio 2005 VS05 key bindings for C# programmers. If you're not using those key bindings, you'll need to map the keyboard shortcuts to your own settings. Finally, because this is the second edition of this book, we've taken care to mark new and updated types in the margins with graphics and in the code with comments. Also, for a summary of what's new in Windows Forms, check out Appendix A: What's New in Windows Forms 2.0.

Pricing and Purchase Info

$64.18 online
$72.99 list price (save 12%)
Ships within 1-2 weeks
Ships free on orders over $25

From the Publisher

Preface Windows 1.0 programmers had a straightforward life. They had almost no choices about how to do things; either there was an application programming interface API , or there wasn't, and most of the time there wasn't. This meant that developers had to build almost everything by hand. At the time, this wasn't a problem. All Windows...

From the Jacket

“Some books are different-the content can't be Googled from the Internet, the authors have real-life experiences to share, the code can be used in real-world projects. This is one of those books.” –Neal Myrddin, Independent Developer   “Chris and Michael nail the soul of Windows Forms 2.0. This book provides an insightful view i...

Chris Sells is a program manager for the Connected Systems Division. He’s written several books, including Programming Windows Presentation Foundation (O’Reilly), Windows Forms Programming in C# (Addison-Wesley), and ATL Internals (Addison-Wesley). In his free time, Chris hosts various conferences and makes a pest of himself on Micros...

other books by Chris Sells

Programming WPF: Building Windows UI with Windows Presentation Foundation
Programming WPF: Building Windows UI with Windows Prese...

Kobo ebook|Dec 17 2008

$34.69 online$44.99list price(save 22%)
Building Windows 8 Apps with JavaScript
Building Windows 8 Apps with JavaScript

Kobo ebook|Dec 27 2012

$40.49 online$52.48list price(save 22%)
ATL Internals: Working with ATL 8
ATL Internals: Working with ATL 8

Kobo ebook|Jul 5 2006

$72.17

see all books by Chris Sells
Format:PaperbackDimensions:1296 pages, 9 × 7 × 2 inPublished:May 16, 2006Publisher:Pearson EducationLanguage:English

The following ISBNs are associated with this title:

ISBN - 10:0321267966

ISBN - 13:9780321267962

Customer Reviews of Windows Forms 2.0 Programming

Reviews

Extra Content

Read from the Book

Windows 1.0 programmers had a straightforward life. They had almost no choices about how to do things; either there was an application programming interface (API), or there wasn’t, and most of the time there wasn’t. This meant that developers had to build almost everything by hand. At the time, this wasn’t a problem. All Windows programmers had the same limitations, so everyone’s apps more or less operated with the same limited set of functionality. A modern Windows developer, on the other hand, is inundated with choices. The invention of the web alone gives us static Hypertext Markup Language (HTML), server-based user interface (UI) interaction via ASP.NET, and client-side UI interaction via ActiveX controls or AJAX (to name a few).1 On the client side, we have native frameworks like Microsoft Foundation Classes (MFC), Active Template Library (ATL), and the next-generation managed framework known as the Windows Presentation Foundation (WPF is part of WinFX, the follow-on to the .NET Framework). How does a developer choose? Where does Windows Forms 2.0, the topic of this book, fit into this pantheon? 1 AJAX stands for Asynchronous JavaScript and XML, as defined by Jesse James Garrett, “Ajax: A New Approach to Web Applications,” http://www.adaptivepath.com/publications/essays/archives/000385.php (http://tinysells.com/38). Client technologies on Windows provide a range of reach and richness in inverse proportion to each other. Reach is the ability of a user interface (UI) to work across operating systems, whereas richness is the ability for a UI to take advantage of the operating system on which it runs. So even though static HTML works almost everywhere, to the extent that your HTML uses client-side JScript, reach is diminished for browsers that don’t support the scripting constructs being used. Even further down the reach scale are ActiveX controls, which work only on Windows but allow full access to its richness.2 2 At one point, the Component Object Model (COM) and ActiveX were made to work across platforms, but they have long since been relegated to Windows only. At the other extreme we have WPF, a completely new managed framework built on top of DirectX. WPF encompasses documents, layout, animation, 2-D, 3-D, text, and graphics primitives in a single, cohesive whole. This framework provides the richest client technology that Microsoft has ever built, and it takes maximum advantage of the platform on which it runs, including any hardware 3-D graphics acceleration that may be available. However, at the time of this writing, WPF has not yet shipped and works only under Windows XP, Windows Server 2003, and Windows Vista.3 3 WinFX (which inclues WPF) and Windows Vista are scheduled to ship in 2006. Where does that leave Windows Forms 2.0? It doesn’t quite offer the richness of WPF, but it’s far richer than the HTML-based technologies. Windows Forms 2.0 also is much richer than Windows Forms 1.0, with greatly enhanced support for flexible layout, modern menu strip and tool strip controls, data binding, multithreading, typed resources and settings, and, most importantly, ClickOnce deployment. With ClickOnce, for the first time, users can deploy Windows applications as easily as web applications. Even better, ClickOnce allows Windows applications to be installed locally, integrated with the Start menu, and accessed even if no network connection is available. These options are not available for users of Web applications. So Windows Forms 2.0 is high on the richness scale, but what about the reach scale? Unlike web applications, Windows Forms requires that users’ machines operate on Windows, specifically Windows 98 or later for PCs. However, it also supports other devices, such as Pocket PCs or Smartphones, via the .NET Compact Framework. This means that you can write a Windows Forms 2.0 application and run it on your Windows smartphone. Based on this level of richness and reach, what kinds of applications should you build with Windows Forms? As it turns out, folks have used Windows Forms to build almost every kind of application, including productivity applications, utilities, enterprise tools, and more—even games. Windows Forms can be used to build smaller things, such as Microsoft’s own internal human resources application (called HeadTrax), or larger things, such as most of the UI enhancements that have been built on top of Visual Studio since the 2002 edition. This range of applications can be built because of Windows Forms’ range of features and tools, its reach, and, as of this version, the maturity of the implementation based on community usage and feedback. In fact, the .NET developer community has embraced Windows Forms to a degree that defies the early .NET focus on web-based applications. Based on talking to my friends in the training industry and on the sales of the first edition of this book, I conclude that a large and growing number of developers write Windows Forms applications. It’s for those developers that we’ve written this book. Who Should Read This Book? When writing this book, we wanted to provide real-world Windows Forms coverage for programmers who’ve programmed in .NET—and those who haven’t. To that end, we briefly introduce core .NET topics as they come up. However, we don’t pretend to cover the .NET Framework completely. It’s too large a topic. Instead, when we think more information would be useful, we reference another work that provides the full details. In particular, you’ll find that we’ve referenced Essential .NET, by Don Box, with Chris Sells, a great deal, making it a good companion to this book. In the same category, we also recommend Advanced .NET Remoting, by Ingo Rammer, and Applied Microsoft .NET Framework Programming, by Jeffrey Richter. (For more details on these books, see the Bibliography.) One core .NET topic is of special importance to Windows Forms programmers, and we cover it in more detail in Appendix C: Delegates and Events. This coverage is particularly important if you’re new to .NET, although we don’t recommend diving into that topic until you’ve got a Windows Forms-specific frame of reference (which is provided about one-third of the way through Chapter 1: Hello, Windows Forms). Also of particular importance to former (or soon-to-be former) MFC programmers, we’ve provided Appendix B: Moving from MFC, and Appendix F: Document Management. Although Windows Forms doesn’t provide all the features of MFC, we’ve worked hard on the material and code samples in this book to plug most of the gaps. One other note: Many years ago, one of the authors wrote his first five-day training course. The topic, Windows 95, included a few hours of coverage on the new controls: what they looked like, what their properties, methods, and events were, and how to program against them. Those hours seemed like days for everyone involved. The details of a particular control are interesting only when you’re putting it to use, and when that time comes, the control-specific documentation and Visual Studio’s IntelliSense help support do a marvelous job of giving you the information you need. To that end, this book covers none of the standard controls completely. Instead, when a control is interesting in the context of the current topic—such as the DataGridView control in Chapter 17: Applied Data Binding—we cover that control appropriately. Also, to give you a list of all the components and controls and to introduce you to each one’s major functionality, Appendix D: Component and Control Survey provides a list of the standard controls and components. We wouldn’t think of wasting your time by attempting to be more thorough than the reference documentation that comes with the .NET Framework software development kit (SDK) and Visual Studio 2005. Instead, this book focuses on the real-world scenarios that aren’t covered in detail elsewhere. Finally, to help you understand the differences between components and controls and how to build both, we’ve provided Chapter 9: Components and Chapter 10: Controls. Conventions If you’ve decided to take the plunge with this book, we’d like to thank you for your faith and express our hope that we live up to it. To aid you in reading the text, we want to let you in on some conventions we use. First, there is the mode of address that we use throughout the book (except for this preface). Because Chris and Michael worked so closely and so hard on every word in this book, the use of “I” really means “both authors,” whereas the use of “we” means “the authors and you.” Although we sometimes use “you” directly, Michael and Chris lean toward the inclusive. The wonderful thing about Windows Forms is how visual it is, and that’s why we use a lot of figures to illustrate its features. Some of those pictures really need to be in color to make the point, so be sure to check the color pages at the center of this book for those color plates. As useful as figures are, we both think primarily in code. Code is shown in monospace type: System.Console.WriteLine("Hello, Windows Forms."); Console application activation is also shown in monospace type: C:\> csc.exe hello.cs When a part of a code snippet or a command line activation is of particular interest, we mark it in bold and often provide a comment: // Notice the use of the .NET System namespace System.Console.WriteLine("Hello, Windows Forms."); When we want to direct your attention to a piece of code more fully, we replace superfluous code with ellipses: class MyForm : System.Windows.Forms.Form { ... // fields private void MyForm_Load( object sender, System.ComponentModel.EventArgs e) { MessageBox.Show("Hello from MyForm"); }} Furthermore, to make the printed code more readable, we often drop namespaces and protection keywords when they don’t provide additional information: // Shortened "System.Windows.Forms.Form" base class class MyForm : Form { ... // fields // Removed "private" specifier and "System.ComponentModel" namespace void MyForm_Load(object sender, EventArgs e) { MessageBox.Show("Hello from MyForm"); }} When showing .NET attributes, we use their abbreviated name: Serializable // Instead of SerializableAttributeclass MyCustomType {...} C# lets you drop the “Attribute” suffix for convenience, but remember to append this suffix when you look up the details of the attribute class in the online documentation. For clarity, we sometimes omit error checking from the printed code, but we try to leave it in the sample code that you’ll find at our web site. In the text, we often put a word or phrase in italics to indicate a new term that we’re about to define. As an example of this kind of term and its definition, hegemony is a preponderant influence or authority, as well as a potent business practice. Sometimes, we mention keyboard shortcuts because we find them convenient. The ones I mention are the default Visual Studio 2005 (VS05) key bindings for C# programmers. If you’re not using those key bindings, you’ll need to map the keyboard shortcuts to your own settings. Finally, because this is the second edition of this book, we’ve taken care to mark new and updated types in the margins with graphics and in the code with comments. Also, for a summary of what’s new in Windows Forms, check out Appendix A: What’s New in Windows Forms 2.0.

Table of Contents

                        About the Authors  xxv

                        Foreword  xxvii

                        Preface  xxxi

Chapter 1     Hello, Windows Forms  1

Chapter 2     Forms  41

Chapter 3     Dialogs  99

Chapter 4     Layout  137

Chapter 5     Drawing Basics  179

Chapter 6     Drawing Text  231

Chapter 7     Advanced Drawing  265

Chapter 8     Printing  291

Chapter 9     Components  327

Chapter 10   Controls  353

Chapter 11   Design-Time Integration: The Properties Window  399

Chapter 12   Design-Time Integration: Designers and Smart Tags  453

Chapter 13   Resources  505

Chapter 14   Applications  549

Chapter 15   Settings  579

Chapter 16   Data Binding Basics  629

Chapter 17   Applied Data Binding  681

Chapter 18   Multithreaded User Interfaces  737

Chapter 19   ClickOnce Deployment  775

Appendix A    What’s New in Windows Forms 2.0  829

Appendix C    Delegates and Events  867

Appendix D    Component and Control Survey  881

Appendix E    Drag and Drop  901

Appendix F    Document Management  927

                        Bibliography  957

                        Index  961