繁体   English   中英

Visual C# 如果文本框等于 1 打开表单 1 如果文本框等于 2 打开表单 2

[英]Visual C# If text box equals 1 open form 1 if text box equals 2 open form 2

我为我在学校的工作创建了一个登录页面,但我有点卡住了,我需要它,以便如果学生登录它会显示学生表格,如果教师登录它会显示教师表格。

        private void btnSignIn_Click(object sender, EventArgs e)
    {
        if (txtUser.Text == "Student" & txtPass.Text == "GPSC1" || txtUser.Text == "Staff" & txtPass.Text == "GPSC2")

            MessageBox.Show("Success");

        else
            MessageBox.Show("Error");

        if (txtUser.Text == "Student" & txtPass.Text == "GPSC1")
            this.Hide();
        StudentForm studentForm = new StudentForm();
        studentForm.ShowDialog();
        this.Close();

        if (txtUser.Text == "Staff" & txtPass.Text == "GPSC2")
            this.Hide();
        TeacherForm teacherForm = new TeacherForm();
        teacherForm.ShowDialog();
        this.Close();


    }

您应该添加大括号来创建一个新的作用域,这样您就可以在一个if语句中执行多个语句。

例如:

private void btnSignIn_Click(object sender, EventArgs e)
{
    if (txtUser.Text == "Student" && txtPass.Text == "GPSC1")
    //                            ^^  use &&   (& suits better for bitwise)
    {
        MessageBox.Show("Success");
        this.Hide();
        StudentForm studentForm = new StudentForm();
        studentForm.ShowDialog();
        this.Close();
    }
    else
        if(txtUser.Text == "Staff" && txtPass.Text == "GPSC2")
        {
            MessageBox.Show("Success");
            this.Hide();
            TeacherForm teacherForm = new TeacherForm();
            teacherForm.ShowDialog();
            this.Close();
        }
        else
            MessageBox.Show("Error");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM