c# - Visual C# 如果文本框等于 1 打开表单 1 如果文本框等于 2 打开表单 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");
}
推荐阅读
- xslt - xslt 多个插入节点(如果不存在)
- c# - Swashbuckle 在 asp.net 核心中失败,出现 NotSupportedException 异常
- jquery - 通过 Media Query 使用 css3 控制我的骨架 HTML5
- node.js - 使用redis发送好友状态
- ionic-framework - 405(不允许的方法),请求的资源上不存在“Access-Control-Allow-Origin”标头
- c# - 如何在 C# asp.net 中从 HTML 字符串生成控件
- java - 我想我需要创建自定义布局,但我不知道该怎么做
- image - 标准化图像的不同方法
- minecraft - 创建改变纹理的块,如 End Portal Frame
- python - 匹配包含所有元音的单词的正则表达式是什么?