单选radioButton可以用GroupBox容器分组。

    属性:
    Checked:指示组件是否处于选中状态。

    登录界面:
    image.png

    1. public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. }
    7. private void label1_Click(object sender, EventArgs e)
    8. {
    9. }
    10. private void label2_Click(object sender, EventArgs e)
    11. {
    12. }
    13. private void btnLogin_Click(object sender, EventArgs e)
    14. {
    15. string name = txtAccount.Text.Trim();
    16. string pwd = txtPwd.Text;
    17. if (radioButtonStudent.Checked) //选择的是学生
    18. {
    19. if (name== "student" && pwd== "student")
    20. {
    21. MessageBox.Show("登陆成功");
    22. this.Close();
    23. }
    24. else
    25. {
    26. MessageBox.Show("账户或密码错误");
    27. txtAccount.Clear();
    28. txtPwd.Clear();
    29. txtAccount.Focus();
    30. }
    31. }
    32. else if(radioButtonTeacher.Checked) //选择的是老师
    33. {
    34. if (name == "teacher" && pwd == "teacher")
    35. {
    36. MessageBox.Show("登陆成功");
    37. this.Close();
    38. }
    39. else
    40. {
    41. MessageBox.Show("账户或密码错误");
    42. txtAccount.Clear();
    43. txtPwd.Clear();
    44. txtAccount.Focus();
    45. }
    46. }
    47. else
    48. {
    49. MessageBox.Show("请选择是学生还是老师!");
    50. }
    51. }
    52. private void btnReset_Click(object sender, EventArgs e)
    53. {
    54. txtAccount.Clear();
    55. txtPwd.Clear();
    56. txtAccount.Focus();
    57. }
    58. private void btnQuit_Click(object sender, EventArgs e)
    59. {
    60. this.Close();
    61. }
    62. }