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

.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);
      &n

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,