C#登录到主窗体上的加载

时间:2016-10-22 14:54:02

标签: c#

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

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");


    }

3 个答案:

答案 0 :(得分:0)

如果登录成功,请先隐藏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");
        }

    }

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

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");
        }

    }