- 功能
单选用控件RadioButton
多选用控件CheckBox
CheckBox:设置默认选中:checked属性设为true
RadioButton:设置默认选中:checked属性设为true。
给控件分组:使用容器中的“GroupBox”控件。
2.学生老师登录小练习
(1)新建窗体
(2)代码
namespace _07老师或者学生登陆
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (rdoStudent.Checked || rdoTeacher.Checked)
{
string name = txtName.Text.Trim();
string pwd = txtPwd.Text;
if (rdoStudent.Checked)
{
if (name == "student" && pwd == "student")
{
MessageBox.Show("学生登陆成功");
}
else
{
MessageBox.Show("登陆失败");
txtName.Clear();
txtPwd.Clear();
txtName.Focus();
}
}
else//选择的是老师
{
if (name == "teacher" && pwd == "teacher")
{
MessageBox.Show("老师登陆成功");
}
else
{
MessageBox.Show("登陆失败");
txtName.Clear();
txtPwd.Clear();
txtName.Focus();
}
}
}
else//都没有选中
{
MessageBox.Show("请首先选择登陆身份");
}
}
}
(3)结果