Table of Contents INTRODUCTION...INTRO-1 Prerequisites...INTRO-2 Installing the Practice Files...INTRO-3 Software Requirements...INTRO-3 Sample Database...INTRO-3 Security...INTRO-4 Installation...INTRO-4 About the Authors...INTRO-5 INTRODUCTION TO WINDOWS FORMS...1-1.NET Does Windows...1-2 Adding Controls to a Form...1-6 Investigating Snap Lines...1-7 Adjusting Control Sizes and Positions...1-8 Controlling Tab Order...1-10 A Peek behind the Scenes...1-12 Viewing the Code...1-12 Adding Event Handling Code...1-18 Adding a Menu to the Form...1-20 Adding Non-Graphical Components...1-26 The ToolTip Component...1-26 The Timer Component...1-28 LAB 1: INTRODUCTION TO WINDOWS FORMS...1-35 Lab 1 Overview...1-36 Create a Windows Application...1-37 Anchor the Controls...1-41 Add Event-Handling Code...1-43 TAKING ADVANTAGE OF THE FORM EVENT MODEL...2-1 How Visual C# Handles Events...2-2 Working with Events...2-2 Working with Forms...2-7 Peeking under the Event Covers...2-13 Exploring Form and Control Events...2-27 TOC-1
Form Events...2-27 Keyboard Events...2-30 Mouse Events...2-37 Control Events...2-41 Validating User Input...2-41 LAB 2: TAKING ADVANTAGE OF THE FORM EVENT MODEL...2-51 Lab 2 Overview...2-52 Creating an Event Handler Using Visual Studio...2-53 Creating a Dynamic Event Handler...2-55 COMMON WINDOWS FORMS CONTROLS...3-1 Windows Forms Controls...3-2 Label Control...3-3 TextBox Control...3-8 Button Control...3-15 CheckBox Control...3-19 RadioButton Control...3-22 ComboBox Control...3-25 ListBox Control...3-29 CheckedListBox Control...3-31 LinkLabel Control...3-33 NumericUpDown Control...3-35 PictureBox Control...3-37 ProgressBar Control...3-39 LAB 3: COMMON WINDOWS FORMS CONTROLS...3-45 Lab 3 Overview...3-46 Create a Simple Data Entry Form...3-47 Modify the Data Entry Form...3-52 Add Validation to the Data Entry Form...3-56 EXCEPTION HANDLING...4-1 Perspectives on Exception Handling...4-2 Exception Handling in.net...4-2 The Demonstration Code...4-4 Getting Started with Exception Handling...4-5 No Error Handling...4-5 Adding a Simple Try/Catch Block...4-8 Unhandled Exceptions...4-10 Using an Exception Object...4-10 TOC-2
Catching Specific Exceptions...4-14 Taking Advantage of the Exception Hierarchy...4-17 Raising Errors...4-19 Using the Throw Keyword...4-19 Exception-Handling Options...4-20 Passing Error Information...4-21 Running Code Unconditionally...4-24 The Finally Block...4-24 Cleaning Up: The Using Statement...4-25 Handling Unhandled Exceptions...4-28 Creating Exception Classes...4-31 LAB 4: EXCEPTION HANDLING...4-39 Lab 4 Overview...4-40 Add Simple Exception Handling...4-42 Handle Individual Exceptions...4-44 Add Code Running Unconditionally...4-47 WORKING WITH STRINGS AND DATES...5-1 Basic.NET Data Types...5-2 Working with Text: The String Class...5-3 Investigating the String Class...5-3 Creating Strings...5-4 String Properties...5-5 Static String Methods...5-6 Instance String Methods...5-11 Manipulating Text with the StringBuilder Class...5-14 Working with Dates and Times...5-18 DateTime Constructors...5-18 Static Members...5-20 Simple Properties...5-21 Conversion Methods...5-22 Calculation Methods...5-23 Static Methods...5-24 Using the DateTime Type...5-25 Handling Elapsed Times with the TimeSpan Type...5-30 Measuring Elapsed Time...5-35 LAB 5: WORKING WITH STRINGS AND DATES...5-41 Lab 5 Overview...5-42 TOC-3
Work with Strings...5-43 Work with the StringBuilder Class...5-48 Use StopWatch and TimeSpan to Measure Elapsed Time...5-52 ACCESSING DATA...6-1 Overview of ADO.NET...6-2 ADO.NET Classes...6-2 The ADO.NET Architecture...6-3 Creating ADO.NET Data Objects...6-4 Connecting to Data...6-9 Setting Up Connection Information...6-9 Creating Connection Strings in the Configuration File...6-12 Using the Connection Object...6-14 Executing Commands...6-17 Working with Data Readers...6-17 Using the SQLDataReader...6-18 Working with Data...6-23 Working with Data Using a Data Adapter...6-23 Managing Data with the DataSet Object...6-23 Filling a DataTable within a DataSet...6-25 Filling a DataTable from SQL Server...6-28 Choosing an ADO.NET Provider...6-33 The SQL Server.NET Data Provider...6-33 The OLE DB.NET Data Provider...6-34 The ODBC Data Provider...6-34 Writing Code for Multiple Providers...6-35 LAB 6: ACCESSING DATA...6-45 Lab 6 Overview...6-46 Create a Connection String in the Configuration File...6-47 Use the DataReader to Fill a ListBox with Categories...6-49 Use a DataSet to Fill a DataGridView with Products...6-52 DATA BINDING TECHNIQUES...7-1 Data Binding Basics...7-2 Creating a Data Source...7-3 The Data Sources Window...7-7 Creating Data-Bound Forms...7-9 Data Binding Components and Controls...7-13 TOC-4
The DataSet...7-13 The TableAdapter...7-15 The BindingSource Component...7-17 The BindingNavigator Control...7-19 Displaying Data from Related Tables...7-21 Displaying Data from Lookup Tables...7-25 Adding Search Capabilities to a Form...7-28 Filtering by Values from a Lookup Table...7-35 Data Validation...7-40 Determining Whether the User Made Changes...7-40 Validating Data at the Form Level...7-43 Validating Data at the Data Level...7-45 Working with Local Data...7-50 LAB 7: DATA BINDING TECHNIQUES...7-57 Lab 7 Overview...7-58 Create a Simple Data-Bound Form...7-59 Search and Filter Based on Criteria...7-69 Validate Data...7-78 USING THE DATA CONTROLS...8-1 Data Controls...8-2 The BindingNavigator Control...8-3 BindingNavigator ToolStripItems...8-3 Built-In and Custom Functionality...8-5 Adding a BindingNavigator Control to a Form...8-6 The BindingSource Component...8-8 Data Navigation...8-8 Data Manipulation...8-10 Sorting and Filtering...8-11 Working with the Underlying Data...8-12 Working with Related Data...8-17 The DataGridView Control...8-21 Customizing How Data is Displayed...8-21 Formatting Cells at Runtime...8-26 Displaying Calculated Values...8-29 Displaying Images...8-30 Adding Controls to a DataGridView...8-33 The ReportViewer Control...8-36 TOC-5
Creating and Viewing Reports...8-36 Local and Remote Report Processing...8-41 LAB 8: USING THE DATA CONTROLS...8-45 Lab 8 Overview...8-46 Navigation and Data Manipulation with the BindingSource Component...8-47 Formatting the DataGridView Control...8-54 Displaying Reports with the ReportViewer Control...8-62 HANDLING INPUT/OUTPUT TASKS...9-1 Introducing the System.IO Namespace...9-2 Selecting Files...9-4 Browsing for Folders...9-13 Handling Path Information...9-17 Working with Directories and Files...9-21 Retrieving Information about Drives...9-23 Investigating the Objects...9-26 Calling Methods of the Directory Class...9-28 Filling the TreeView...9-31 Displaying Drive, File, and Directory Information...9-34 Using Stream-Based Objects to Read and Write File Contents...9-40 Reading and Writing Text Files...9-40 Investigating the Code...9-42 Alternate Solutions...9-46 Writing to Files Using the StreamWriter...9-47 LAB 9: HANDLING INPUT/OUTPUT TASKS...9-53 Lab 9 Overview...9-54 Browse for Files...9-55 Display Selected File Information...9-57 Load File Contents...9-59 PROJECT SETTINGS IN VISUAL STUDIO 2005...10-1 The Project Designer...10-2 Application Properties...10-3 Assembly Name...10-3 Root Namespace...10-4 Icon...10-4 Startup Object...10-4 TOC-6
Assembly Information...10-5 Compilation Properties...10-8 Conditional Compilation...10-9 Compiler Errors and Warnings...10-11 Build Events...10-12 Debugging Properties...10-14 Start Action...10-14 Start Options...10-15 Enable Debuggers...10-15 Project References...10-17 Adding Project References...10-17 Project Resources...10-22 Project Settings...10-26 LAB 10: PROJECT SETTINGS IN VISUAL STUDIO 2005...10-33 Lab 10 Overview...10-34 Application Behavior and Appearance...10-35 Building the Project...10-37 Project Resources and Settings...10-42 DEBUGGING YOUR APPLICATIONS...11-1 Debugging Applications...11-2 Setting Breakpoints and Stepping through Code...11-3 Setting a Breakpoint...11-3 Continuing Program Execution...11-5 Setting Conditional Breakpoints...11-5 Specifying a Breakpoint Hit Count...11-7 Setting a Breakpoint on a Function...11-7 Breakpoints Window...11-9 Breaking on Exceptions...11-9 Debugging Code in Assemblies...11-12 Stepping into Code in an Assembly...11-12 Stepping into Code Written in another Language...11-14 Evaluating Variables and Objects...11-15 DataTips and Visualizers...11-15 Using the Watch Window and QuickWatch Dialog Box...11-18 Using the Locals Window...11-20 Using the Autos Window...11-22 Using the Immediate Window...11-22 TOC-7
Edit and Continue...11-24 Tracing Applications...11-28 Trace Sources...11-28 Trace Switches...11-32 Trace Listeners...11-33 Trace Filtering...11-37 Using Configuration Files to Control Tracing...11-38 LAB 11: DEBUGGING YOUR APPLICATIONS...11-45 Lab 11 Overview...11-46 Setting Breakpoints and Stepping Through Code...11-47 Evaluating Expressions and Using Edit and Continue...11-52 Tracing Applications...11-60 CONTAINER CONTROLS...12-1 Introducing Container Controls...12-2 Working with Container Controls...12-3 Creating the Switchboard Form...12-3 Grouping Controls with the GroupBox Control...12-7 GroupBox Control Members...12-7 BackgroundImage and BackColor Properties...12-11 Visible, Enabled, and Font Properties...12-12 Introducing the Panel Control...12-14 BorderStyle Property...12-14 Scrolling Features...12-15 Displaying Multiple Panels with the SplitContainer Control...12-17 Orientation Property...12-18 IsSplitterFixed Property...12-19 SplitterWidth Property...12-19 SplitterDistance Property...12-19 FixedPanel Property...12-19 Panel1MinSize and Panel2MinSize Properties...12-20 Panel1Collapsed and Panel2Collapsed Properties...12-21 SplitContainer Control Events...12-21 Laying out Panels with the TabControl Control...12-24 Working with Tab Pages...12-26 Alignment Property...12-27 Appearance Property...12-27 MultiLine Property...12-28 SizeMode Property...12-29 TOC-8
ShowToolTips Property...12-31 Informational Properties...12-32 Adding Tab Pages...12-33 TabControl Events...12-33 Simple Layout Using the FlowLayoutPanel Control...12-35 Working with the FlowLayoutPanel Control...12-38 FlowLayoutPanel Control Properties...12-42 Advanced Layout Using the TableLayoutPanel Control...12-46 Getting Started with the TableLayoutPanel Control...12-46 Setting Row and Column Styles...12-48 Positioning New Controls...12-51 Adding Controls Programmatically...12-51 Working with Other Properties...12-53 Laying out the Columns or Rows...12-54 TableLayoutPanel Control Extender Properties...12-58 TableLayoutPanel Control Methods...12-60 LAB 12: CONTAINER CONTROLS...12-67 Lab 12 Overview...12-68 Lay Out a Panel Control...12-69 Use the SplitContainer Control...12-72 Work with the TableLayoutPanel Control...12-74 MENUS AND TOOLBARS...13-1 Introducing the 'Strips...13-2 Adding Menu Images...13-6 Using Visual Studio s Automatic Menus...13-8 The ToolStrip Control...13-9 The StatusStrip Control...13-12 The ContextMenuStrip Control...13-16 The ToolStripContainer Control...13-18 Working with a ToolStripItem...13-20 ToolStripItem Display Styles...13-22 Making and Saving Runtime Customizations...13-23 ToolStrip Layout Issues and Overflow...13-28 Working with the 'Strips...13-33 RichTextBox Demo...13-33 WebBrowser Demo...13-38 LAB 13: MENUS AND TOOLBARS...13-45 TOC-9
Lab 13 Overview...13-46 Add the MenuStrip...13-47 Add the ToolStrip...13-49 Add the StatusStrip...13-51 CREATING MDI APPLICATIONS...14-1 MDI Applications...14-2 Child Windows...14-3 Creating an MDI Application...14-3 MDI Parent and Child Forms...14-4 MDI Parent Form...14-4 MDI Child Forms...14-5 Managing Open Windows...14-8 Automatically Creating a Window List...14-8 Manually Creating a Window List...14-9 Arranging Open Windows...14-10 Managing Menus...14-14 Merging Menus...14-14 Design Considerations...14-18 Working with Open Forms...14-21 Handling the mdichildactivate Event...14-21 Handling Edit and Format Menu Items...14-22 Passing Data between Open Forms...14-24 Handling Form Closing...14-27 LAB 14: CREATING MDI APPLICATIONS...14-33 Lab 14 Overview...14-34 Create the Parent and Child Forms...14-35 Manage Menus and Open Windows...14-37 Work with Open Forms...14-46 COMPLEX WINDOWS FORMS CONTROLS...15-1 Introducing Complex Controls...15-2 NotifyIcon Control...15-3 Investigating the Sample Form...15-5 WebBrowser Control...15-11 Investigating the Sample Form...15-13 Investigating WebBrowser Navigation...15-14 Displaying Dialog Boxes...15-16 TOC-10
Reacting to Environment Changes...15-17 In Summary...15-22 MaskedTextBox Control...15-23 Operation of the MaskedTextBox Control...15-24 Reacting to the MaskInputRejected Event...15-29 Status Properties...15-30 In Summary...15-31 RichTextBox Control...15-32 Handling State...15-33 Undo and Redo...15-38 Working with the Clipboard...15-40 Working with Fonts and Styles...15-42 Formatting Text...15-47 Working with Zoom, Links, and More...15-51 TreeView and ListView Controls...15-53 TreeView Control...15-55 ListView Control...15-65 Setting Up the Images...15-68 Filling the List of Files...15-69 Working with ListView Items...15-72 Showing Items in Groups...15-74 Sorting the Columns...15-78 MonthCalendar and DateTimePicker Controls...15-86 The MonthCalendar Control...15-87 The DateTimePicker Control...15-94 In Summary...15-98 LAB 15: COMPLEX WINDOWS FORMS CONTROLS...15-103 Lab 15 Overview...15-104 Set Up the NotifyIcon Control...15-105 Add the DateTimePicker Control...15-108 Add a Web Browser...15-111 CREATING CUSTOM CONTROLS...16-1 Control Creation in Windows Forms...16-2 Inheriting from an Existing Control...16-3 Required Steps...16-3 Creating a TextBox with Required Input...16-4 Adding the Finishing Touches...16-9 Inheriting Controls, In Review...16-13 TOC-11
Drawing Your Own Custom Controls...16-14 Inheriting from the Control Base Class...16-14 Control Properties...16-16 Drawing the Control...16-17 Default Values and Serializing Properties...16-20 Raising Events...16-23 Hiding Properties...16-25 Adding Ease-of-Use Features...16-26 Drawing Your Own Controls, In Review...16-26 Creating Composite Controls...16-28 Required Steps...16-28 The Sample Control...16-29 Designer Support for Properties...16-31 Handling Events...16-32 Setting the Default Control Text...16-32 Serializing the Text Property...16-33 Data Binding...16-34 Inheriting from UserControl, In Review...16-36 LAB 16: CREATING CUSTOM CONTROLS...16-41 Lab 16 Overview...16-42 Create the DigitalClock Control...16-43 Add Finishing Touches...16-48 WORKING WITH TOOLBOX COMPONENTS...17-1 Introducing Components...17-3 Adding Components to Your Forms...17-5 Timer Component...17-8 Using the Timer Component...17-8 BackgroundWorker Component...17-12 Understanding the Code...17-13 Introducing Multi-Threading with the BackgroundWorker Component...17-17 FileSystemWatcher Component...17-23 Working with the FileSystemWatcher Component...17-24 PerformanceCounter Component...17-28 Retrieving Values Using the PerformanceCounter Component...17-30 Creating Performance Counters...17-33 Process Component...17-38 TOC-12
Using the Process Component...17-38 Process Component Methods...17-42 Start a Process and React When it s Completed...17-44 Working with Processes...17-46 ServiceController Component...17-48 Using the ServiceController Component...17-48 Interacting with Windows Services...17-51 Working with Services...17-53 EventLog Component...17-59 Event Log Concepts...17-59 Using the EventLog Component...17-61 Investigating the Sample Form...17-63 Working with the Server Explorer...17-72 LAB 17: WORKING WITH TOOLBOX COMPONENTS...17-79 Lab 17 Overview...17-80 Monitor File Changes Using the FileSystemWatcher Component...17-81 Keep Track of File Changes Using the EventLog Component...17-84 Run an Application Using the Process Component...17-87 INTEROPERABILITY...18-1 Interoperability...18-2 COM and.net...18-3 Understanding COM Components...18-3 Managed and Unmanaged Code...18-4 Calling COM Objects from.net...18-6 Calling a COM DLL from.net...18-6 Interop Assemblies...18-11 Exporting Data to Excel...18-12 Calling.NET Assemblies from COM...18-16 Creating COM DLLs in Visual Studio...18-16 Creating Wrappers for.net Framework Classes...18-21 Using Platform Invoke...18-26 Using the Windows API...18-26 Platform Invoke Caveats...18-29 Web Services...18-30 Web Service Requirements and Technologies...18-30 Creating a Web Service...18-32 Adding a Project Reference to a Web Service...18-36 TOC-13
Calling a Web Service...18-38 LAB 18: INTEROPERABILITY...18-43 Lab 18 Overview...18-44 Call a COM DLL from.net...18-45 Call a COM Object from.net...18-48 Call a.net Assembly from COM...18-52 DEPLOYING APPLICATIONS USING CLICKONCE...19-1 ClickOnce Deployment...19-2 Publishing an Application...19-3 Publishing an Application with the Publish Wizard...19-3 Publish, Install, and Update Locations...19-8 Setting Publish Options...19-10 Prerequisites...19-12 Additional Files...19-13 Updating a Deployed Application...19-15 Rolling Back an Update...19-16 Setting Update Options...19-17 What Happens when You Publish an Application?...19-20 Examining Publish.htm and Setup.exe...19-21 The Deployment Manifest...19-21 The Application Manifest...19-24 Application Files...19-26 The ClickOnce Cache...19-27 What Happens when You Publish an Update to an Application?...19-28 ClickOnce and Security...19-30 Setting Permissions...19-31 Debugging a Partial Trust Application...19-33 Changes to the Manifest Files...19-34 Using the Deployment API to Manage Updates...19-35 LAB 19: DEPLOYING APPLICATIONS USING CLICKONCE...19-41 Lab 19 Overview...19-42 Publish an Application...19-43 Update an Application...19-45 Debug with Reduced Permissions...19-50 INDEX...INDEX-1 TOC-14