public class EventLoginArgs:System.EventArgs { public string strUserID; public string strUserName; public string strUserPWD; public bool bVaild; public EventLoginArgs(string userID,string userName,string userPWD) { strUserID = userID; strUserName = userName; strUserPWD = userPWD; } |
public delegate void UserLoginEventHandler(object sender,EventLoginArgs e); public delegate void CancelEventHandler(object sender,EventArgs e); |
public event UserLoginEventHandler SubmitLogin; public event CancelEventHandler Cancel; protected virtual void OnSubmitLogin(EventLoginArgs e) { if(this.SubmitLogin!=null) { SubmitLogin(this,e); } } protected virtual void OnCancel(EventArgs e) { if(this.Cancel!=null) { Cancel(this,e); } |
其实SubmitLogin 是UserLoginEventHandler委托的实例,令人费解的是此事件的触发,传递,处理过程如何呢?
精品教程尽在www.xvna.com
在本例中是通过确定按钮来触发submitLogin事件的:
private void btnOK_Click(object sender, System.EventArgs e) { if(txtID.Text != ""&&txtName.Text !=""&&txtPWD.Text !="") { intLoginTime++; OnSubmitLogin(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text)); bLogin = TestUserInDB(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text)); MessageBox.Show("this is the btnOK_click function!","In control",MessageBoxButtons.OK); if(!bLogin) MessageBox.Show("Login in Failed!","Login Error",MessageBoxButtons.OK); } else { MessageBox.Show("Your must input all the items!","Login Info",MessageBoxButtons.OK); } } |
private void userControl1_SubmitLogin(object sender, Userlogin.EventLoginArgs e) { MessageBox.Show("This is in test form!"+ userControl1.bLogin +"\ns Login times is "+userControl1.intLoginTime +"\ne's strUserID="+e.strUserID,"Test",MessageBoxButtons.OK); } |
This is in test form! this is the process in DB this is the btnOK_click function! |
public delegate void UserLoginEventHandler(object sender,EventLoginArgs e); public delegate void CancelEventHandler(object sender,EventArgs e); public event UserLoginEventHandler SubmitLogin; public event CancelEventHandler Cancel; protected virtual void OnSubmitLogin(EventLoginArgs e) { if(this.SubmitLogin!=null) { SubmitLogin(this,e); } } protected virtual void OnCancel(EventArgs e) { if(this.Cancel!=null) Cancel(this,e); } public string Server { } public string DataBase { } public string TableSet { } public string UserForDB { } public string PWDForDB { } public bool TestUserInDB(EventLoginArgs e) { //MessageBox.Show("this is the process for DB!","TestUserInDB",MessageBoxButtons.OK); bool bOK = false; if(this.strDataBase!=null && this.strServer!=null && this.strUserForDB!=null) { if(this.strPWDForDB==null) this.strPWDForDB = ""; string strConnection = "server="+this.strServer +";database="+this.strDataBase +";UID="+this.strUserForDB +";PWD="+this.strPWDForDB; string strSQL = "select UserID,UserName,UserPWD from "+this.strTableSet+" where UserID='"+e.strUserID+"' and UserName='"+e.strUserName +"' and UserPWD='"+e.strUserPWD+"'"; SqlConnection conn = new SqlConnection(strConnection); try { conn.Open(); } catch(SqlException ex) { MessageBox.Show("数据库不能打开!请检查有关参数.","Error",MessageBoxButtons.OK); return false; } SqlDataAdapter da = new SqlDataAdapter(strSQL,conn); DataSet ds = new DataSet(); try { da.Fill(ds,this.strTableSet); } catch(SqlException ex) { ...... } foreach(DataRow row in ds.Tables[this.strTableSet].Rows) { if(row != null) { bOK = true; } } ....... } else { bOK = false; } return bOK; } private void btnOK_Click(object sender, System.EventArgs e) { if(txtID.Text != ""&&txtName.Text !=""&&txtPWD.Text !="") { intLoginTime++; bLogin = TestUserInDB(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text)); if(!bLogin) MessageBox.Show("Login in Failed!","Login Error",MessageBoxButtons.OK); else OnSubmitLogin(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text)); } else { MessageBox.Show("Your must input all the items!","Login Info",MessageBoxButtons.OK); } } private void btnCancel_Click(object sender, System.EventArgs e) { OnCancel(e); } private void UserControl_Load(object sender, System.EventArgs e) { intLoginTime = 0; } } public class EventLoginArgs:System.EventArgs { public string strUserID; public string strUserName; public string strUserPWD; public bool bVaild; public EventLoginArgs(string userID,string userName,string userPWD) { strUserID = userID; strUserName = userName; strUserPWD = userPWD; } } |
private void userControl1_SubmitLogin(object sender, Userlogin.EventLoginArgs e) { MessageBox.Show("This result is bLogin="+ userControl1.bLogin +" At "+userControl1.intLoginTime +" times \n UserID="+e.strUserID+"\n UserName="+e.strUserName,"TestResult",MessageBoxButtons.OK); } private void Form1_Load(object sender, System.EventArgs e) { userControl1.Server = "localhost"; userControl1.DataBase="weiwen"; userControl1.TableSet = "TestUser"; userControl1.UserForDB="sa"; userControl1.PWDForDB = "sa"; } |