9.8 Creating a UI Type Editor

 <  Day Day Up  >  

You want to create a custom editor for a control's property.


Technique

You construct a type editor with a class separate from the control that uses the editor. Create a new class derived from System.Drawing.Design.UITypeEditor . You need to add a reference to the System.Design.dll to your project by choosing Project, Add Reference and selecting the System.Design assembly.

The UITypeEditor contains a virtual method named EditValue , which is called when a property using the type editor is being edited. To capture this event, override the EditValue in your derived class. The three parameters passed into the method allow you to access the control the property is associated with, obtain a reference to the Windows Forms editor service, and obtain a reference to the value itself. After ensuring that the necessary parameters are not null, create a new instance of the editor control that you want to use, initialize it with the value passed into the EditValue method for consistency, and call the DropDownControl method defined in the editor service object, as shown in Listing 9.5. The object value that is returned from the method is the value that is placed in the property field.

Listing 9.5 The UI Type Editor for the Percentage Property Displaying a TrackBar Control to Change the Property's Value
 using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; using System.Windows.Forms.Design; using System.Drawing.Design; namespace _8_UITypeEditor {     public class TemperatureGaugeDesigner : UITypeEditor     {         private IWindowsFormsEditorService editorService = null;         private TemperatureGauge5 tempGauge = null;         public override object EditValue(ITypeDescriptorContext context,                                         IServiceProvider provider, object value)         {             if (context != null && context.Instance != null && provider != null)             {                 // save the editor service object for later                 editorService = (IWindowsFormsEditorService)provider.GetService                    (typeof(IWindowsFormsEditorService));                 // save the control instance associated with this editor                 this.tempGauge = (TemperatureGauge5) context.Instance;              if (editorService != null)              {                  // create trackbar control                  TrackBar tb = new TrackBar();                  tb.Minimum = 0;                  tb.Maximum = 100;                  tb.Orientation = System.Windows.Forms.Orientation.Vertical;                  tb.TickFrequency = 5;                  tb.Size = new System.Drawing.Size(75, 160);                  tb.ValueChanged += new EventHandler(this.ValueChanged);                  // init with current property value                  tb.Value = (int)value;                  editorService.DropDownControl(tb);                  value = tb.Value;                 }             }             return value;         }         public override UITypeEditorEditStyle GetEditStyle(             ITypeDescriptorContext context)         {             if (context != null && context.Instance != null)             {                 return UITypeEditorEditStyle.DropDown;             }             return base.GetEditStyle(context);         }         private void ValueChanged(object sender, EventArgs e)         {             // update the associated control on the Windows Form             this.tempGauge.Percentage = ((TrackBar)sender).Value;         }     } } 

To associate a UI type editor with a property in a control, apply the Editor attribute to the property declaration. This attribute accepts two parameters denoting the type of the editor, which you can find by using the typeof keyword, and the type of the base class of the editor:

 
 [ Editor(typeof(TemperatureGaugeDesigner), typeof(UITypeEditor)), Description("Percentage of gauge that is filled."),Category("Data"), ParenthesizePropertyNameAttribute(true), DefaultValue(100)] public int Percentage {     get     {         return percent;     }     set     {         if( value < 0  value > 100 )             return;         percent = value;         Invalidate();     } } 

Comments

The property browser is an extensible container allowing you to customize how properties are edited by creating your own property editor. In some cases, creating a property is useful if the property value can benefit from a different editor from the default editor for that value type. For instance, the Percentage property defined earlier is an integer type. The default editor is a TextBox control that allows a user to type in the value for the property. However, it would seem beneficial to allow the user to use a different control that both constrains the range of values for the property and provides a visual indication of the changes that are occurring as the value of the property is incremented or decremented. The TrackBar control is well suited for this type of functionality, but a NumericUpDown control would also work well.

In Listing 9.5, you can see that an event handler for the ValueChanged event fires when the TrackBar control is created. Although this step isn't required, it does allow you to perform real-time feedback as a user changes the value of the property. When the trackbar changes, the TemperatureGauge control within the forms designer automatically updates. Because of this feature, the developer using the control does not have to guess the effect that the value change has on the control because she receives a visual indication as the property value runs the full gamut of its available range. Granted, not all controls benefit from this type of functionality, but for those that do, it can be a tremendous help and alleviate some of the frustration of property value editing that might occur during design time.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net