C#繼承基本控件實現自定義控件


自定義控件分三類:
1.復合控件:基本控件組合而成。繼承自UserControl
2.擴展控件:繼承基本控件,擴展一些屬性與事件。比如繼承Button
3.自定義控件:直接繼承自Control
第一種情況上手比較容易,也比較常用,其中也有不少技巧,慢慢總結。
比如要單獨建個類庫項目,才不會引起沖突。
怎樣把事件代碼推遲到使用者。
今天把擴展控件簡單入門。
------------------------------------------------------------------
步驟一:這里首先要建一個Windows控件庫項目。
步驟二:新建用戶控件,修改代碼(注意注釋掉.Designer.cs文件中的代碼)
擴展Button
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinFormControlLibrary
{
     public  partial  class UcButton : Button
    {
         public UcButton()
        {
            InitializeComponent();
        }
         //  Creates the private variable that will store the value of your 
        
//  property.
         private  int varValue;
         //  Declares the property.
         public  int ButtonValue
        {
             //  Sets the method for retrieving the value of your property.
             get
            {
                 return varValue;
            }
             //  Sets the method for setting the value of your property.
             set
            {
                varValue = value;
            }
        }
    }
}
修改.Desinger.cs
namespace WinFormControlLibrary
{
     partial  class UcButton
    {
         ///   <summary>  
        
///  Required designer variable.
        
///   </summary>
         private System.ComponentModel.IContainer components =  null;

         ///   <summary>  
        
///  Clean up any resources being used.
        
///   </summary>
        
///   <param name="disposing"> true if managed resources should be disposed; otherwise, false. </param>
         protected  override  void Dispose( bool disposing)
        {
             if (disposing && (components !=  null))
            {
                components.Dispose();
            }
             base.Dispose(disposing);
        }

         #region Component Designer generated code

         ///   <summary>  
        
///  Required method for Designer support - do not modify 
        
///  the contents of this method with the code editor.
        
///   </summary>
         private  void InitializeComponent()
        {
            components =  new System.ComponentModel.Container();
             // 把這句注釋掉
            
// this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }

         #endregion
    }
}
擴展Label
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinFormControlLibrary
{
     public  partial  class UcLabel : Label
    {
         public UcLabel()
        {
            InitializeComponent();
        }
         protected  override  void OnMouseEnter(EventArgs e)
        {
             base.OnMouseEnter(e);
             this.Font =  new Font( " 宋體 ", 10F, FontStyle.Underline);
        }
         protected  override  void OnMouseLeave(System.EventArgs e)
        {
             base.OnMouseLeave(e);
             this.Font =  new Font( " 宋體 ", 10F, FontStyle.Regular);
        }
    }
}
步驟三:在其他Windows窗體項目中添加項目引用。編譯之后就在工具箱看到生成的自定義控件。
url: http://greatverve.cnblogs.com/archive/2012/02/16/user-control-Inherit.html
參考msdn:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM