After editing your code, it does not compile again.
Are you sure you posted your real code?
问题在于:
if(textBox1.Text==123)
<代码>TextBox.Text is a string
property,123
is int
.
您不能直接比较<条码>、<条码>和<条码>。
因此,你想要做些什么?
Do you want open the Form1
from Form2
, type username and password on textboxes in Form1
and pass it into Form2
, Right?
EDIT:
Your solution
class FormLogin : Form
{
public string UserName
{
get { return textBoxUserName.Text; }
set { textBoxUserName.Text = value; }
}
public string Password
{
get { return textBoxPassword.Text; }
set { textBoxPassword.Text = value; }
}
public FormLogin()
{
this.Closing += new System.ComponentModel.CancelEventHandler(FormLogin_Closing);
}
void FormLogin_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.DialogResult == DialogResult.Cancel) return;
if (!LoginUser(UserName, Password))
{
MessageBox.Show("Username or password is incorrect");
e.Cancel = true;
}
}
bool LoginUser(string userName, string password)
{
// check login and password in database for example
return true;
}
}
class Form3 :Form
{
void LoginButtonClick()
{
FormLogin formLogin = new FormLogin();
formLogin.UserName = "LastUserName";
//uncomment if you want hide main form
//this.Hide();
if (formLogin.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(string.Format("Congratulation! You are logged as {0}", formLogin.UserName));
}
//this.Show();
}
}
Don t forget add two buttons on FormLogin
: OK with DialogResult
property OK and Cancel with DialogResult
Cancel.