c# 自定義控件如何在屬性欄添加自定義事件

用戶控件的實現比較簡單,直接從System.Windows.Forms.UserControl繼承。

public class UserControl1 : System.Windows.Forms.UserControl

 

爲了便於測試我在上面添加了一個TextBox,並註冊TextBox的TextChanged事件,

this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

事件處理函數,

private void textBox1_TextChanged(object sender, System.EventArgs e)

{

    MessageBox.Show(this.textBox1.Text);

}

這裏演示如果控件中文本框的內容改變就會用MessageBox顯示當前的文本框內容。

窗體中添加上面的用戶控件,當我們改變textBox的文本時,可以看到跳出一個對話框,很簡單吧。

下面來看看對控件添加屬性。
這裏定義一個私有變量。
private string customValue;
添加訪問他的屬性
public string CustomValue
{
    get{return customValue;}
    set{customValue =value;}
}
在窗體中使用的時候像普通控件一樣進行訪問,
userControl11.CustomValue = "用戶控件自定義數據";
通過事件可以傳遞消息到窗體上,在定義之前我們先來寫一個簡單的參數類。
public class TextChangeEventArgs : EventArgs
{
    private string message;
    public TextChangeEventArgs(string message)
    {
        this.message = message;
    }
public string Message
    {
        get{return message;}
    }
}
定義委託爲,
public delegate void TextBoxChangedHandle(object sender,TextChangeEventArgs e);
接下去在用戶控件中添加事件,
//定義事件
public event TextBoxChangedHandle UserControlValueChanged;
爲了激發用戶控件的新增事件,修改了一下代碼,
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
    if(UserControlValueChanged != null)
        UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));
           
}
好了,爲了便於在Csdn上回答問題,把完整的代碼貼了出來:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
 
namespace ZZ.WindowsApplication1
{
    public class UserControl1 : System.Windows.Forms.UserControl
    {
        private System.Windows.Forms.TextBox textBox1;
        private string customValue;
       
private System.ComponentModel.Container components = null;
 
        public string CustomValue
        {
            get{return customValue;}
            set{customValue =value;}
        }
 
        //定義事件
        public event TextBoxChangedHandle UserControlValueChanged;
 
        public UserControl1()
        {
            InitializeComponent();
        }
 
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
 
        #region組件設計器生成的代碼
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            this.textBox1.Location = new System.Drawing.Point(12, 36);
            this.textBox1.Name = "textBox1";
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "textBox1";
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            this.Controls.Add(this.textBox1);
            this.Name = "UserControl1";
            this.Size = new System.Drawing.Size(150, 92);
            this.ResumeLayout(false);
 
        }
        #endregion
 
        private void textBox1_TextChanged(object sender, System.EventArgs e)
        {
            if(UserControlValueChanged != null)
                UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));
           
        }
    }
    //定義委託
    public delegate void TextBoxChangedHandle(object sender,TextChangeEventArgs e);
 
    public class TextChangeEventArgs : EventArgs
    {
        private string message;
        public TextChangeEventArgs(string message)
        {
            this.message = message;
        }
        public string Message
        {
            get{return message;}
        }
    }
}
使用時要在窗體中註冊上面的事件,比較簡單都貼源代碼了,
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
 
namespace ZZ.WindowsApplication1
{
    public class Form1 : System.Windows.Forms.Form
    {
        private WindowsApplication1.UserControl1 userControl11;
        private System.ComponentModel.Container components = null;
 
        public Form1()
        {
            InitializeComponent();
            userControl11.CustomValue = "用戶控件自定義數據";
            userControl11.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);
        }
 
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
 
        #region Windows 窗體設計器生成的代碼
        private void InitializeComponent()
        {
            this.userControl11 = new WindowsApplication1.UserControl1();
            this.SuspendLayout();
            this.userControl11.Location = new System.Drawing.Point(8, 8);
            this.userControl11.Name = "userControl11";
            this.userControl11.Size = new System.Drawing.Size(150, 84);
            this.userControl11.TabIndex = 0;
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(292, 193);
            this.Controls.Add(this.userControl11);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
 
        }
        #endregion
 
         [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
 
        private void userControl11_UserControlValueChanged(object sender, TextChangeEventArgs e)
        {
            MessageBox.Show("當前控件的值爲:" + e.Message);
        }
    }
}
另外需要動態加載,就把控件添加在容器的Controls集合就行了,下面是在構造函數中添加控件,
public Form1()
{
    InitializeComponent();
    UserControl1 uc = new UserControl1();
    uc.CustomValue = "動態加載的用戶控件";
    uc.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);
    this.Controls.Add(uc);
}
另外從VS.net中的工具箱中拖動用戶控件到窗體上,如果是第一次需要編譯一下項目。
 
//如果我有一個寫好的控件,想在Form中使用如何???????
在控件中:  
        public delegate void OnSubBureauSelectChanged();//定義委託
        public event OnSubBureauSelectChanged onSubBureauSelectChanged;//定義事件
//以下代碼放在你要用在窗體中調用的事件中,可以是控件中有的也可以自己寫的
 if ( ( subBureaus.Count > 0 ) && ( onSubBureauSelectChanged != null ) )
                onSubBureauSelectChanged ();
//以下寫在窗體構造中
searchPanel.onSubBureauSelectChanged += new SearchPanel.OnSubBureauSelectChanged ( OnSubBureauSelectChanged );
//以下再寫一個自己寫的事件
 private void OnSubBureauSelectChanged ( )
{這樣就可以了}
發佈了44 篇原創文章 · 獲贊 4 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章