欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net WinForm用户控件开发--(7)用户控件下拉式属性设置

程序员文章站 2022-05-15 15:15:38
  这一节我们共同学习下用户控件的下拉式属性设置,可以为用户控件设置属性以下拉框的形式显示出来,效果图如下        &nb...

  这一节我们共同学习下用户控件的下拉式属性设置,可以为用户控件设置属性以下拉框的形式显示出来,效果图如下

     .net WinForm用户控件开发--(7)用户控件下拉式属性设置

   这里我们定义一个用户控件,设置一个属性dropdownpropery

   代码如下

   

[csharp]
public partial class uclab : usercontrol 
  { 
      public uclab() 
      { 
          initializecomponent(); 
      } 
 
      
 
      private string dropdownpropery="hello"; 
      [description("下拉属性")] 
      [editor(typeof(droptypedialogeditor),typeof(uitypeeditor))] 
      public string dropdownpropery 
      { 
          get 
          { 
              return dropdownpropery; 
          } 
          set 
          { 
              dropdownpropery = value; 
          } 
      } 
 
      
  } 

  public partial class uclab : usercontrol
    {
        public uclab()
        {
            initializecomponent();
        }

      

        private string dropdownpropery="hello";
        [description("下拉属性")]
        [editor(typeof(droptypedialogeditor),typeof(uitypeeditor))]
        public string dropdownpropery
        {
            get
            {
                return dropdownpropery;
            }
            set
            {
                dropdownpropery = value;
            }
        }

      
    }
    接着我们该属性设置一个下拉式编辑器,代码如下

     

[csharp]
/// <summary>  
   /// 下拉式编辑器  
   /// </summary>  
   public class droptypedialogeditor: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) 
       { 
           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 lab =(uclab)context.instance; 
                   ucdrop drop = new ucdrop(lab.dropdownpropery); 
                   editorservice.dropdowncontrol(drop); 
                   value = drop.result; 
                   return value; 
               } 
 
           } 
           //return base.editvalue(context, provider, value);  
           return value; 
       } 
    
   } 

 /// <summary>
    /// 下拉式编辑器
    /// </summary>
    public class droptypedialogeditor: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)
        {
            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 lab =(uclab)context.instance;
                    ucdrop drop = new ucdrop(lab.dropdownpropery);
                    editorservice.dropdowncontrol(drop);
                    value = drop.result;
                    return value;
                }

            }
            //return base.editvalue(context, provider, value);
            return value;
        }
   
    }
   以上代码就可以实现,当我们把用户控件拖动到窗体上,设置该属性,就可以以下拉式窗体显示。