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;
}
再聲明兩個委託,它們是對EventLoginArgs和EventArgs對象中的資訊的封裝,如下:
public delegate void UserLoginEventHandler(object sender,EventLoginArgs e);
public delegate void CancelEventHandler(object sender,EventArgs e);
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!
}
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 = "";
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";
}
這兩個例子的完整代碼可以點擊這裡下載.
讀者可以參考學習,也可直接使用此組件,但使用時應當以Microsoft SQL Server 作為後台資料庫,所用到的使用者表格應有 UserID,UserName,UserPWD三列,同時在客戶程式中應對有關參數初始化,SubmitLogin事件傳回值是嘗試次數intLoginTime和驗證是否成功bLogin,可參考擴充例子二。