The following code implements a UI type editor for the Value property of the FlashTrackBar control in the Win Forms Control Sample. To download the source code, see the Win Forms Quickstart.
namespace SampleControlPack { using System; using System.ComponentModel; using System.ComponentModel.Design; using System.Core; using System.Diagnostics; using System.Drawing; using System.Drawing. Drawing2D; using System.Drawing.Design; using System.WinForms; using System.WinForms.ComponentModel; using System.WinForms.Design; // // <doc> // <desc> // This is a UI Type Editor for the value property of the // FlashTrackBar control. // A UI Type Editor provides custom UI in the property browser // for any property it is connected to. // Here, you will provide a custom UI that consists of a FlashTrackBar // so that the user may visually edit the value of the track bar. // </desc> // </doc> // public class FlashTrackBarValueEditor : UITypeEditor { private IWinFormsEditorService edSvc = null; // // <doc> // <desc> // This is a protected method that is used to set up the // properties of the flash track bar you // will display as the custom user interface. // </desc> // </doc> // protected virtual void SetEditorProps(FlashTrackBar editingInstance, FlashTrackBar editor) { editor.ShowValue = true; editor.StartColor = Color.Navy; editor.EndColor = Color.White; editor.ForeColor = Color.White; editor.Min = editingInstance.Min; editor.Max = editingInstance.Max; editor.Height = 23; } // // <doc> // <desc> // This is called by the property browser to display the custom // user interface for editing the value. // Here you can simply display your own editor, or you can ask the // given service object provider for an editing service. // You will choose the latter so that you // can provide a combo box like drop-down to the user. // </desc> // </doc> // public override object EditValue(ITypeDescriptorContext context, IServiceObjectProvider provider, object value) { if (context != null && context.Instance != null && provider != null) { edSvc = (IWinFormsEditorService)provider.GetServiceObject(typeof(IWinFormsEditorService)); if (edSvc != null) { FlashTrackBar trackBar = new FlashTrackBar(); trackBar.AddOnValueChanged(new EventHandler(this.ValueChanged)); SetEditorProps((FlashTrackBar)context.Instance, trackBar); bool asInt = true; if (value is int) { trackBar.Value = (int)value; } else if (value is byte) { asInt = false; trackBar.Value = (byte)value; } edSvc.DropDownControl(trackBar); if (asInt) { value = trackBar.Value; } else { value = (byte)trackBar.Value; } } } return value; } // // <doc> // <desc> // This is called by the property browser so it knows what type of // editing support to expect. // You have provided a custom drop down box. // </desc> // </doc> // public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { if (context != null && context.Instance != null) { return UITypeEditorEditStyle.DropDown; } return base.GetEditStyle(context); } // // <doc> // <desc> // This is an event handler that is connected to the ValueChanged // event of the flash track bar. When the value changes, // you want to commit the value, so // you tell the editing service to close the drop down box. // </desc> // </doc> // private void ValueChanged(object sender, EventArgs e) { if (edSvc != null) { edSvc.CloseDropDown(); } } } }
The following code shows a UI type editor for the DarkenBy
property of the FlashTrackBar control. This UI editor extends the FlashTrackBarValueEditor
implemented above.
[C#] namespace SampleControlPack { using System; using System.ComponentModel; using System.ComponentModel.Design; using System.Core; using System.Diagnostics; using System.Drawing; using System.Drawing.Advanced; using System.Drawing.Design; using System.WinForms; using System.WinForms.ComponentModel; using System.WinForms.Design; // // <doc> // <desc> //This is a UI type editor that is used for the DarkenBy property. // It inherits from the default UI Type Editor for the Value property // of FlashTrackBar, and then modifies the min and max values of the // track bar. // </desc> // </doc> // public class FlashTrackBarDarkenByEditor : FlashTrackBarValueEditor { protected override void SetEditorProps(FlashTrackBar editingInstance, FlashTrackBar editor) { base.SetEditorProps(editingInstance, editor); editor.Min = 0; editor.Max = byte.MaxValue; } } }