Cover image for Programming .NET Windows Applications

Book description

For those of you who develop standalone Windows applications for PCs and other devices, Microsoft's .NET Windows Forms provide a much better way to get it done. This new technology gives you more power and flexibility for a fraction of the effort compared to classic Win32 development, with a streamlined programming model that deals automatically with many tedious details that once plagued developers. As with most things .NET, the only hitch is the learning curve. But that's where acclaimed author Jesse Liberty makes the difference with Programming .NET Windows Applications. With this tutorial, you will explore all aspects of using .NET Windows Forms class libraries and the associated programming tools in Visual Studio .NET, enabling you to build applications for the Windows 9x, Windows 2000 and Windows XP desktop platforms. Step-by-step, you'll learn ways to design applications that either function alone on a PC, or work in combination with your web-based application server to take advantage of the richer interface and higher level of security. The book also explains how your new Windows applications can sidestep problems that used to arise from the use of DLLs (known collectively as "DLL hell"), and how .NET Windows Forms can be used as an alternative to ASP.NET and browser-based approaches for building web application clients. Jesse Liberty definitely knows his stuff when it comes to the .NET platform. As the author of O'Reilly's Programming C# and Learning Visual Basic .NET, he's well-known for his clear and concise style that prompted one reviewer to say, "It's as if he knows exactly what questions I'm going to ask ahead of time." Jesse also co-authored Programming ASP.NET with contract programmer Dan Hurwitz, and now the two have teamed up again to bring you this comprehensive tutorial--without a doubt, the best source available for learning how to program with .NET Windows Forms.

Table of Contents

  1. Programming .NET Windows Applications
  2. A Note Regarding Supplemental Files
  3. Copyright
  4. Preface
    1. About This Book
    2. How the Book Is Organized
    3. Who This Book Is for
    4. Conventions Used in This Book
    5. Version Support
    6. Support: A Note From Jesse Liberty
    7. We'd Like to Hear from You
    8. Acknowledgments
  5. 1. Windows Forms and the .NET Framework
    1. 1.1. The .NET Framework
    2. 1.2. Windows Forms
      1. 1.2.1. Languages: C# and VB.NET
      2. 1.2.2. Visual Studio .NET
  6. 2. Getting Started
    1. 2.1. System Requirements
    2. 2.2. Hello World
      1. 2.2.1. Using a Text Editor
        1. 2.2.1.1. Hello World as a console application
        2. 2.2.1.2. Compiling the program
        3. 2.2.1.3. Hello World as a Windows application
        4. 2.2.1.4. Hello World Windows application with a button
      2. 2.2.2. Using Visual Studio .NET
        1. 2.2.2.1. Hello World as a console application
        2. 2.2.2.2. Hello World as a Windows application
        3. 2.2.2.3. Hello World Windows application with a button
  7. 3. Visual Studio .NET
    1. 3.1. Overview
    2. 3.2. Start Page
    3. 3.3. Projects and Solutions
      1. 3.3.1. Solutions
      2. 3.3.2. Projects
        1. 3.3.2.1. Templates
        2. 3.3.2.2. Project names
    4. 3.4. The Integrated Development Environment (IDE)
      1. 3.4.1. Layout
      2. 3.4.2. Menus and Toolbars
        1. 3.4.2.1. File menu
          1. New...
          2. Open...
          3. Add New Item...(Ctrl+Shift+A)
          4. Add Existing Item...(Shift+Alt+A)
          5. Add Project
          6. Open Solution
          7. Close Solution
          8. Advanced Save Options...
          9. Source Control
        2. 3.4.2.2. Edit menu
          1. Cycle Clipboard Ring (Ctrl+Shift+V)
          2. Find and Replace → Find in Files (Ctrl+Shift+F)
          3. Find and Replace → Replace in Files (Ctrl+Shift+H)
          4. Find and Replace → Find Symbol (Alt+F12)
          5. Go To...
          6. Insert File As Text...
          7. Advanced
          8. Incremental search (Ctrl+I)
          9. Bookmarks
          10. Outlining
          11. IntelliSense
        3. 3.4.2.3. View menu
          1. Open/Open With...
          2. Solution Explorer (Ctrl+Alt+L)
          3. Properties Windows (F4)
          4. Server Explorer (Ctrl+Alt+S)
          5. Class View (Ctrl+Shift+C)
          6. Other Windows
        4. 3.4.2.4. Project menu
          1. Add... Menu Items
          2. Exclude From Project
          3. Add Reference...
          4. Add Web Reference...
          5. Set as StartUp Project
          6. Project Dependencies... / Project Build Order...
        5. 3.4.2.5. Build menu
        6. 3.4.2.6. Debug menu
        7. 3.4.2.7. Data menu
        8. 3.4.2.8. Format menu
        9. 3.4.2.9. Tools menu
          1. Connect to Device...
          2. Connect to Database...
          3. Connect to Server...
          4. Add/Remove Toolbox Items...
          5. Build Comment Web Pages...
          6. Macros
          7. External Tools...
          8. Customize...
          9. Options...
        10. 3.4.2.10. Window menu
        11. 3.4.2.11. Help menu
          1. Dynamic Help (Ctrl+F1)
          2. Contents... (Ctrl+Alt+F1)/Index... (Ctrl+Alt+F2)/Search... (Ctrl+Alt+F3)
          3. Index Results... (Shift+Alt+F2)
          4. Search Results... (Shift+Alt+F3)
          5. Edit Filters...
          6. Check for Updates
    5. 3.5. Building and Running
  8. 4. Events
    1. 4.1. Publish and Subscribe
      1. 4.1.1. Events and Delegates
      2. 4.1.2. Event Arguments
      3. 4.1.3. Control Events
      4. 4.1.4. Implementing an Event
        1. 4.1.4.1. In C#
          1. Double-click the control
          2. Use the lightning bolt icon in the Properties window
        2. 4.1.4.2. In VB.NET
          1. Double-click the control
          2. Use the drop-down lists at the top of the code window
          3. Dynamic event implementation
    2. 4.2. Performance
    3. 4.3. Some Examples
      1. 4.3.1. Keyboard Events
      2. 4.3.2. TextBox Validation
  9. 5. Windows Forms
    1. 5.1. Web Applications Versus Windows Applications
    2. 5.2. The Forms Namespace
      1. 5.2.1. The Control Class
        1. 5.2.1.1. Ambient properties
        2. 5.2.1.2. The Control inheritance hierarchy
      2. 5.2.2. The Form Class
      3. 5.2.3. Component Classes
      4. 5.2.4. CommonDialog Classes
    3. 5.3. Form Properties
    4. 5.4. Forms Inheritance
      1. 5.4.1. Programmatic Inheritance
      2. 5.4.2. Visual Inheritance
    5. 5.5. User Interface Design
      1. 5.5.1. UI Principles
      2. 5.5.2. Interface Types
        1. 5.5.2.1. SDI
        2. 5.5.2.2. MDI
        3. 5.5.2.3. Tabbed interface
        4. 5.5.2.4. Explorer style
      3. 5.5.3. Input
        1. 5.5.3.1. Mouse input
        2. 5.5.3.2. Keyboard input
        3. 5.5.3.3. Keyboard shortcuts
  10. 6. Dialog Boxes
    1. 6.1. Modal Versus Modeless
    2. 6.2. Form Properties
    3. 6.3. DialogResult
    4. 6.4. Termination Buttons
    5. 6.5. Apply Button
    6. 6.6. CommonDialog Classes
      1. 6.6.1. FileDialog
        1. 6.6.1.1. OpenFileDialog
        2. 6.6.1.2. SaveFileDialog
      2. 6.6.2. ColorDialog
      3. 6.6.3. FontDialog
      4. 6.6.4. PageSetupDialog
      5. 6.6.5. PrintDialog
      6. 6.6.6. Print Preview Dialog
  11. 7. Controls: The Base Class
    1. 7.1. Control Class
      1. 7.1.1. Class Hierarchy
      2. 7.1.2. Common Features
        1. 7.1.2.1. Parent/child relationship
        2. 7.1.2.2. Z-order
        3. 7.1.2.3. Changing the z-order
        4. 7.1.2.4. Ambient properties
        5. 7.1.2.5. Font
        6. 7.1.2.6. Size and location
        7. 7.1.2.7. Dynamically setting size and location
          1. Dynamically controlling size
          2. Dynamically controlling location
        8. 7.1.2.8. AutoScale
        9. 7.1.2.9. Anchoring
        10. 7.1.2.10. Docking
        11. 7.1.2.11. Painting the control
        12. 7.1.2.12. Tag property
        13. 7.1.2.13. Tabbing
        14. 7.1.2.14. Keyboard interaction
          1. Input focus
          2. Navigation
        15. 7.1.2.15. ImageLists
  12. 8. Mouse Interaction
    1. 8.1. SystemInformation Properties
    2. 8.2. Mouse Events
      1. 8.2.1. Mouse Properties
  13. 9. Text and Fonts
    1. 9.1. Text
    2. 9.2. Fonts
      1. 9.2.1. Typographical Stuff
      2. 9.2.2. Font Class
        1. 9.2.2.1. Font properties
        2. 9.2.2.2. Constructors
          1. 9.2.2.2.1. Constructor based on existing font and FontStyles
          2. 9.2.2.2.2. Constructor based on FontFamily and size
          3. 9.2.2.2.3. Constructor based on FontFamily, size, and FontStyles
          4. 9.2.2.2.4. Constructor based on FontFamily, size, and GraphicsUnit
          5. 9.2.2.2.5. Constructor based on FontFamily, size, FontStyles, and GraphicsUnit
        3. 9.2.2.3. Line spacing
      3. 9.2.3. Drawing Strings
        1. 9.2.3.1. DrawString( )
        2. 9.2.3.2. Brush and brushes
        3. 9.2.3.3. Formatting and alignment
      4. 9.2.4. Measuring Strings
  14. 10. Drawing and GDI+
    1. 10.1. The Drawing Namespace
      1. 10.1.1. The Graphics Class
        1. 10.1.1.1. Coordinates
        2. 10.1.1.2. Transforms introduced
      2. 10.1.2. Graphics Methods
      3. 10.1.3. Obtaining a Graphics Object
      4. 10.1.4. Color Structure
      5. 10.1.5. Geometric Structures—Points, Rectangles, and Sizes
      6. 10.1.6. Brush and Brushes
      7. 10.1.7. Pen and Pens
      8. 10.1.8. Paint Event
        1. 10.1.8.1. Overriding the OnPaint method
        2. 10.1.8.2. Forcing a paint event—the Invalidate method
    2. 10.2. The Analog Clock Project
      1. 10.2.1. Drawing the Clock Face
        1. 10.2.1.1. Color
        2. 10.2.1.2. OnPaint
        3. 10.2.1.3. Transforming the coordinates
        4. 10.2.1.4. World transforms
      2. 10.2.2. Adding the Hands
        1. 10.2.2.1. Creating the timer
          1. Implementing OnTimer
        2. 10.2.2.2. DrawTime method
        3. 10.2.2.3. Drawing the hands
          1. Deleting the existing line
        4. 10.2.2.4. RotateTransform
        5. 10.2.2.5. FillElipse
        6. 10.2.2.6. Drawing the new time
        7. 10.2.2.7. Implementing the 24 hour clock button
      3. 10.2.3. Drawing the Animated Date
        1. 10.2.3.1. Animating the string
          1. 10.2.3.1.1. The LtrDraw class
          2. 10.2.3.1.2. The StringDraw class
          3. 10.2.3.1.3. New member variables
          4. 10.2.3.1.4. Modifying the constructor
        2. 10.2.3.2. Resetting the center
  15. 11. Labels and Buttons
    1. 11.1. Label
      1. 11.1.1. Using labels to provide access keys
      2. 11.1.2. LinkLabel
    2. 11.2. Button Classes
      1. 11.2.1. Button
        1. 11.2.1.1. DialogResult property
        2. 11.2.1.2. PerformClick method
        3. 11.2.1.3. Access keys
        4. 11.2.1.4. Form Button properties
      2. 11.2.2. CheckBox
      3. 11.2.3. RadioButton
  16. 12. Text Controls
    1. 12.1. Text
    2. 12.2. Editable Text Controls: TextBoxBase
      1. 12.2.1. Properties and Methods
      2. 12.2.2. Events
    3. 12.3. RichTextBox
      1. 12.3.1. RichTextBox Methods and Events
  17. 13. Other Basic Controls
    1. 13.1. Containers
    2. 13.2. Tabbed Pages
      1. 13.2.1. Tabbed Pages Using Visual Studio .NET
    3. 13.3. PictureBox
    4. 13.4. ScrollBar
      1. 13.4.1. As a Positioning Device
      2. 13.4.2. As a Parameter Adjuster
    5. 13.5. TrackBar
    6. 13.6. Up-Down Controls
      1. 13.6.1. NumericUpDown
      2. 13.6.2. DomainUpDown
    7. 13.7. ProgressBar
  18. 14. TreeView and ListView
    1. 14.1. Class Hierarchy
    2. 14.2. Splitter
      1. 14.2.1. Splitters in Visual Studio .NET
    3. 14.3. TreeView
      1. 14.3.1. Code Analysis
        1. 14.3.1.1. The constructor
        2. 14.3.1.2. Populating the Tree
        3. 14.3.1.3. Look Ma, no recursion
        4. 14.3.1.4. TreeView events
    4. 14.4. ListView
      1. 14.4.1. Windows Explorer Clone
        1. 14.4.1.1. Creating the form
        2. 14.4.1.2. Setting events
        3. 14.4.1.3. Creating the menus
        4. 14.4.1.4. Populating the ListView
        5. 14.4.1.5. Adding SubItems to the ListView
        6. 14.4.1.6. Activation
        7. 14.4.1.7. Sorting by Text property
        8. 14.4.1.8. Sorting by clicking on a column
      2. 14.4.2. Editing the Labels
      3. 14.4.3. Complete Code Listings
  19. 15. List Controls
    1. 15.1. Class Hierarchy
    2. 15.2. ListControls
      1. 15.2.1. Filling a ListControl
        1. 15.2.1.1. Filling a ListControl via the Items collection
        2. 15.2.1.2. Filling a ListControl using a DataSource
      2. 15.2.2. Retrieving Item Text
      3. 15.2.3. ListControl Events
      4. 15.2.4. ListBox
        1. 15.2.4.1. CheckedListBox
      5. 15.2.5. ComboBox
  20. 16. Date and Time Controls
    1. 16.1. Class Hierarchy
    2. 16.2. Date and Time Values
      1. 16.2.1. DateTime Structure
      2. 16.2.2. TimeSpan Structure
      3. 16.2.3. Date and Time Format Strings
    3. 16.3. DateTimePicker
    4. 16.4. MonthCalendar
    5. 16.5. Timer Component
  21. 17. Custom Controls
    1. 17.1. Specializing an Existing Control
      1. 17.1.1. Testing the Control
      2. 17.1.2. Adding Properties
    2. 17.2. Creating a User Control
      1. 17.2.1. Teeter Totter Analysis
      2. 17.2.2. Properties
      3. 17.2.3. Handling Internal Events
      4. 17.2.4. Publishing Events
      5. 17.2.5. Testing the User Control
        1. 17.2.5.1. Creating the Teeter Totter tester project
        2. 17.2.5.2. Interacting with properties
        3. 17.2.5.3. Handling events
        4. 17.2.5.4. Handling the event in C#
        5. 17.2.5.5. Handling the event In Visual Basic .NET
        6. 17.2.5.6. Populating the listbox
    3. 17.3. Creating Custom Controls from Scratch
      1. 17.3.1. Testing the custom control
  22. 18. Menus and Bars
    1. 18.1. Creating Your First Menu
    2. 18.2. The MainMenu Object
      1. 18.2.1. Adding Menu Items
      2. 18.2.2. Responding to Events
        1. 18.2.2.1. Adding MDI Windows
      3. 18.2.3. Option Menu Items
      4. 18.2.4. Shared Event Handlers
        1. 18.2.4.1. Handling radio button options
      5. 18.2.5. Menu Navigation
        1. 18.2.5.1. Shortcut keys
      6. 18.2.6. Creating Menus by Hand
        1. 18.2.6.1. Analysis
        2. 18.2.6.2. Creating the options menu
        3. 18.2.6.3. The options menu item event handler
      7. 18.2.7. Merging Menus
        1. 18.2.7.1. Merging menus by hand
      8. 18.2.8. OwnerDraw Menus
        1. 18.2.8.1. Create the bitmaps
        2. 18.2.8.2. Set the OwnerDraw property
        3. 18.2.8.3. Implement the event handlers
      9. 18.2.9. Context Menus
    3. 18.3. Toolbars
      1. 18.3.1. Creating the ImageList
        1. 18.3.1.1. Adding the toolbar
        2. 18.3.1.2. Adding event handlers
        3. 18.3.1.3. Tool tips
        4. 18.3.1.4. Altering the appearance
        5. 18.3.1.5. Adding a combo box to the Toolbar
    4. 18.4. Writing It by Hand
      1. 18.4.1. Creating the Toolbar by Hand: Analysis
        1. 18.4.1.1. Adding ToolTips and setting their properties
        2. 18.4.1.2. Setting properties of the Toolbar
        3. 18.4.1.3. Handling ToolBar button clicks
    5. 18.5. Status Bars
      1. 18.5.1. Analysis
        1. 18.5.1.1. Setting the timer
        2. 18.5.1.2. Updating the status bar based on menu information
        3. 18.5.1.3. Handling status bar events
        4. 18.5.1.4. Adding a context menu
  23. 19. ADO.NET
    1. 19.1. Bug Database: A Windows Application
      1. 19.1.1. Preliminary Design Considerations
      2. 19.1.2. The Bug Database Design
    2. 19.2. The ADO.NET Object Model
      1. 19.2.1. The DataSet Class
        1. 19.2.1.1. The DataTable class
        2. 19.2.1.2. The DataRow class
      2. 19.2.2. DBCommand and DBConnection
      3. 19.2.3. The DataAdapter Object
    3. 19.3. Getting Started with ADO.NET
      1. 19.3.1. Using the Wizards
      2. 19.3.2. Command and Control Objects
    4. 19.4. Managed Providers
    5. 19.5. Binding Data
    6. 19.6. Data Reader
    7. 19.7. Creating a DataGrid
      1. 19.7.1. Controlling Column Headings
      2. 19.7.2. Displaying Parent/Child Relationships
      3. 19.7.3. Creating Data Objects by Hand
      4. 19.7.4. Creating the DataSet by Hand
        1. 19.7.4.1. Setting column properties
        2. 19.7.4.2. Setting constraints
        3. 19.7.4.3. Adding data to the table
        4. 19.7.4.4. Adding additional tables to the DataSet
        5. 19.7.4.5. Adding rows with an array of objects
      5. 19.7.5. Creating Primary Keys
      6. 19.7.6. Creating Foreign Keys
      7. 19.7.7. Creating Data Relations
  24. 20. Updating ADO.NET
    1. 20.1. Updating with SQL
    2. 20.2. Updating Data with Transactions
      1. 20.2.1. The ACID Test
      2. 20.2.2. Implementing Transactions
        1. 20.2.2.1. Database transactions
        2. 20.2.2.2. Filling in the ListBoxes
      3. 20.2.3. DataBase Transactions
      4. 20.2.4. Connection Transaction
    3. 20.3. Updating Data Using DataSets
      1. 20.3.1. The DataSet and the DataAdapter
      2. 20.3.2. Steps for Updating the Database
        1. 20.3.2.1. Creating and displaying a DataSet
        2. 20.3.2.2. Updating the records in the DataSet
        3. 20.3.2.3. Updating the DataSet
      3. 20.3.3. Updating the Database from the Dataset
        1. 20.3.3.1. The delete command
        2. 20.3.3.2. The Update command
        3. 20.3.3.3. The Insert command
        4. 20.3.3.4. Adding transaction support
        5. 20.3.3.5. Calling the Update method
    4. 20.4. Multiuser Updates
      1. 20.4.1. Lock the Records
      2. 20.4.2. Compare Original Against New
      3. 20.4.3. Handle the Errors
    5. 20.5. Command Builder
  25. 21. Exceptions and Debugging
    1. 21.1. Bugs Versus Exceptions
    2. 21.2. Exceptions
    3. 21.3. Throwing and Catching Exceptions
      1. 21.3.1. The Throw Statement
      2. 21.3.2. The Try and Catch Statements
        1. 21.3.2.1. The finally statement
    4. 21.4. Bugs
    5. 21.5. Debugging in Visual Studio .NET
      1. 21.5.1. Finding Syntax Errors
      2. 21.5.2. Debug Versus Release Mode
      3. 21.5.3. The Debug Toolbar
      4. 21.5.4. Breakpoints
        1. 21.5.4.1. Setting a breakpoint
        2. 21.5.4.2. Breakpoint window
        3. 21.5.4.3. Breakpoint properties
          1. 21.5.4.3.1. Condition
          2. 21.5.4.3.2. Hit count
        4. 21.5.4.4. Breakpoint icons
        5. 21.5.4.5. Stepping through code
        6. 21.5.4.6. Examining variables and objects
        7. 21.5.4.7. Immediate window
      5. 21.5.5. Examining Objects
        1. 21.5.5.1. Autos window
        2. 21.5.5.2. Locals window
        3. 21.5.5.3. This/Me window
        4. 21.5.5.4. Watch window
        5. 21.5.5.5. Threads window
        6. 21.5.5.6. Modules window
        7. 21.5.5.7. Disassembly window
        8. 21.5.5.8. Registers window
        9. 21.5.5.9. Memory windows
    6. 21.6. Assert Yourself
      1. 21.6.1. Side effects
        1. 21.6.1.1. Trace versus Debug
        2. 21.6.1.2. Trace and Debug methods
      2. 21.6.2. Listeners
  26. 22. Configuration and Deployment
    1. 22.1. Class Hierarchy
    2. 22.2. Configuration
      1. 22.2.1. Hierarchical Configuration
      2. 22.2.2. Configuration File Format
      3. 22.2.3. Configuration Section Handler Declarations
      4. 22.2.4. Configuration Sections
        1. 22.2.4.1. appSettings
        2. 22.2.4.2. system.diagnostics
        3. 22.2.4.3. system.net
        4. 22.2.4.4. system.web
        5. 22.2.4.5. system.windows.forms
        6. 22.2.4.6. system.runtime.remoting
      5. 22.2.5. Custom Configuration
        1. 22.2.5.1. Name/Value pairs
        2. 22.2.5.2. Objects
      6. 22.2.6. .NET Framework Configuration Tool (mscorcfg.msc)
    3. 22.3. Assemblies
      1. 22.3.1. Microsoft Intermediate Language (MSIL)
        1. 22.3.1.1. ILDASM
      2. 22.3.2. Modules
      3. 22.3.3. Manifests
      4. 22.3.4. Single- and Multimodule Assemblies
        1. 22.3.4.1. Single-file assemblies
        2. 22.3.4.2. Multimodule assemblies
          1. 22.3.4.2.1. Preparing the source files
          2. 22.3.4.2.2. Compiling the modules
          3. 22.3.4.2.3. Linking the modules into an assembly
      5. 22.3.5. Versioning
      6. 22.3.6. Private Versus Shared Assemblies
      7. 22.3.7. Strong Names
        1. 22.3.7.1. Creating a strong name, step by step
        2. 22.3.7.2. Delayed signing
      8. 22.3.8. Global Assembly Cache (GAC)
        1. 22.3.8.1. GacUtil.exe
      9. 22.3.9. Resolving and Binding Assemblies
    4. 22.4. Build Configurations
    5. 22.5. Deployment
      1. 22.5.1. Private (XCOPY) Installations
      2. 22.5.2. Windows Installer
      3. 22.5.3. Using Visual Studio .NET
        1. 22.5.3.1. Using the wizard to create a debug version
        2. 22.5.3.2. Creating a release version
        3. 22.5.3.3. Further customizations
          1. 22.5.3.3.1. File System
          2. 22.5.3.3.2. Registry
          3. 22.5.3.3.3. File Types
          4. 22.5.3.3.4. User Interface
          5. 22.5.3.3.5. Custom Actions
          6. 22.5.3.3.6. Launch Conditions
      4. 22.5.4. Internet Deployment
        1. 22.5.4.1. Auto deployment via Cabinet (CAB) files
        2. 22.5.4.2. Auto deployment via URL
        3. 22.5.4.3. Auto deployment via calling program
      5. 22.5.5. .NET Runtime Redistributable
  27. A. Characters and Keys
  28. About the Authors
  29. Colophon
  30. Copyright