在類form2
,創建兩個public
類屬性,每一個文本框的值:
private String _username = null;
public String UserName { get { return _username; } }
private String _password = null;
public String Password { get { return _password; } }
在form2
您可以驗證並分配給屬性:
private void btnOk_Click(Object sender, EventArgs e)
{
//verify if txtUser and txtPassword are correct
if (correct)
{
_username = txtUser.Text;
_password = txtPassword.Text;
}
this.Close();
}
然後,您可以在form1
代碼中檢索它們,例如:
private void btnTransfer_Click(Object sender, EventArgs e)
{
//This using statement will ensure that you still have an object reference when you return from form2...
using (Form2 frmConn = new Form2())
{
frmConn.Show();
String user = frmConn.UserName;
String pass = frmConn.Password;
if (!String.IsNullOrEmpty(user) && !String.IsNullOrEmpty(pass))
//do something with them, they are valid!
}
}
您的第二個表單只有2個文本框,那麼認證在那裏完成,那麼您想要傳輸到第一個表單的數據是什麼? –