c#登录加载主表单

  • 本文关键字:表单 加载 登录 c#
  • 更新时间 :
  • 英文 :


我有2个表单,登录(Form2)和主(Form1)表单。我想在输入正确的用户名和密码后加载主表单,同时隐藏/关闭登录表单。

private void button1_Click(object sender, EventArgs e)
    {
        string username = "admin";
        string password = "admin";
        if ((textBox1.Text == username) && (textBox2.Text == password))
            MessageBox.Show("Login Successful");
        else
            MessageBox.Show("Invalid Login");

    }

如果登录成功,首先隐藏Login(Form2)表单,然后显示Main(Form1)表单

private void button1_Click(object sender, EventArgs e)
    {
        string username = "admin";
        string password = "admin";
        if ((textBox1.Text == username) && (textBox2.Text == password))
        {
           this.Visible=false;
           Form1 form1 = new Form1();
           form1.show();
        }
        else
        {
            MessageBox.Show("Invalid Login");
        }
    }
private void LoginButton_Click(object sender, EventArgs e)
    {
        string username = usernameTextBox.Text ;
        string password = passwordTextBox.Text ;
        if (checkDatabaseForValidDetails(username,password))
        {
           this.Visible=false;
           Form1 formObject = new Form1();
           formObject.show();
        }
        else
        {
            MessageBox.Show("Login Failed");
        }
    }

试试这个:

    this.Visible=false;
    Form1 form1 = new Form1();
    form1 .ShowDialog(this);

最新更新