单选radioButton可以用GroupBox容器分组。
属性:
Checked:指示组件是否处于选中状态。
登录界面:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void btnLogin_Click(object sender, EventArgs e)
{
string name = txtAccount.Text.Trim();
string pwd = txtPwd.Text;
if (radioButtonStudent.Checked) //选择的是学生
{
if (name== "student" && pwd== "student")
{
MessageBox.Show("登陆成功");
this.Close();
}
else
{
MessageBox.Show("账户或密码错误");
txtAccount.Clear();
txtPwd.Clear();
txtAccount.Focus();
}
}
else if(radioButtonTeacher.Checked) //选择的是老师
{
if (name == "teacher" && pwd == "teacher")
{
MessageBox.Show("登陆成功");
this.Close();
}
else
{
MessageBox.Show("账户或密码错误");
txtAccount.Clear();
txtPwd.Clear();
txtAccount.Focus();
}
}
else
{
MessageBox.Show("请选择是学生还是老师!");
}
}
private void btnReset_Click(object sender, EventArgs e)
{
txtAccount.Clear();
txtPwd.Clear();
txtAccount.Focus();
}
private void btnQuit_Click(object sender, EventArgs e)
{
this.Close();
}
}