📄 9.5.txt
字号:
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 )
percent = value;
Invalidate();
}
}
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -