轉(zhuǎn)帖|使用教程|編輯:龔雪|2023-08-25 09:55:04.630|閱讀 113 次
概述:本文將為大家介紹在Winform程序開發(fā)過程中的一些復(fù)選框控件賦值小技巧,希望能幫助到大家~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
本人在開發(fā)WinForm程序中,有一些復(fù)選框賦值和獲取值的小技巧,分享討論一下。
PS:給大家推薦一個(gè)C#開發(fā)可以用到的界面組件——DevExpress WinForms,它能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
DevExpress技術(shù)交流群8:523159565 歡迎一起進(jìn)群討論
應(yīng)用場景是這樣的,如果你有一些需要使用復(fù)選框來呈現(xiàn)內(nèi)容的時(shí)候,如下圖所示:
以上的切除部分的內(nèi)容,是采用在GroupBox中放置多個(gè)CheckBox的方式;其實(shí)這個(gè)部分也可以使用Winform控件種的CheckedListBox控件來呈現(xiàn)內(nèi)容,如下所示。
不管采用那種控件,我們都會涉及到為它賦值的麻煩,我這里封裝了一個(gè)函數(shù),可以很簡單的給控件 賦值,大致代碼如下。
CheckBoxListUtil.SetCheck(this.groupRemove, info.切除程度);
那么取控件的內(nèi)容代碼是如何的呢,代碼如下:
info.切除程度 = CheckBoxListUtil.GetCheckedItems(this.groupRemove);
賦值和取值通過封裝函數(shù)調(diào)用,都非常簡單,也可以重復(fù)利用,封裝方法函數(shù)如下所示。
public class CheckBoxListUtil { /// <summary> /// 如果值列表中有的,根據(jù)內(nèi)容勾選GroupBox里面的成員. /// </summary> /// <param name="group">包含CheckBox控件組的GroupBox控件</param> /// <param name="valueList">逗號分隔的值列表</param> public static void SetCheck(GroupBox group, string valueList) { string[] strtemp = valueList.Split(','); foreach (string str in strtemp) { foreach (Control control in group.Controls) { CheckBox chk = control as CheckBox; if (chk != null && chk.Text == str) { chk.Checked = true; } } } } /// <summary> /// 獲取GroupBox控件成員勾選的值 /// </summary> /// <param name="group">包含CheckBox控件組的GroupBox控件</param> /// <returns>返回逗號分隔的值列表</returns> public static string GetCheckedItems(GroupBox group) { string resultList = ""; foreach (Control control in group.Controls) { CheckBox chk = control as CheckBox; if (chk != null && chk.Checked) { resultList += string.Format("{0},", chk.Text); } } return resultList.Trim(','); } /// <summary> /// 如果值列表中有的,根據(jù)內(nèi)容勾選CheckedListBox的成員. /// </summary> /// <param name="cblItems">CheckedListBox控件</param> /// <param name="valueList">逗號分隔的值列表</param> public static void SetCheck(CheckedListBox cblItems, string valueList) { string[] strtemp = valueList.Split(','); foreach (string str in strtemp) { for (int i = 0; i < cblItems.Items.Count; i++) { if (cblItems.GetItemText(cblItems.Items[i]) == str) { cblItems.SetItemChecked(i, true); } } } } /// <summary> /// 獲取CheckedListBox控件成員勾選的值 /// </summary> /// <param name="cblItems">CheckedListBox控件</param> /// <returns>返回逗號分隔的值列表</returns> public static string GetCheckedItems(CheckedListBox cblItems) { string resultList = ""; for (int i = 0; i < cblItems.CheckedItems.Count; i++) { if (cblItems.GetItemChecked(i)) { resultList += string.Format("{0},", cblItems.GetItemText(cblItems.Items[i])); } } return resultList.Trim(','); } }
以上代碼分為兩部分, 其一是對GroupBox的控件組進(jìn)行操作,第二是對CheckedListBox控件進(jìn)行操作。
這樣在做復(fù)選框的時(shí)候,就比較方便一點(diǎn),如我采用第一種GroupBox控件組方式,根據(jù)內(nèi)容勾選的界面如下所示。
應(yīng)用上面的輔助類函數(shù),如果你是采用GroupBox方案,你就可以隨便拖幾個(gè)CheckBox控件進(jìn)去就可以了,也犯不著給他取個(gè)有意義的名字,因?yàn)椴还芩菑埲€是李四,只要它的父親是GroupBox就沒有問題了。
本文轉(zhuǎn)載自:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: