CheckBox(復選框)是 WinForm 中用于實現 “多項選擇” 的控件,允許用戶從一組選項中選擇任意數量的項(包括零項、一項或多項),適用于需要同時選擇多個選項的場景(如愛好、權限設置、功能開關等)。與 RadioButton(單選按鈕)的 “互斥選擇” 不同,CheckBox 各組選項之間相互獨立,選擇狀態互不影響。
CheckBox 控件的核心屬性
CheckBox 的核心特性是 “多選性” 和 “獨立狀態”,其屬性圍繞選擇狀態和外觀展開,常用屬性如下:
屬性名 | 作用描述 |
---|---|
Checked | 布爾值,獲取或設置復選框是否被選中(True 表示選中,基礎狀態屬性)。例如:checkBox1.Checked = true 可默認選中。 |
CheckState | 枚舉值,獲取或設置復選框的狀態(擴展狀態屬性),可選值: - Unchecked :未選中(默認) - Checked :選中 - Indeterminate :不確定(半選狀態,僅當 ThreeState = true 時有效) |
ThreeState | 布爾值,控制是否啟用三態模式(默認 False )。設為 True 時,CheckState 可設為 Indeterminate (半選狀態,通常用于父選項表示 “部分子選項被選中”)。 |
Text | 復選框旁邊顯示的文本(如 “籃球”“足球”“同意條款”)。 |
Appearance | 控制外觀樣式: - Normal :默認樣式(方形復選框 + 文本) - Button :按鈕樣式(選中時呈按下狀態,類似開關) |
AutoCheck | 布爾值,控制點擊時是否自動切換選中狀態(默認 True ,設為 False 需手動處理狀態變化)。 |
Enabled /Visible | 控制控件是否啟用 / 可見(同其他控件)。 |
TextAlign | 文本相對于復選框的對齊方式(如 MiddleRight 文本在右側居中)。 |
CheckBox 控件的常用事件
CheckBox 的事件主要用于響應選擇狀態的變化,常用事件如下:
事件名 | 觸發時機 |
---|---|
CheckedChanged | 當 Checked 屬性值改變時觸發(基礎事件),無論從 True 變為 False 還是相反,都會觸發。 |
CheckStateChanged | 當 CheckState 屬性值改變時觸發(擴展事件,適用于三態模式,涵蓋 Indeterminate 狀態變化)。 |
Click | 點擊復選框時觸發(可能不反映狀態變化,不如狀態事件精準)。 |
CheckBox 控件的典型用法
CheckBox 適用于需要 “多選” 或 “獨立開關” 的場景,常見用法如下:
多項選擇 一組 CheckBox 允許用戶選擇多個選項(如愛好:籃球、足球、游泳、閱讀)。
功能開關 單個 CheckBox 作為功能開關(如 “記住密碼”“自動登錄”“啟用通知”)。
三態選擇(父子關聯) 啟用三態模式(
ThreeState = true
),父 CheckBox 用Indeterminate
狀態表示 “部分子選項被選中”(如文件夾選擇:父文件夾部分子文件被選中)。關聯控件啟用 / 禁用 根據 CheckBox 的選中狀態,動態啟用或禁用其他控件(如選中 “其他” 時,顯示自定義輸入框)。
基礎多選示例(愛好選擇)
按鈕樣式復選框(開關效果)
使用示例:多樣化的 CheckBox 效果
以下代碼演示了 CheckBox 的核心用法,包括基礎多選、三態模式、功能開關及關聯控件等場景:
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; ? namespace CheckBoxDemo {public class CheckBoxExampleForm : Form{// 用于關聯顯示的文本框(選中"其他"時顯示)private TextBox otherTextBox;// 子復選框(用于演示三態父復選框)private List<CheckBox> childCheckboxes = new List<CheckBox>(); ?public CheckBoxExampleForm(){// 窗體基礎設置Text = "CheckBox 控件示例";Size = new Size(500, 450);StartPosition = FormStartPosition.CenterScreen;otherTextBox = new TextBox();Controls.AddRange(CreateCheckboxes()); // 添加所有控件} ?// 創建不同功能的CheckBox控件private Control[] CreateCheckboxes(){List<Control> controls = new List<Control>(); ?// 1. 基礎多選示例(愛好選擇)Label hobbyLabel = new Label{Text = "1. 請選擇愛好(可多選):",Location = new Point(30, 30),AutoSize = true};controls.Add(hobbyLabel); ?// 愛好選項CheckBox basketballCheck = new CheckBox{Text = "籃球",Location = new Point(50, 60),AutoSize = true};CheckBox footballCheck = new CheckBox{Text = "足球",Location = new Point(150, 60),AutoSize = true};CheckBox readingCheck = new CheckBox{Text = "閱讀",Location = new Point(250, 60),AutoSize = true};CheckBox musicCheck = new CheckBox{Text = "音樂",Location = new Point(350, 60),AutoSize = true};CheckBox otherCheck = new CheckBox{Text = "其他",Location = new Point(450, 60),AutoSize = true};controls.AddRange(new[] { basketballCheck, footballCheck, readingCheck, musicCheck, otherCheck }); ?// 關聯的"其他"輸入框otherTextBox = new TextBox{Location = new Point(50, 90),Size = new Size(200, 20),PlaceholderText = "請輸入其他愛好",Visible = false // 初始隱藏};controls.Add(otherTextBox); ?// 綁定"其他"復選框的狀態變化otherCheck.CheckedChanged += (sender, e) =>{otherTextBox.Visible = otherCheck.Checked; // 選中則顯示}; ?// 2. 功能開關示例(系統設置)Label settingLabel = new Label{Text = "2. 系統設置(功能開關):",Location = new Point(30, 130),AutoSize = true};controls.Add(settingLabel); ?CheckBox rememberCheck = new CheckBox{Text = "記住密碼",Location = new Point(50, 160),AutoSize = true};CheckBox autoLoginCheck = new CheckBox{Text = "自動登錄",Location = new Point(200, 160),AutoSize = true};CheckBox notifyCheck = new CheckBox{Text = "啟用消息通知",Location = new Point(350, 160),AutoSize = true,Checked = true // 默認啟用};controls.AddRange(new[] { rememberCheck, autoLoginCheck, notifyCheck }); ?// 3. 三態復選框示例(父子關聯)Label threeStateLabel = new Label{Text = "3. 三態復選框(父子關聯):",Location = new Point(30, 200),AutoSize = true};controls.Add(threeStateLabel); ?// 父復選框(三態)CheckBox parentCheck = new CheckBox{Text = "全選/反選",Location = new Point(50, 230),AutoSize = true,ThreeState = true, // 啟用三態模式CheckState = CheckState.Unchecked};controls.Add(parentCheck); ?// 子復選框(3個)for (int i = 1; i <= 3; i++){CheckBox childCheck = new CheckBox{Text = $"子選項 {i}",Location = new Point(100, 260 + (i - 1) * 30),AutoSize = true};childCheckboxes.Add(childCheck);controls.Add(childCheck); ?// 子選項狀態變化時,更新父復選框狀態childCheck.CheckedChanged += (sender, e) => UpdateParentCheckState(parentCheck);} ?// 父復選框狀態變化時,同步子選項parentCheck.CheckStateChanged += (sender, e) =>{if (parentCheck.CheckState == CheckState.Checked){// 全選childCheckboxes.ForEach(c => c.Checked = true);}else if (parentCheck.CheckState == CheckState.Unchecked){// 全不選childCheckboxes.ForEach(c => c.Checked = false);}// Indeterminate狀態由子選項自動觸發,無需手動處理}; ?// 4. 按鈕樣式復選框(開關效果)Label buttonStyleLabel = new Label{Text = "4. 按鈕樣式復選框:",Location = new Point(30, 350),AutoSize = true};controls.Add(buttonStyleLabel); ?CheckBox darkModeCheck = new CheckBox{Text = "深色模式",Location = new Point(50, 380),Size = new Size(100, 30),Appearance = Appearance.Button, // 按鈕樣式FlatStyle = FlatStyle.Flat};CheckBox fullscreenCheck = new CheckBox{Text = "全屏顯示",Location = new Point(200, 380),Size = new Size(100, 30),Appearance = Appearance.Button,FlatStyle = FlatStyle.Flat};controls.AddRange(new[] { darkModeCheck, fullscreenCheck }); ?// 5. 顯示選中結果的按鈕和標簽Button showResultButton = new Button{Text = "顯示選中結果",Location = new Point(350, 380),Size = new Size(120, 30)};Label resultLabel = new Label{Text = "選中的愛好:無",Location = new Point(50, 420),AutoSize = true,ForeColor = Color.Blue};controls.AddRange(new[] { showResultButton, resultLabel }); ?// 點擊按鈕時,獲取選中的愛好showResultButton.Click += (sender, e) =>{List<string> selectedHobbies = new List<string>();if (basketballCheck.Checked) selectedHobbies.Add("籃球");if (footballCheck.Checked) selectedHobbies.Add("足球");if (readingCheck.Checked) selectedHobbies.Add("閱讀");if (musicCheck.Checked) selectedHobbies.Add("音樂");if (otherCheck.Checked && !string.IsNullOrEmpty(otherTextBox.Text))selectedHobbies.Add(otherTextBox.Text); ?resultLabel.Text = $"選中的愛好:{ (selectedHobbies.Any() ? string.Join("、", selectedHobbies) : "無") }";}; ?return controls.ToArray();} ?// 更新父復選框的三態狀態(根據子選項選中情況)private void UpdateParentCheckState(CheckBox parentCheck){int checkedCount = childCheckboxes.Count(c => c.Checked);if (checkedCount == 0){parentCheck.CheckState = CheckState.Unchecked; // 全不選}else if (checkedCount == childCheckboxes.Count){parentCheck.CheckState = CheckState.Checked; // 全選}else{parentCheck.CheckState = CheckState.Indeterminate; // 部分選中(半選)}} ?// 程序入口[STAThread]static void Main(){Application.EnableVisualStyles();Application.Run(new CheckBoxExampleForm());}} } ?
代碼說明
這個示例展示了 CheckBox 的核心用法,重點包括:
基礎多選功能:通過一組 CheckBox 實現愛好選擇,用戶可同時選中多個選項;“其他” 選項選中時,通過
CheckedChanged
事件顯示關聯的輸入框,實現條件交互。功能開關場景:模擬系統設置中的開關(記住密碼、自動登錄等),單個 CheckBox 獨立控制一項功能,默認選中 “啟用消息通知”。
三態模式與父子關聯:父 CheckBox 啟用三態模式(
ThreeState = true
),根據子選項的選中情況自動切換狀態:全選 → 父選項
Checked
全不選 → 父選項
Unchecked
部分選中 → 父選項
Indeterminate
(半選狀態) 同時,點擊父選項可反向控制子選項(全選 / 全不選)。
按鈕樣式復選框:設置
Appearance = Appearance.Button
后,CheckBox 呈現按鈕樣式,選中時呈按下狀態,適合作為功能開關(如深色模式、全屏顯示)。結果獲取:通過 “顯示選中結果” 按鈕遍歷 CheckBox 的
Checked
屬性,收集并展示選中的愛好,演示如何處理多選結果。
使用注意事項
與 RadioButton 的區別 CheckBox 支持多選(狀態獨立),RadioButton 支持單選(同組互斥),選擇控件時需根據業務場景(多選 / 單選)決定。
三態模式的啟用
Indeterminate
狀態僅在ThreeState = true
時有效,通常用于 “部分選擇” 的抽象場景(如樹形結構、列表批量選擇),普通多選場景無需啟用。事件選擇 基礎場景用
CheckedChanged
即可;三態模式需監聽CheckStateChanged
以響應Indeterminate
狀態變化。狀態判斷順序 判斷
CheckState
時,建議先檢查Indeterminate
,再檢查Checked
/Unchecked
(避免邏輯錯誤)。批量處理選中項 遍歷一組 CheckBox 時,可通過
Checked
屬性篩選選中項(如示例中 “顯示選中結果” 的邏輯),無需逐個判斷。
CheckBox 是實現 “多項選擇” 的核心控件,通過靈活的狀態控制和事件處理,可滿足從簡單多選到復雜父子關聯的多種交互需求,是表單設計中處理 “非互斥選擇” 場景的首選。