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

.net WinForm用户控件开发--(5)用户控件复杂属性设置

  这一节,大家共同学习下,用户控件的自定义的复杂的属性设置,我们这里自定义一个用户控件和自定义一个属性。

   本节重点:

      1.怎样定义复杂属性

      2.复杂属性和基本类型相互转换  

 1.第一步, 先来自定义一个类,代码如下

   

[csharp]
/// <summary>  
   /// 自定义属性类  
   /// </summary>  
   public class CustomAttri 
   { 
 
       public CustomAttri(int width, int height) 
       { 
           _width = width; 
           _height = height; 
       } 
 
       public CustomAttri() 
       {  
        
       } 
       private int _width; 
       private int _height; 
 
       /// <summary>  
       /// 宽度  
       /// </summary>  
       public int Width 
       { 
           get 
           { return _width; } 
           set 
           { 
               _width = value; 
           } 
       } 
 
       /// <summary>  
       /// 高度  
       /// </summary>  
       public int Height 
       { 
           get 
           { 
               return _height; 
           } 
           set 
           { 
               _height = value; 
           } 
       } 
   } 

 /// <summary>
    /// 自定义属性类
    /// </summary>
    public class CustomAttri
    {

        public CustomAttri(int width, int height)
        {
            _width = width;
            _height = height;
        }

        public CustomAttri()
        {
       
        }
        private int _width;
        private int _height;

        /// <summary>
        /// 宽度
        /// </summary>
        public int Width
        {
            get
            { return _width; }
            set
            {
                _width = value;
            }
        }

        /// <summary>
        /// 高度
        /// </summary>
        public int Height
        {
            get
            {
                return _height;
            }
            set
            {
                _height = value;
            }
        }
    }
  然后自定义一个控件,并且定义一属性SecondSize,代码如下

  

[csharp]
public partial class UCPanel : Control 
    { 
        public UCPanel() 
        { 
            InitializeComponent(); 
        } 
 
      private CustomAttri _sSize=new CustomAttri(20,30);; 
        /// <summary>  
        /// 定义自定义属性  
        /// </summary>  
        [Description("第二尺寸")] 
        [Category("尺寸")] 
        public CustomAttri SecondSize 
        {  
          get 
          { 
            return _sSize; 
          } 
         set 
            { 
              _sSize=value; 
      

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