Download presentation
Presentation is loading. Please wait.
Published byPatience Webb Modified over 9 years ago
1
DEV393.NET Windows Forms Tips and Tricks Ken Getz Senior Consultant MCW Technologies, LLC
2
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
3
Special Thanks… Half of the topics in this session developed by Shawn Burke Development Manager,.NET Client Team
4
Eight Real-World Tips 1.Display Controls Your Way Owner-drawn ListBox and MenuItem 2.Use Data to Control the User Interface Working with the data bindings 3.Expose Protected Info with Inheritance Synchronized scrolling DataGrids 4.Improve the Look of Your Application Add XP Theming and non-rectangular windows
5
Eight Real-World Tips 5.Avoid Work When Drawing Controls ControlPaint class 6.Control DataGrid Formatting and Data Entry Use Column styles and DataView properties 7.Handle Single-Threaded Forms Use Invoke to handle thread switch 8.Allow Users to Easily Modify Settings Use the PropertyGrid control in your app!
6
Display Controls Your Way Use GDI+ to handle painting yourself Applies to (among others): ListBox/ComboBox MenuItem (set OwnerDraw property to True) For ListBox/ComboBox, set DrawMode property to one of: Normal OwnerDrawFixed OwnerDrawVariable
7
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 DrawItem Draw the item using GDI+ in the rectangle provided
8
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
9
Create Owner-Drawn ListBox and MenuItem demo demo
10
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
11
Controlling UI With Databinding demo demo
12
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 control Or can simply create a class in your project
13
Create Synchronized Scrolling DataGrids demo demo
14
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
15
Theming your Windows Forms Application demo demo
16
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
17
Create a Resizable Checkbox Control demo demo
18
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 GridTableStylesCollection Collection of DataGridTableStyle objects
19
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
20
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
21
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 rescue Provides AllowEdit, AllowNew, AllowDelete properties All based on DataGrid binding to DataView
22
Formatting Columns and Handling Editing demo demo
23
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-safe You can’t reliably interact with the form from a background thread Can’t handle multiple threads updating properties concurrently
24
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
25
Solving the Problem Examine the Original Code Create a Delegate Type Create the Delegate Instance and Call It Use Invoke method Pass array containing all parameters Sample displays thread information So you can verify the running thread
26
Handle Thread Switch using Invoke Method demo demo
27
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
28
Using the PropertyGrid in Your Application demo demo
29
Ask The Experts Get Your Questions Answered Stop by Ask the Experts area 16:00 to 17:00, Wednesday
30
Community Resources http://www.microsoft.com/communities/default.mspx Most Valuable Professional (MVP) http://www.mvp.support.microsoft.com/ Newsgroups Converse online with Microsoft Newsgroups, including Worldwide http://www.microsoft.com/communities/newsgroups/default.mspx User Groups Meet and learn with your peers http://www.microsoft.com/communities/usergroups/default.mspx
31
evaluations evaluations
32
© 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.