当前位置: 移动技术网 > IT编程>开发语言>.net > .net WinForm用户控件开发--(6)用户控件弹出式属性设置

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

2018年12月06日  | 移动技术网IT编程  | 我要评论

电子科学与技术专业就业前景,猛将无双论坛,如履薄冰txt

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

      \

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

    定义属性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)
            {
                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;
        }
    }
   这样我们把用户控件拖到界面上,就可以设置属性了。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网