欢迎来到天天文库
浏览记录
ID:38102557
大小:61.00 KB
页数:4页
时间:2019-05-24
《winform复选框控件赋值的小技巧》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、winform复选框控件赋值的小技巧Postedon2010-02-0716:36伍华聪阅读(1359)评论(5) 编辑收藏前几天,有一位园友写了一篇不错的文章《WinForm清空界面控件值的小技巧》,文章里面介绍了怎么清空界面各个控件值的一个好技巧,这个方法确实是不错的,在繁杂的界面控件值清理中,可谓省时省力。本人在开发Winform程序中,也有一个类似的小技巧,不是清空控件值,而是赋值,给复选框赋值和获取值的小技巧,分享讨论一下。应用场景是这样的,如果你有一些需要使用复选框来呈现内容的时候,如下面两图所示: 以上的切除部分的内容,是采用在Gro
2、upBox中放置多个CheckBox的方式;其实这个部分也可以使用Winform控件种的CheckedListBox控件来呈现内容。如下所示。 不管采用那种控件,我们都会涉及到为它赋值的麻烦,我这里封装了一个函数,可以很简单的给控件赋值,大致代码如下。CheckBoxListUtil.SetCheck(this.groupRemove, info.切除程度); 那么取控件的内容代码是如何的呢,代码如下:info.切除程度 = CheckBoxListUtil.GetCheckedItems(this.groupRemove); 赋值和取值通过封装函
3、数调用,都非常简单,也可以重复利用,封装方法函数如下所示。代码 public class CheckBoxListUtil { /// /// 如果值列表中有的,根据内容勾选GroupBox里面的成员. /// /// 包含CheckBox控件组的GroupBox控件 /// 逗号分隔的值列表
4、public static void SetCheck(GroupBox group, string valueList) { string[] strtemp = valueList.Split(','); foreach (string str in strtemp) { foreach (Control control in group.Controls) { Che
5、ckBox chk = control as CheckBox; if (chk != null && chk.Text == str) { chk.Checked = true; } } } } /// /// 获取GroupBox控件成员勾选的值 /// 6、ummary> /// 包含CheckBox控件组的GroupBox控件 /// 返回逗号分隔的值列表 public static string GetCheckedItems(GroupBox group) { string resultList = ""; foreach (Control control in group.Controls) 7、 { CheckBox chk = control as CheckBox; if (chk != null && chk.Checked) { resultList += string.Format("{0},", chk.Text); } } return resultList.Trim(','); } //8、/ /// 如果值列表中有的,根据内容勾选CheckedListBox的成员.
6、ummary> /// 包含CheckBox控件组的GroupBox控件 /// 返回逗号分隔的值列表 public static string GetCheckedItems(GroupBox group) { string resultList = ""; foreach (Control control in group.Controls)
7、 { CheckBox chk = control as CheckBox; if (chk != null && chk.Checked) { resultList += string.Format("{0},", chk.Text); } } return resultList.Trim(','); } //
8、/ /// 如果值列表中有的,根据内容勾选CheckedListBox的成员.
此文档下载收益归作者所有