DEV393.NET Windows Forms Tips and Tricks Ken Getz Senior Consultant MCW Technologies, LLC

Preview:

Citation preview

DEV393

.NET Windows Forms Tips and Tricks

Ken GetzSenior ConsultantMCW Technologies, LLC

Me.About

Senior Consultant with MCW Technologies, LLC

Microsoft Regional Director, SoCal

Technical editor for Access-VB-SQL Advisor (www.advisor.com)

Author of several developer’s books on ASP.NET, VB, Access, and VBA

ASP.NET, VB.NET, ADO.NET, VB6 and Access video training for AppDev (www.appdev.com)

www.mcwtech.com

Special Thanks…

Half of the topics in this session developed by Shawn Burke

Development Manager, .NET Client Team

Eight Real-World Tips

1. Display Controls Your WayOwner-drawn ListBox and MenuItem

2. Use Data to Control the User InterfaceWorking with the data bindings

3. Expose Protected Info with InheritanceSynchronized scrolling DataGrids

4. Improve the Look of Your ApplicationAdd XP Theming and non-rectangular windows

Eight Real-World Tips

5. Avoid Work When Drawing ControlsControlPaint class

6. Control DataGrid Formatting and Data EntryUse Column styles and DataView properties

7. Handle Single-Threaded FormsUse Invoke to handle thread switch

8. Allow Users to Easily Modify SettingsUse the PropertyGrid control in your app!

Display Controls Your Way

Use GDI+ to handle painting yourselfApplies to (among others):

ListBox/ComboBox

MenuItem (set OwnerDraw property to True)

For ListBox/ComboBox, set DrawMode property to one of:

Normal

OwnerDrawFixed

OwnerDrawVariable

Drawing the Items

Provide code for two events:MeasureItem

Provide the width and height for each item

Not used for List/ComboBox if DrawMode set to OwnerDrawFixed

DrawItemDraw the item using GDI+ in the rectangle provided

It’s all in DrawEventArgs

DrawEventArgs parameter provides:Bounds property: Rectangle containing the item

Graphics property: Graphics context

Index property: Index of current item

State property: bit-flag property indicating current state of the item being drawn

DrawBackground method

DrawFocusRectangle method

Create Owner-Drawn Create Owner-Drawn ListBox and MenuItemListBox and MenuItem

demodemo

Using Data To Control UI

Data binding can be used for more than just data

Data binding infrastructure is very general and can bind almost any property to almost any value

Notifications based on property change events

Applications often want to show/hide/disable/enable controls based on the state of other controls

Controlling UI With Controlling UI With DatabindingDatabinding

demodemo

Expose Protected Info

Much functionality of WinForms controls is protected

Only available in control class, and in classes that inherit from the control

What if you want to use protected functionality yourself?

Must inherit from the control, and expose

Can create real custom controlOr can simply create a class in your project

Create Synchronized Create Synchronized Scrolling DataGridsScrolling DataGrids

demodemo

Improving the Look of your Application

Integrating Windows XP Visual Styles with your application

Many controls can theme: system controls, ListView, TreeView, TabControl, Progress Bar

Windows 2000, Windows XP Allow transparent top-level windows

Control shapes can be modified with regions

Theming your Theming your Windows Forms Windows Forms ApplicationApplication

demodemo

Avoid Work Drawing

Three ways to create custom controls:Inherit from existing

Create UserControl and its design surface

Draw yourself, using GDI+

The last option made easier through the magic of the ControlPaint class

Provides shared methods so you can easily create modified versions of standard controls

Create a Resizable Create a Resizable Checkbox ControlCheckbox Control

demodemo

Formatting a DataGrid Control

By default, DataGrid shows all columns in all tables

Using default settings for color, width, and formatting

What if you want more control over the way the data looks?

Use the GridTableStylesCollectionCollection of DataGridTableStyle objects

Configuring with Properties Window

Check out the TableStyles property

Set MappingName property to match DataMember providing data

Can also set general settings here

Select GridColumnStyles to set up individual column styles

Adds DataGridColumnStyle objects

Set MappingName property (column name)

Can set ReadOnly (see ProductID column)

Show code generated by the designer

Writing Code

You can also write code to manipulate styles:

Dim dgCol As DataGridTextBoxColumn = _ CType(Me.styleOrderDetails. _ GridColumnStyles(2), DataGridTextBoxColumn)dgCol.Format = "c"dgCol.Alignment = HorizontalAlignment.Right

Limit Editing in DataGrid

What if you want to allow edits, but not additions?

Deletions, but not edits?

Can set DataTable to be read-only, but that doesn't help

DataView to the rescueProvides AllowEdit, AllowNew, AllowDelete properties

All based on DataGrid binding to DataView

Formatting Columns Formatting Columns and Handling Editingand Handling Editing

demodemo

Forms Aren’t Thread-Safe

Call a Web Service asynchronously

CLR retrieves a background thread from the thread pool

Callback runs on the background thread

Forms aren’t thread-safeYou can’t reliably interact with the form from a background thread

Can’t handle multiple threads updating properties concurrently

The Invoke Method

Form (actually Control) provides Invoke method

Runs a delegate on the thread that owns the form/control's window

Calling Invoke performs a thread switch, and runs the called code on the host’s thread

Solving the Problem

Examine the Original Code

Create a Delegate Type

Create the Delegate Instance and Call ItUse Invoke method

Pass array containing all parameters

Sample displays thread informationSo you can verify the running thread

Handle Thread Switch Handle Thread Switch using Invoke Methodusing Invoke Method

demodemo

Using the PropertyGrid in Your Application

The PropertyGrid is the heart of the Property Browser in Visual Studio .NET

The PropertyGrid provides a simple model for displaying property settings

Easier and more space-efficient than laying out a dialog

Can use a serializable object with properties to display, save, and load state

Using the PropertyGrid Using the PropertyGrid in Your Applicationin Your Application

demodemo

Ask The ExpertsGet Your Questions Answered

Stop by Ask the Experts area16:00 to 17:00, Wednesday

Community Resources

Community Resourceshttp://www.microsoft.com/communities/default.mspx

Most Valuable Professional (MVP)http://www.mvp.support.microsoft.com/

NewsgroupsConverse online with Microsoft Newsgroups, including Worldwidehttp://www.microsoft.com/communities/newsgroups/default.mspx

User GroupsMeet and learn with your peershttp://www.microsoft.com/communities/usergroups/default.mspx

evaluationsevaluations

© 2003 Microsoft Corporation. All rights reserved.© 2003 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.