Why are you trying to pass username and password back to form2? Ideally, the validation should be made in form3, which sould not allow to close if password and passwordre do not match, and should return either
DialogResult.OK
if the username and password match together (sucessful login), or
DialogResult.Cancel
if they do not (wrong username, or wrong password).
Then form2 can use the
DialogResult
of form3 instance to know whether or not login was successful.
Here's a brief "architecture" to put you on a path:
public class Form2 : Form
{
private void LoginButton_Click(object sender, EventArgs e)
{
using (Form3 form = new Form3())
{
if (form.ShowDialog(this) == DialogResult.OK)
{
string username = form.Username;
}
else
{
}
}
}
}
public class Form3 : Form
{
public string Username => UsernameTextBox.Text;
private void TextBox_Validated(object sender, EventArgs e)
{
OkButton.Enabled =
PasswordTextBox.Text == PasswordRepeatTextBox.Text;
}
private void OkButton_Click(object sender, EventArgs e)
{
this.DialogResult =
(IsValidLogin(UsernameTextBox.Text, PasswordTextBox.Text))
? DialogResult.OK
: DialogResult.Cancel;
this.Close();
}
private bool IsValidLogin(string username, string password)
{
}
}
I also have the feeling that you should begin with something simpler than a login system, since you do not seem to have understood the basics of OOP already. For example, you cannot create a new instance of form2 in form3 and expect to get a reference to the already existing form2 instance. When you create a new instance, it is distinct from all other instances of the same class that may have been created so far.
And, as a general advise, start with a pen and paper, and draw a quick diagram of the important steps which have to be followed; this will allow you to write more clear and effective code later on.