1.委託是一種數據類型,就和int 同樣,事件是對象c#
事件只能+= -= 不能直接=賦值,這樣保證了事件的安全,事件內部是用委託來實現的,經過私有委託,和共有方法,來實現+= -=安全
事件和委託在用戶控件中使用的比較多ide
2.下面是一個用戶登陸控件this
模仿 private void button1_Click(object sender, EventArgs e)中的EventArgs spa
在c#中通常會經過相似EventArgs的形式,來傳遞一些數據 code
好比DataGridView中的 DataGridViewCellEventArgs eorm
可得到 e.ColumnIndex 和 e.RowIndex對象
這裏寫了一個UserLoginEventArgs來傳遞登陸的一些信息blog
1 /// <summary> 2 /// 包含用戶登陸控件的事件數據的類 3 /// </summary> 4 public class UserLoginEventArgs : EventArgs 5 { 6 public string LoginId 7 { 8 get; 9 set; 10 } 11 public string LoginPassword 12 { 13 get; 14 set; 15 } 16 /// <summary> 17 /// 是否登陸成功 18 /// </summary> 19 public bool IsOk 20 { 21 get; 22 set; 23 } 24 25 }
這裏定義了一個控件的事件事件
1 /// <summary> 2 /// 定義了一個用戶校驗事件 3 /// </summary> 4 public event Action<object, UserLoginEventArgs> UserLoginValidating; 5 6 private void button1_Click(object sender, EventArgs e) 7 { 8 //在這裏執行用戶的校驗 9 if (UserLoginValidating != null) 10 { 11 UserLoginEventArgs userLoginArgs = new UserLoginEventArgs(); 12 //給用戶控件賦值 13 userLoginArgs.IsOk = false; 14 userLoginArgs.LoginId = txtUid.Text.Trim(); 15 userLoginArgs.LoginPassword = txtPwd.Text.Trim(); 16 //驗證 this 是用戶控件, userLoginArgs裏面包含了數據 17 //由於在引用控件的地方是訪問不到控件裏面的txtUid的值的 18 //因此咱們要經過UserLoginEventArgs把數據傳過去,好在那裏作判斷 登陸是否成功 19 //來給isok賦值 20 UserLoginValidating(this, userLoginArgs); 21 22 if (userLoginArgs.IsOk) 23 { 24 //經過驗證 25 } 26 else 27 { 28 //沒用經過驗證 29 } 30 } 31 32 }
下面就是調用用戶控件
1 private void Form1_Load(object sender, EventArgs e) 2 { 3 //註冊事件 4 ucLogin1.UserLoginValidating += new Action<object, UserLoginEventArgs>(ucLogin1_UserLoginValidating); 5 } 6 7 /// <summary> 8 /// 驗證登陸信息 9 /// </summary> 10 /// <param name="arg1">這裏是控件</param> 11 /// <param name="arg2">控件的一些數據</param> 12 void ucLogin1_UserLoginValidating(object arg1, UserLoginEventArgs arg2) 13 { 14 //驗證 15 if (arg2.LoginId == "admin" && arg2.LoginPassword == "111") 16 { 17 arg2.IsOk = true;//isok默認是false 18 } 19 }
用戶控件 調用的時候 參數怎麼設置成sender e
public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } //這樣生成的事件參數就會是 sender 和 e 了 public delegate void LoginDel<in T1, in T2>(T1 sender, T2 e); public event LoginDel<object, LoginEventArgs> ValidateingTwo; //Action 定義的事件生成的參數是 arg1 arg2 public event Action<object, LoginEventArgs> Validating; private void button1_Click(object sender, EventArgs e) { bool ischeck = checkBox1.Checked; string name = textBox1.Text.Trim(); LoginEventArgs l=new LoginEventArgs (); l.IsCheck=ischeck; l.Name=name; //這個事件的參數是arg1 arg2 Validating(this,l); //這個事件的參數是sender e ValidateingTwo(this,l); if (l.isOk) {// } } } public class LoginEventArgs:EventArgs { public string Name { get; set; } public bool IsCheck { get; set; } public bool isOk { get; set; } }
調用
private void Form1_Load(object sender, EventArgs e) { userControl11.Validating += userControl11_Validating; userControl11.ValidateingTwo += userControl11_ValidateingTwo; } void userControl11_ValidateingTwo(object sender, LoginEventArgs e) { bool ischeck = e.IsCheck; string name = e.Name; } void userControl11_Validating(object arg1, LoginEventArgs arg2) { string name= arg2.Name; bool ischeck = arg2.IsCheck; }