用户控件使用控件编辑其值(使用DropDownControl(ctl)方法)如何向ctl赋初始值?
我想写一个用户控件,这个控件有一个属性如下:
[Description("设置X的位移量"), DefaultValue(50), Editor(typeof(UITypeEditorOffset), typeof(System.Drawing.Design.UITypeEditor))]
public int OffetX
{
get{return this._OffsetX;}
set {
this._OffsetX = value;
this.Refresh();
}
}
其中类UITypeEditorOffset代码如下:
public class UITypeEditorOffset : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if (context != null && context.Instance != null)
{
return UITypeEditorEditStyle.DropDown;
}
return base.GetEditStyle(context);
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService iService = null
if (context != null && context.Instance != null && provider != null)
{
iService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (iService != null)
{
System.Windows.Forms.TrackBar trkOffset = new System.Windows.Forms.TrackBar();
// 在这里如何初始化 trkOffset 的 Value 属性呢? 也就是说如何将上面的 OffetX 传递过来呢?
trkOffset.ValueChanged += new System.EventHandler(this.trkOffset_ValueChanged);
iService.DropDownControl(trk);
// 如何让 trkOffset 在滑块滑动时就返回值呢?
}
}
return value;
}
private void trkOffset_ValueChanged(Object sender, System.EventArgs e)
{
..........
}
}
我想用 System.Windows.Forms.TrackBar 这个控件来编辑OffsetX属性值,每次点击属性浏览器中该属性右侧向下箭头的时候会显示TrackBar控件,但是每次显示TrackBar的时候如何让TrackBar的Value值为上次编辑完毕的值呢(也就是如何初始TrackBar的Value值)?还有当TrackBar滑动滑块时如何让其当前值立即返回(不是等到TrackBar控件消失才返回)? 我的控件是想非常直观的显示效果(每次OffsetX值改变都会重新绘制自身外观)。
希望高手指点迷津,万分感谢!
------解决方案--------------------
参考如下:
C# code
public class UITypeEditorOffset : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if (context != null && context.Instance != null)
{
return UITypeEditorEditStyle.DropDown;
}
return base.GetEditStyle(context);
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService iService = null;
if (context != null && context.Instance != null && provider != null)
{
iService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (iService != null)
{
[color=#FF0000]用户控件[/color] control = ([color=#FF0000]用户控件[/color])context.Instance;
TrackBar trkOffset = new TrackBar();
trkOffset.Value = control.OffetX;
iService.DropDownControl(trkOffset);
return trkOffset.Value;
}
}
return value;
}
}
------解决方案--------------------