Cover image for Windows forms programming in C#
Title:
Windows forms programming in C#
Personal Author:
Publication Information:
Boston : Addison-Wesley, 2004
ISBN:
9780321116208

Available:*

Library
Item Barcode
Call Number
Material Type
Item Category 1
Status
Searching...
30000010078800 QA76.76.M52 S45 2004 Open Access Book Book
Searching...

On Order

Summary

Summary

• The WinForms team at Microsoft is praising Sells as a definitive authority on the subject > • The content and structure are based on years of experience both building apps with WinForms as well as teaching other developers about WinForms > • Chris Sells is widely revered in the programming community, and has been named by Microsoft as one of eight "Software Legends "


Author Notes

Chris Sells is a content strategist on the Microsoft MSDN content team. Previously, he was the director of software engineering at DevelopMentor


Excerpts

Excerpts

As a fairly public figure in the Windows developer community, I often get asked if I think that .NET is going to "take off." I always answer the same thing; it's not a matter of "if," it's a matter of "when." Microsoft's .NET Framework has so many benefits that even as a grizzled old C++/Win32 guy, I wasn't able to resist the siren song of a managed development environment. It's ironic that the temporary dip in the economy has caused folks to avoid anything new just when .NET comes along to deliver significant reductions in time to market and cost while simultaneously increasing code quality. The organizations that have already adopted .NET know that it's going to have a long and happy life, especially as it gets pushed further and further into Microsoft's own plans for the future of the Windows platform, both on the server and on the client. The primary server-side technology in .NET is ASP.NET, which provides the infrastructure needed to build web sites and web services. ASP.NET provides the reach to deploy web sites to anyone by aiming at the baseline of features provided by the middle generation web browsers. To provide the highest level of functionality possible, ASP.NET does most of the work on the server-side, leaving the client-side HTML as a thin wrapper to trigger server-side requests for new pages of data. The server-side handles practically everything, from data manipulation to user preferences to the rendering of simple things like menus and toolbars. This model provides the greatest availability across operating systems and browsers. If, on the other hand, your targeted customers are Windows users, an HTML-based experience limits the users to a lowest-common denominator approach that is unnecessary. In fact, in an attempt to provide a richer client-side experience, many organizations that know they're targeting Windows users require specific versions of Microsoft's Internet Explorer (IE) web browser. However, as soon as that happens, IE becomes less of a browser and more of an HTML-based application runtime. And for that purpose, the HTML object model is fairly primitive, often requiring a lot of work to do things that are normally simple (like keeping track of a user's session state). If you're going to be targeting Windows users, the .NET Framework provides a much richer set of objects for building interactive user interfaces. This brings me to the subject of this book: Windows Forms (WinForms). WinForms is the face of .NET on the client, providing a forms-based development environment meant to provide the best of the UI object models that have come before it. In addition, it provides one feature that no Windows-based development framework has provided to date - the deployment features of HTML-based web applications. The ability to combine the richness of Windows applications with the deployment of web applications signals a completely new world for Windows developers; one that makes me more than happy to give up the mess of unmanaged code. Audience When writing this book, I had two target audiences in mind. I wanted to provide real-world WinForms coverage for both the programmer that had already programmed in .NET as well as for the programmer that hadn't. Towards that end, I do briefly introduce core .NET topics as they come up. However, the .NET Framework itself is a large area that this book doesn't pretend to cover completely. Instead, when I think more information would be useful to the reader, I reference another work that provides the full details. In particular, I find that I've referenced Essential .NET, by Don Box, with Chris Sells, a great deal, making it a good companion to this book. In this same category, I can also recommend Pragmatic ADO.NET, by Shawn Wildermuth, Advanced .NET Remoting, by Ingo Rammer, .NET Web Services, by Keith Ballinger and Applied Microsoft .NET Framework Programming, by Jeffrey Richter. Two core .NET topics are of special importance to WinForms programmers and I cover them in more detail in Appendix B: Delegates & Events and Appendix C: Serialization Basics. The coverage of delegates and events is particularly important if you're new to .NET, although I don't recommend diving into that topic until you've got a WinForms-specific frame of reference (which is provided about 1/3rd of the way through Chapter 1: Hello, Windows Forms). I'd like to provide one other note for potential readers. Many years ago, I wrote my first five-day training course. The topic was Windows 95 and 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 to take days for both me and for the students. The details of a particular control are only interesting when you're putting that control to use and when that time comes, the control-specific documentation and IntelliSense do a marvelous job of giving you the information you need. Towards that end, this book covers none of the standard controls completely. Instead, as each control is interesting the context of the current topic, like the DataGrid control in Chapter 13: Data Binding & Data Grids, that control is covered appropriately. Also, Chapter 8: Controls and Chapter 9: Design-Time Integration introduces the broad range of categories of controls that WinForms provides, including the category of non-visual controls called components in .NET. Finally, to give you a visual to go with all of the controls and components and to introduce you to the major functionality of each of them, Appendix D: Standard WinForms Components & Controls provides a list of the standard controls and components. I wouldn't think to waste your time by attempting to be more thorough that that reference documentation that comes with the .NET Framework SDK and Visual Studio .NET. Instead, this book focuses on the real-world scenarios that aren't already covered in detail elsewhere. Conventions For those of you that have decided to take the plunge with this book, I'd like to thank you for your faith and express my hope that I live up to in. To aid you in reading the following text, I want to let you in on some conventions I use in my writing. First and foremost, the wonderful thing about WinForms is how visual it is, which is why I use a lot of figures to illustrate its features. Some of those pictures really need to be color to make the point, so be sure to check the color pages at the center of this book for those figures. As useful as figures are, I think primarily in code. Code will be shown in mono-faced type: System.Console.WriteLine("Hello, WinForms."); Console application activation will also be shown in mono-faced type: C:\>csc.exe hello.cs When a part of a code snippet or a command line activation is of particular interest, I mark it in bold and often provide a comment: // Notice the use of the .NET System namespace System. Console.WriteLine("Hello, WinForms."); When I want to direct your attention to a piece of code even more fully, I'll 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"); }} }} Further, to make the printed code more readable, I'll 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, I use their full name: SerializableAttribute class MyCustomType {{...}} Some languages, like C#, allow the "Attribute" suffix to be dropped for convenience, but that makes it hard to find the details of the attribute class in the online documentation. Also, I sometimes take error checking out of the printed code for clarity, but try to leave it in the sample code that comes with this book.In the prose itself, I often put a word or phrase in italics to indicate a new term that I'm about to define. As an example of this kind of term and its definition, hegemony is preponderant influence or authority, as well as a useful business practice. Finally, I often mention keyboard shortcuts because I find them convenient. The ones I mention are the default Visual Studio Developer key bindings. If you're not using those key bindings, you'll need to map the keyboard shortcuts to your own settings. Contact The up-to-date information for this book, including the source code and the errata, are maintained at http://www.sellsbrothers.com/ . This site also provides a way for you to send feedback to me about the book, both complimentary and less so. 0321116208P05022003 Excerpted from Windows Forms Programming in C# by Chris Sells All rights reserved by the original copyright owners. Excerpts are provided for display purposes only and may not be reproduced, reprinted or distributed without the written permission of the publisher.

Table of Contents

Figuresp. xix
Tablesp. xxxi
Forewordp. xxxiii
Prefacep. xxxvii
1 Hello, Windows Formsp. 1
WinForms from Scratchp. 1
Windows Forms in Visual Studio .NETp. 8
Arranging Controlsp. 13
Controlsp. 16
Application Settingsp. 18
Resourcesp. 22
Dialogsp. 24
Drawing and Printingp. 26
Data Bindingp. 29
Multithreaded User Interfacesp. 31
Deploymentp. 31
Moving from MFCp. 33
Where Are We?p. 33
2 Formsp. 35
Showing Formsp. 35
Form Lifetimep. 38
Form Size and Locationp. 42
Form Adornmentsp. 49
Form Transparencyp. 50
Form Menusp. 55
Child Controlsp. 59
Layoutp. 65
Multiple Document Interfacep. 77
Visual Inheritancep. 82
Where Are We?p. 86
3 Dialogsp. 87
Standard Dialogsp. 88
Stylesp. 89
Data Exchangep. 92
Data Validationp. 98
Implementing Helpp. 105
Where Are We?p. 116
4 Drawing Basicsp. 117
Drawing to the Screenp. 118
Colorsp. 122
Brushesp. 127
Pensp. 135
Shapesp. 142
Pathsp. 146
Imagesp. 150
Where Are We?p. 166
5 Drawing Textp. 167
Fontsp. 167
Stringsp. 177
Where Are We?p. 187
6 Advanced Drawingp. 189
Page Unitsp. 189
Transformsp. 193
Regionsp. 204
Optimized Drawingp. 207
Where Are We?p. 212
7 Printingp. 213
Print Documentsp. 213
Print Controllersp. 215
Basic Print Eventsp. 219
Marginsp. 222
Page Settingsp. 226
Printer Settingsp. 229
Where Are We?p. 235
8 Controlsp. 237
Standard Controlsp. 237
Custom Controlsp. 254
User Controlsp. 277
Drag and Dropp. 279
Where Are We?p. 288
9 Design-Time Integrationp. 289
Componentsp. 289
Design-Time Integration Basicsp. 300
Extender Property Providersp. 319
Type Convertersp. 324
UI Type Editorsp. 337
Custom Designersp. 346
Where Are We?p. 355
10 Resourcesp. 357
Resource Basicsp. 357
Resource Localizationp. 373
Where Are We?p. 385
11 Applications and Settingsp. 387
Applicationsp. 387
Environmentp. 406
Settingsp. 409
Where Are We?p. 433
12 Data Sets and Designer Supportp. 435
Data Setsp. 436
Designer Supportp. 453
Typed Data Setsp. 456
Where Are We?p. 466
13 Data Binding and Data Gridsp. 467
Data Bindingp. 467
Data Gridsp. 497
Custom Data Sourcesp. 503
Where Are We?p. 516
14 Multithreaded User Interfacesp. 519
Long-Running Operationsp. 519
Asynchronous Web Servicesp. 541
Where Are We?p. 545
15 Web Deploymentp. 547
Hosting Controls in Internet Explorerp. 547
Code Access Securityp. 552
No-Touch Deploymentp. 556
Partially Trusted Assembly Considerationsp. 565
Increasing Permissionsp. 578
Authenticodep. 587
Where Are We?p. 589
A Moving from MFCp. 591
A Few Words About MFCp. 591
MFC Versus WinFormsp. 594
Genghisp. 604
B Delegates and Eventsp. 607
Delegatesp. 607
Eventsp. 612
Happiness in the Universep. 615
C Serialization Basicsp. 619
Streamsp. 619
Formattersp. 622
ISerializablep. 626
Data Versioningp. 628
D Standard WinForms Components and Controlsp. 631
Components and Controls Definedp. 633
Standard Componentsp. 634
Standard Controlsp. 642
Bibliographyp. 659
Indexp. 663