当前位置:编程学习 > asp >>

.net WinForm用户控件开发--(6)用户控件弹出式属性设置

 这一节给大家演示下怎样使属性值以弹出式对话框的形式显示出来,先来看下效果图.

      \

    这里我们定义一个用户控件,并为用户控件设置一个属性,使用弹出式对话框为属性设置值。

    定义属性ShowPropery

   代码如下

  

[csharp]
public partial class UCLab : UserControl 
    { 
        public UCLab() 
        { 
            InitializeComponent(); 
        } 
 
        private string showpropery; 
        [Description("弹出属性")] 
        [Editor(typeof(ShowTypeDialogEditor),typeof(UITypeEditor))] 
        public string ShowPropery 
        { 
            get 
            { 
                return showpropery; 
            } 
            set 
            { 
                showpropery = value; 
            } 
        } 

public partial class UCLab : UserControl
    {
        public UCLab()
        {
            InitializeComponent();
        }

        private string showpropery;
        [Description("弹出属性")]
        [Editor(typeof(ShowTypeDialogEditor),typeof(UITypeEditor))]
        public string ShowPropery
        {
            get
            {
                return showpropery;
            }
            set
            {
                showpropery = value;
            }
        }
[csharp] view plaincopyprint?} 

}
   然后我们为属性设置弹出式属性编辑器,需要继承UITypeEditor类,代码如下

 

[csharp]
/// <summary>  
   /// 弹出式编辑器  
   /// </summary>  
   public class ShowTypeDialogEditor : UITypeEditor 
   { 
 
       public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) 
       { 
           if (context!=null&&context.Instance!=null) 
           { 
               return UITypeEditorEditStyle.Modal;//显示一个省略号  
           } 
           return base.GetEditStyle(context); 
       } 
 
       public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) 
       { 
           System.Windows.Forms.Design.IWindowsFormsEditorService editorService = null; 
           if (context!=null&&context.Instance!=null&&provider!=null) 
           { 
               editorService =(System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService)); 
               if (editorService!=null) 
               { 
                   UCLab uclab =(UCLab)context.Instance; 
                   ShowForm sf = new ShowForm(uclab.ShowPropery); 
                   if (sf.ShowDialog()==DialogResult.OK) 
                   { 
                          value = sf.Result; 
                           return value; 
                   } 
               } 
           } 
           //return base.EditValue(context, provider, value);  
               return value; 
       } 
   } 

 /// <summary>
    /// 弹出式编辑器
    /// </summary>
    public class ShowTypeDialogEditor : UITypeEditor
    {

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context!=null&&context.Instance!=null)
            {
     &nbs

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,