C# checkboxlist的使用

C# checkboxlist的使用
最近做项目,需要用到checklistbox这一控件,由于对这一控件并不是很熟悉,导致在此浪费了一点时间。

http://www.cnblogs.com/hongfei/archive/2012/12/21/2828408.html

复制代码
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]));
}
}
参考:
最近用到checklistbox控件,在使用其过程中,花了较多的时间,这里我收集了其相关的代码段,希望对大家有所帮助。
1.
添加项
checkedListBox1.Items.Add(“蓝色”);
checkedListBox1.Items.Add(“红色”);
checkedListBox1.Items.Add(“黄色”);

判断第i项是否选中,选中为true,否则为false
if(checkedListBox1.GetItemChecked(i))
{
return true;
}
else
{
return false;
}

设置第i项是否选中
checkedListBox1.SetItemChecked(i, true); //true改为false为没有选中。

设置全选
添加一个名为select_all的checkbox控件,由其控制checkedListBox是全选还是全不选。
private void select_all_CheckedChanged(object sender, EventArgs e)
{
if(select_all.Checked)
{
for (int j = 0; j < checkedListBox1.Items.Count; j++)
checkedListBox1.SetItemChecked(j, true);
}
else
{
for (int j =0; j < checkedListBox1.Items.Count; j++)
checkedListBox1.SetItemChecked(j, false);
}
}

得到全部选中的值,并将选中的项的文本组合成为一个字符串.
string strCollected = string.Empty;
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
if (strCollected == string.Empty)
{
strCollected = checkedListBox1.GetItemText(checkedListBox1.Items[i]);
}
}
else
{
strCollected = strCollected + “/” + checkedListBox1.
GetItemText(checkedListBox1.Items[i]);
}
}

设置CheckedListBox中第i项的Checked状态
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
7.
private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxAll.Checked)
{
//被选择了则将CheckedListBox中的所有条目都变为Checked状态
for (int i = 0; i < checkedListBoxLayerControl.Items.Count;i++)
{
checkedListBoxLayerControl.SetItemCheckState(i,
CheckState.Checked);
}
}
else
{
//否则变成Unchecked状态
for (int i = 0;i < checkedListBoxLayerControl.Items.Count; i++)
{
checkedListBoxLayerControl.SetItemCheckState(i, CheckState.Unchecked);
}
}
}

checkedListBox 单选设置(代码实现)
private void chkl_ItemAuditing_ItemCheck(object sender,ItemCheckEventArgs e)
{
if (chkl_ItemAuditing.CheckedItems.Count > 0)
{
for (int i = 0; i < chkl_ItemAuditing.Items.Count; i++)
{
if (i != e.Index)
{
this.chkl_ItemAuditing.SetItemCheckState(i,
System.Windows.Forms.CheckState.Unchecked);
}
}
}
}

checkedListBox1显示一个数据库中关键字对应的所有记录
for (int i = 0; i < table.Rows.Count; i++)
{
string name = table.Rows[“myname”].ToString();
string paw = table.Rows[“mypaw”].ToString();
checkedListBox1.Items.Add(name + paw);
}

for(i=0;i<CheckedListBox.Items.Count;i++)
{
if(CheckedListBox.GetItemText(CheckedListBox.Items)==“你得到的值”)
{
CheckedListBox.SetItemChecked(i,true);
}
}

清除checkedListBox1中所有的选项
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.Items.Clear();
}

//设置索引为index的项为选中状态
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}

for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetSelected(i))
{
MessageBox.Show(checkedListBox1.CheckedItems.ToString());
}
}

//选中checkedListBox1所有的选项
for(int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
}
15.
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
//如果checkedListBox1的第i项被选中,
//则显示checkedListBox1对应的值
if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.Items.ToString());
}
}

//反向选择checkedListBox1的选项
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
checkedListBox1.SetItemChecked(i, false);
}
else
{
checkedListBox1.SetItemChecked(i, true);
}
}
17.
//checkedListBox1中选定的项->checkedListBox2
for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{
checkedListBox2.Items.Add(this.checkedListBox1.CheckedItems);
//remove是除去一个具体的值,不是index,注意了
this.checkedListBox1.Items.Remove(
this.checkedListBox1.CheckedItems);
}
18.
//绑定数据
checkedListBox1.DataSource = dt;
checkedListBox1.DisplayMember = “item”;
checkedListBox1.ValueMember = “code”;
这个属性在checklistbox里是没有的,但是可以直接使用
19.
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
checkedListBox1.SelectedIndex = i;
//利用SelectedValue取得Value值时,只能取得当前焦点项的值。所以要对整个CheckedListBox中的所有勾选项,让其都做一次焦点项才能取得所有勾选的项的值。
str+= checkedListBox1.SelectedValue;
}
}
20.
CheckedlistBox控件比较有用到两个属性分别为CheckOnClick为True:表示单击就选中当前行,为False :要点两下才可以选中。(默认值为False)。还有一个属性为ThreeDCheckBoxes为True:表示三维的选中标记,为False:表示表面的显示标记。(默认值为False)。
复制代码

在百度上找到了这篇,挺实用的。

但是,在得到value值时出现了有点问题。

由于我是在selectedindexchanges时触发读取value事件,导致在循环焦点项时发生无限循环的异常。

于是用了比较传统的手法来解决这个问题。

将所要得到的字符串定义成全局变量,在高亮处时判断是否被选中,选中则加上该value值,没有选中则替换掉该value值。

复制代码
1 /*
2 * 先修改checkonclick属性值,使得控件在单击时选中
3 */
4 string s = string.Empty;
5 private void clbParameter_SelectedIndexChanged(object sender, EventArgs e)
6 {
7 // checkList.Clear();
8
9 QuotaModel Quota = new QuotaModel();
10 string ParameterText = “”;//value值
11 string ParameterRowCol = “”;//参数值
12
13 //string name=this.clbParameter.SelectedItem.ToString();
14 string name = this.clbParameter.Text;//当前checklistbox高亮文本值
15
16 string ParameterValue = string.Empty;
17 ParameterValue = this.clbParameter.SelectedValue.ToString();//当前checklistbox高亮value值
18
19 if (QuotaListCheck != null)
20 {
21
22 foreach (QuotaModel model in QuotaListCheck)
23 {
24 //获取行与列值
25 if (model.Quota_Id == ParameterValue)
26 {
27 Quota.Formext_Row = model.Formext_Row;
28 Quota.Formext_Col = model.Formext_Col;
29 ParameterRowCol = “${” + Quota.Formext_Col + Quota.Formext_Row + “}”;
30 }
31 }
32
33 }
34
35 //选项状态改变时遍历判断是否被选中
36 for (int i = 0; i < clbParameter.Items.Count; i++)
37 {
38 string ItemName = string.Empty;
39 ItemName = this.clbParameter.GetItemText(this.clbParameter.Items[i]);
40 if (clbParameter.GetItemChecked(i))
41 {
42 if (ParameterText != “”)
43 {
44 ParameterText = ParameterText + “+”;
45 }
46 ParameterText += ItemName;
47
48 if (name.Equals(ItemName))
49 {
50 if (s != “”)
51 {
52 s += “+”;
53 }
54 s += ParameterRowCol;
55 }
56 }
57 else
58 {
59 if (name.Equals(ItemName))
60 {
61 int index = s.IndexOf(ParameterRowCol);
62 //if (s.Contains("+"))
63 if (index == 0) //判断读取到的value值是否是在字符串的第一位
64 { //若是第一位,判断后续是否还有其他字符
65 if (s.Contains("+")) //若有其他字符,连同后面的“+”号一块替换
66 {
67 ParameterRowCol += “+”;
68 }
69 }
70 else
71 { //如果不是在第一位,则删除该字符与之前的“+”号
72 ParameterRowCol = “+” + ParameterRowCol;
73 }
74 if (ParameterRowCol != “”)
75 {
76 s = s.Replace(ParameterRowCol, “”);
77 }
78 }
79 }
80 }
81 this.txtCheckValue.Text = ParameterText;
82 this.txtCheckParameter.Text = s;
83 }
复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值