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

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

Embed Size (px)

Citation preview

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

DEV393

.NET Windows Forms Tips and Tricks

Ken GetzSenior ConsultantMCW Technologies, LLC

Page 2: DEV393.NET Windows Forms Tips and Tricks Ken Getz Senior Consultant MCW 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

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

Special Thanks…

Half of the topics in this session developed by Shawn Burke

Development Manager, .NET Client Team

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

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

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

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!

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

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

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

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

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

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

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

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

demodemo

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

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

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

Controlling UI With Controlling UI With DatabindingDatabinding

demodemo

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

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

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

Create Synchronized Create Synchronized Scrolling DataGridsScrolling DataGrids

demodemo

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

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

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

Theming your Theming your Windows Forms Windows Forms ApplicationApplication

demodemo

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

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

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

Create a Resizable Create a Resizable Checkbox ControlCheckbox Control

demodemo

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

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

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

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

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

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

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

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

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

Formatting Columns Formatting Columns and Handling Editingand Handling Editing

demodemo

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

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

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

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

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

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

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

Handle Thread Switch Handle Thread Switch using Invoke Methodusing Invoke Method

demodemo

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

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

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

Using the PropertyGrid Using the PropertyGrid in Your Applicationin Your Application

demodemo

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

Ask The ExpertsGet Your Questions Answered

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

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

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

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

evaluationsevaluations

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

© 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.