I have a C# application with two forms(form1,form2).In form1 on btnTransfer_click I open second form.
private void btnTransfer_Click(object sender, EventArgs e)
{
Form2 frmConn = new Form2 ;
frmConn .Show();
//i need here values from second form
}
In second form I have 2 textboxes (txtUser,txtPassword) and a button (btnOk) .On button btnOk I verify user and password and if are correct i have to come back to first form and get this values on click button.
In Form2 :
private void btnOk_Click(object sender, EventArgs e)
{
//verify if txtUser and txtPassword are correct
//if are corrects i have to send back to first form
this.Close();
}
How can I do This? Thanks!
In the class for form2
, create two public
class properties, one for the value of each textbox:
private String _username = null;
public String UserName { get { return _username; } }
private String _password = null;
public String Password { get { return _password; } }
In form2
you can verify and assign to properties:
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();
}
Then you can retrieve them in your form1
code as such:
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!
}
}