這里的自定義控件是由普通控件組合而成的。
希望事件響應代碼推遲到使用自定義控件的窗體里寫。
步驟一:新建一個用戶控件,放兩個按鈕,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()); // 把按鈕自身作為參數傳遞
}
}
}
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());
}
{
Button btn = sender as Button;
MessageBox.Show(btn.Tag.ToString());
}
這個操作很有用。
url: http://greatverve.cnblogs.com/archive/2012/02/15/csharp-usercontrol-event.html
c# 自定義控件如何在屬性欄添加自定義事件?可以雙擊生成+=代碼?
url: http://greatverve.cnblogs.com/archive/2012/02/15/csharp-usercontrol-event.html
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;//定義事件
public delegate void OnSubBureauSelectChanged();//定義委托
public event OnSubBureauSelectChanged onSubBureauSelectChanged;//定義事件
//以下代碼放在你要用在窗體中調用的事件中,可以是控件中有的也可以自己寫的
if ( ( subBureaus.Count > 0 ) && ( onSubBureauSelectChanged != null ) )
onSubBureauSelectChanged ();
onSubBureauSelectChanged ();
//以下寫在窗體構造中
searchPanel.onSubBureauSelectChanged += new SearchPanel.OnSubBureauSelectChanged ( OnSubBureauSelectChanged );
//以下再寫一個自己寫的事件
private void OnSubBureauSelectChanged ( )
{這樣就可以了}
{這樣就可以了}