天天看點

為C#自定義控件添加自定義事件

為C#自定義控件添加自定義事件

這裡的自定義控件是由普通控件組合而成的。

希望事件響應代碼推遲到使用自定義控件的窗體裡寫。

步驟一:建立一個使用者控件,放兩個按鈕,Tag分别是btn1,btn2.

這兩個按鈕的共用單擊事件處理代碼如下:

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 UcDll
{
    public partial class UcTest : UserControl
    {
        public UcTest()
        {
            InitializeComponent();
        }

        //定義委托
        public delegate void BtnClickHandle(object sender, EventArgs e);
        //定義事件
        public event BtnClickHandle UserControlBtnClicked;
        private void btn_Click(object sender, EventArgs e)
        {
            if (UserControlBtnClicked != null)
                UserControlBtnClicked(sender, new EventArgs());//把按鈕自身作為參數傳遞
        }
    }
}      

步驟二:當使用者拖一個自定義控件在窗體的時候,

在事件裡可以找到UserControlBtnClicked事件。

private void ucTest1_UserControlBtnClicked(object sender, EventArgs e)
{
    Button btn = sender as Button;
    MessageBox.Show(btn.Tag.ToString());
}      

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 += newTextBoxChangedHandle(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 ( )

        {這樣就可以了}

你們的評論、回報,及對你們有所用,是我整理材料和博文寫作的最大的鼓勵和唯一動力。歡迎讨論和關注!

沒有整理與歸納的知識,一文不值!高度概括與梳理的知識,才是自己真正的知識與技能。

永遠不要讓自己的自由、好奇、充滿創造力的想法被現實的架構所束縛,讓創造力自由成長吧!

多花時間,關心他(她)人,正如别人所關心你的。理想的騰飛與實作,沒有别人的支援與幫助,是萬萬不能的。

繼續閱讀