1.call loginform and flipped the MainForm instance in the argument.
2.if login succ in loginform , show MainForm
3.fail - > application.exit();
http://csdp000.org/?p=154
namespace test
{
class LoginForm : Form
{
private Form _mainForm;
private TextBox pwTextBox;
private Button loginButton;
private TextBox idTextBox;
public LoginForm(Form form)
{
InitializeComponent();
_mainForm = form;
}
private void InitializeComponent()
{
this.idTextBox = new System.Windows.Forms.TextBox();
this.pwTextBox = new System.Windows.Forms.TextBox();
this.loginButton = new System.Windows.Forms.Button();
this.SuspendLayout();
this.idTextBox.Location = new System.Drawing.Point(12, 12);
this.idTextBox.Name = "idTextBox";
this.idTextBox.Size = new System.Drawing.Size(159, 21);
this.idTextBox.TabIndex = 0;
this.pwTextBox.Location = new System.Drawing.Point(12, 39);
this.pwTextBox.Name = "pwTextBox";
this.pwTextBox.Size = new System.Drawing.Size(159, 21);
this.pwTextBox.TabIndex = 1;
this.loginButton.Location = new System.Drawing.Point(177, 10);
this.loginButton.Name = "loginButton";
this.loginButton.Size = new System.Drawing.Size(121, 50);
this.loginButton.TabIndex = 2;
this.loginButton.Text = "Login";
this.loginButton.UseVisualStyleBackColor = true;
this.loginButton.Click += new System.EventHandler(this.loginButton_Click);
this.ClientSize = new System.Drawing.Size(310, 70);
this.Controls.Add(this.loginButton);
this.Controls.Add(this.pwTextBox);
this.Controls.Add(this.idTextBox);
this.Name = "LoginForm";
this.Text = "Login";
this.ResumeLayout(false);
this.PerformLayout();
}
private void loginButton_Click(object sender, EventArgs e)
{
if (idTextBox.Text == "abcd" && pwTextBox.Text == "abcd")
{
MessageBox.Show("Login");
this.Hide();
_mainForm.Show();
}
else MessageBox.Show("Fail");
}
}
}
namespace test
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LoginForm(new Form1()));
}
}
}