C#---多選和單選控件
1、控件介紹
CheckBox:設(shè)置默認(rèn)選中:checked屬性設(shè)為true。
RadioButton:設(shè)置默認(rèn)選中:checked屬性設(shè)為true。
給控件分組:使用容器中的“GroupBox”控件。
2、實例演示
(1)新建一個C#---WINFORM程序。

(2)其代碼為:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace 單選和多選控件{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } PRivate void b_Login_Click(object sender, EventArgs e) { if (rad_stu.Checked||rad_teh.Checked) { string name = txt_name.Text.Trim(); string pwd = txt_pwd.Text; if (rad_stu.Checked) //學(xué)生被選中 { if (name == "student" && pwd == "student") { MessageBox.Show("學(xué)生登錄成功!"); } else { MessageBox.Show("登錄失??!"); txt_name.Clear(); //清空用戶名文本框 txt_pwd.Clear(); //清空密碼文本框 txt_name.Focus(); //用戶名文本框獲得輸入光標(biāo)焦點 } } else //選擇老師 { if (name == "teacher" && pwd == "teacher") { MessageBox.Show("老師登錄成功!"); } else { MessageBox.Show("登錄失敗!"); txt_name.Clear(); //清空用戶名文本框 txt_pwd.Clear(); //清空密碼文本框 txt_name.Focus(); //用戶名文本框獲得輸入光標(biāo)焦點 } } } else //都沒有選中 { MessageBox.Show("請先選擇學(xué)生或者老師"); } } }}運行結(jié)果


新聞熱點
疑難解答