Download presentation
Presentation is loading. Please wait.
Published byDaisy Harvey Modified over 8 years ago
1
Sofia, Bulgaria | 9-10 October.NET Windows Forms Tips and Tricks Cathi Gero Prenia Software & Consulting Services cgero@prenia.com Cathi Gero Prenia Software & Consulting Services cgero@prenia.com
2
Sofia, Bulgaria | 9-10 October Cathi Gero ●Prenia Software & Consulting Services Corp. (www.prenia.com) ●Specialize in.NET mentoring / application development / training ●Certified Public Accountant (C.P.A.) ●Microsoft C# /.NET MVP ●Monthly column called Cathi Gero’s.NET Tips in UniversalThread Magazine ●Author of articles for MSDN, TSS, and other publications ●Sits on.NET Advisory Council ●Weblog: http://blogs.prenia.com/cathihttp://blogs.prenia.com/cathi ●Prenia Software & Consulting Services Corp. (www.prenia.com) ●Specialize in.NET mentoring / application development / training ●Certified Public Accountant (C.P.A.) ●Microsoft C# /.NET MVP ●Monthly column called Cathi Gero’s.NET Tips in UniversalThread Magazine ●Author of articles for MSDN, TSS, and other publications ●Sits on.NET Advisory Council ●Weblog: http://blogs.prenia.com/cathihttp://blogs.prenia.com/cathi
3
Sofia, Bulgaria | 9-10 October Real-World Tips and Tricks ●Using the System Tray for Your Application ●Automatically Advance To Next Control ●Using Extended Providers ●Create Balloon Style Tooltips ●Using API Calls to Provide Additional Functionality ●Making the Window Flash in the TaskBar ●Determining the Icon Associated With a File Type ●Only Allow One Instance of an Application ●Using the System Tray for Your Application ●Automatically Advance To Next Control ●Using Extended Providers ●Create Balloon Style Tooltips ●Using API Calls to Provide Additional Functionality ●Making the Window Flash in the TaskBar ●Determining the Icon Associated With a File Type ●Only Allow One Instance of an Application
4
Sofia, Bulgaria | 9-10 October Real-World Tips and Tricks ●How to Use Image Mapping ●How to Use Image Transitioning ●Take Advantage of Alpha Levels ●How to Load a TreeView On-Demand ●How to Use a Virtual Earth Inside a Form ●Allow Users to Easily Modify Application Settings ●Use the PropertyGrid control ●How to Use Image Mapping ●How to Use Image Transitioning ●Take Advantage of Alpha Levels ●How to Load a TreeView On-Demand ●How to Use a Virtual Earth Inside a Form ●Allow Users to Easily Modify Application Settings ●Use the PropertyGrid control
5
Sofia, Bulgaria | 9-10 October Using the System Tray ●Use the NotifyIcon control ●Associate a ContextMenu for user control in the system tray ●Set the ContextMenu property of the NotifyIcon control to the ContextMenu control ●Use the NotifyIcon control ●Associate a ContextMenu for user control in the system tray ●Set the ContextMenu property of the NotifyIcon control to the ContextMenu control
6
Sofia, Bulgaria | 9-10 October Using the System Tray Continued ●To have application be invisible at start- up: ●Instantiate main form before calling Application.Run ●Set the form’s Visible property to false ●To have application be invisible at start- up: ●Instantiate main form before calling Application.Run ●Set the form’s Visible property to false static void Main() { SystemTrayForm MainForm = new SystemTrayForm(); SystemTrayForm MainForm = new SystemTrayForm(); MainForm.Visible = false; MainForm.Visible = false; Application.Run(); Application.Run();}
7
Sofia, Bulgaria | 9-10 October Using the System Tray Continued ●Set NotifyIcon ’s Text property to description to show in System Tray ●When exiting application ●Set the NotifyIcon ’s Visible property to false before calling Application.Exit ●Set NotifyIcon ’s Text property to description to show in System Tray ●When exiting application ●Set the NotifyIcon ’s Visible property to false before calling Application.Exit D1 notifyIcon1.Visible = false; this.Close();Application.Exit();
8
Sofia, Bulgaria | 9-10 October Automatically Advancing To Next Control ●Once the MaxLength of a control is reached, advance to the next control ●Allows for faster data input by users ●Sub-class controls and add event handlers ●Use the TextChanged event handler to determine if max length has been reached ●Call parent form’s SelectNextControl method ●Once the MaxLength of a control is reached, advance to the next control ●Allows for faster data input by users ●Sub-class controls and add event handlers ●Use the TextChanged event handler to determine if max length has been reached ●Call parent form’s SelectNextControl method
9
Sofia, Bulgaria | 9-10 October Automatically Advancing To Next Control ● SelectNextControl method has the following parameters: ●Control : The Control at which to start the search ●Forward : true to move forward in the tab order; false to move backward in the tab order ●TabStopOnly : true to ignore the controls with the TabStop property set to false; otherwise, false ●Nested : true to include nested (children of child controls) child controls; otherwise, false ●Wrap : true to continue searching from the first control in the tab order after the last control has been reached; otherwise, false ● SelectNextControl method has the following parameters: ●Control : The Control at which to start the search ●Forward : true to move forward in the tab order; false to move backward in the tab order ●TabStopOnly : true to ignore the controls with the TabStop property set to false; otherwise, false ●Nested : true to include nested (children of child controls) child controls; otherwise, false ●Wrap : true to continue searching from the first control in the tab order after the last control has been reached; otherwise, false D2
10
Sofia, Bulgaria | 9-10 October Using Extended Providers ●A control that works with several controls ●Inserts additional properties into each control that it extends ●A control that works with several controls ●Inserts additional properties into each control that it extends
11
Sofia, Bulgaria | 9-10 October Using Extended Providers Continued ●Derive a class from: ● System.ComponentModel.Component ● System.ComponentModel.IExtenderProvide r ●Implement CanExtend method ●Called whenever a control in a form is selected in the forms designer ●Derive a class from: ● System.ComponentModel.Component ● System.ComponentModel.IExtenderProvide r ●Implement CanExtend method ●Called whenever a control in a form is selected in the forms designer bool IExtenderProvider.CanExtend(object extendee) { return (extendee is Control); }
12
Sofia, Bulgaria | 9-10 October Using Extended Providers Continued ●For each property to be added to the controls apply the ProvideProperty attribute to the class declaration [ProvideProperty("ToolTip", typeof(Control))] public class BalloonToolTip : Component, IExtenderProvider IExtenderProvider{......}
13
Sofia, Bulgaria | 9-10 October Using Extended Providers Continued ●Create the Get/Set for each property ●Uses a syntactical naming convention ●“Get” + name of the property ●“Set” + name of the property ●Create the Get/Set for each property ●Uses a syntactical naming convention ●“Get” + name of the property ●“Set” + name of the property [DefaultValue("")] public string GetToolTip(Control control) { string retval = (string)m_controls[control]; if (retval == null) return ""; return retval; } HashTable
14
Sofia, Bulgaria | 9-10 October Using Extended Providers Continued ●Each control has a new property ●Called the name using the ProvideProperty attribute followed by the word “on” and the instance name of the extender provider object ●Each control has a new property ●Called the name using the ProvideProperty attribute followed by the word “on” and the instance name of the extender provider object D3
15
Sofia, Bulgaria | 9-10 October API Calls to Add Additional Functionality ●The Windows API still provides useful functionality not found in the.NET Framework (yet!) ●Make the form reference flash in the TaskBar ●Determine the icon associated with a File Type ●Only allow one instance of a application to run ●The Windows API still provides useful functionality not found in the.NET Framework (yet!) ●Make the form reference flash in the TaskBar ●Determine the icon associated with a File Type ●Only allow one instance of a application to run
16
Sofia, Bulgaria | 9-10 October Making the Form Flash in the TaskBar ●Useful when running a long process to inform the user when it has completed ●Use the FlashWindowEx API found in the User32.dll ●Useful when running a long process to inform the user when it has completed ●Use the FlashWindowEx API found in the User32.dll D4
17
Sofia, Bulgaria | 9-10 October Finding the Associated Icon For a File Type ●Show the icon when displaying file names for richer UI ●Use the SHGetFileInfo API found in the Shell32.dll ●Show the icon when displaying file names for richer UI ●Use the SHGetFileInfo API found in the Shell32.dll D5
18
Sofia, Bulgaria | 9-10 October Allow Only One Instance of an Application ●Only allow one instance of an application to run ●Use the ShowWindowAsync API found in the User32.dll ●Find if another application instance is running ●If true then bring up that instance and exit out ●Only allow one instance of an application to run ●Use the ShowWindowAsync API found in the User32.dll ●Find if another application instance is running ●If true then bring up that instance and exit out D
19
Sofia, Bulgaria | 9-10 October Image Mapping ●Allows the user to click on a portion of an image and trap where the click occurred ●No control in WinForms like available in web development ●Drop ImageMap custom control on form ●Add “hot-spots” within your image ●Ellipse shape – AddEllipse method ●Rectangle shape – AddRectangle method ●Polygon shape – AddPolygon method ●Raises the RegionClick event when mouse clicked in a “hot-spot” ●Allows the user to click on a portion of an image and trap where the click occurred ●No control in WinForms like available in web development ●Drop ImageMap custom control on form ●Add “hot-spots” within your image ●Ellipse shape – AddEllipse method ●Rectangle shape – AddRectangle method ●Polygon shape – AddPolygon method ●Raises the RegionClick event when mouse clicked in a “hot-spot” D6
20
Sofia, Bulgaria | 9-10 October Image Transitioning ●Allows fading for images instead of the entire form ●To fade an image, use a timer to decrease the alpha (opacity) setting for the image ●Override the form’s OnPaint event handler ●Set the alpha for each image using the System.Drawing.Image.ColorMatrix class ●Repaint that area ●Allows fading for images instead of the entire form ●To fade an image, use a timer to decrease the alpha (opacity) setting for the image ●Override the form’s OnPaint event handler ●Set the alpha for each image using the System.Drawing.Image.ColorMatrix class ●Repaint that area D7
21
Sofia, Bulgaria | 9-10 October Load a TreeView On-Demand ●Only load the top nodes ●Allows for fast loading and unlimited number of nodes ●Create a dummy child node for all parent nodes containing children ●In the TreeView’s BeforeExpand event handler remove the dummy child node and then add all the children ●Only load the top nodes ●Allows for fast loading and unlimited number of nodes ●Create a dummy child node for all parent nodes containing children ●In the TreeView’s BeforeExpand event handler remove the dummy child node and then add all the children D8
22
Sofia, Bulgaria | 9-10 October Use Virtual Earth Inside a Form ●Mapping / imagery tiles ●User Navigation ●Bird’s Eye imagery ●Pushpins ●Find places / addresses ●Driving Directions / routing ●Mapping / imagery tiles ●User Navigation ●Bird’s Eye imagery ●Pushpins ●Find places / addresses ●Driving Directions / routing D9 map = new VEMap("myMap"); map.LoadMap(new VELatLong(lat_a, lon_a),zoom_a,VEMapStyle.Road);
23
Sofia, Bulgaria | 9-10 October Using the PropertyGrid ●The heart of the Property Browser in Visual Studio.NET ●Provides a simple model for displaying property settings ●Easier and more space-efficient than laying out a dialog ●Use the SelectedObject property to get/set the information to show in the PropertyGrid ●Use attributes to control how the property is displayed ●MSDN: http://msdn.microsoft.com/library/default.asp?url=/lib rary/en-us/dndotnet/html/usingpropgrid.asp http://msdn.microsoft.com/library/default.asp?url=/lib rary/en-us/dndotnet/html/usingpropgrid.asp ●The heart of the Property Browser in Visual Studio.NET ●Provides a simple model for displaying property settings ●Easier and more space-efficient than laying out a dialog ●Use the SelectedObject property to get/set the information to show in the PropertyGrid ●Use attributes to control how the property is displayed ●MSDN: http://msdn.microsoft.com/library/default.asp?url=/lib rary/en-us/dndotnet/html/usingpropgrid.asp http://msdn.microsoft.com/library/default.asp?url=/lib rary/en-us/dndotnet/html/usingpropgrid.asp
24
Sofia, Bulgaria | 9-10 October Using the PropertyGrid Continued D Imports System.ComponentModel Required for the Attributes DefaultPropertyAttribute("Title") Indicates the default property for the PropertyGrid (i.e. where cursor is located when loaded) CategoryAttribute("Application") Category attribute indicating the Category to which the property belongs to. Properties belonging to one category are grouped together. Browsable(True) Indicates whether the property is shown in the grid [ReadOnly](False) Indicates that the property is read-only or not BindableAttribute(False) Indicates whether the property can be bound to a data source or not DefaultValueAttribute("") Default value for the property DesignOnly(False) If true, it indicates that the property is Read-only at run time DescriptionAttribute("Enter Title for the application") Property Description. This description appears in the bottom when you click the property.
25
Sofia, Bulgaria | 9-10 October Questions? ●Sample code can be downloaded at: http://blogs.prenia.com/cathi ●Cathi Gero, cgero@prenia.comcgero@prenia.com ●Weblog: http://blogs.prenia.com/cathihttp://blogs.prenia.com/cathi ●Sample code can be downloaded at: http://blogs.prenia.com/cathi ●Cathi Gero, cgero@prenia.comcgero@prenia.com ●Weblog: http://blogs.prenia.com/cathihttp://blogs.prenia.com/cathi
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.