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

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

for (int i = ; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]));
}
}
参考:
最近用到checklistbox控件,在使用其过程中,花了较多的时间,这里我收集了其相关的代码段,希望对大家有所帮助。
.
添加项
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 = ; j < checkedListBox1.Items.Count; j++)
checkedListBox1.SetItemChecked(j, true);
}
else
{
for (int j =; j < checkedListBox1.Items.Count; j++)
checkedListBox1.SetItemChecked(j, false);
}
} .
得到全部选中的值,并将选中的项的文本组合成为一个字符串.
string strCollected = string.Empty;
for (int i = ; 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);
.
private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxAll.Checked)
{
//被选择了则将CheckedListBox中的所有条目都变为Checked状态
for (int i = ; i < checkedListBoxLayerControl.Items.Count;i++)
{
checkedListBoxLayerControl.SetItemCheckState(i,
CheckState.Checked);
}
}
else
{
//否则变成Unchecked状态
for (int i = ;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 > )
{
for (int i = ; i < chkl_ItemAuditing.Items.Count; i++)
{
if (i != e.Index)
{
this.chkl_ItemAuditing.SetItemCheckState(i,
System.Windows.Forms.CheckState.Unchecked);
}
}
}
} .
checkedListBox1显示一个数据库中关键字对应的所有记录
for (int i = ; i < table.Rows.Count; i++)
{
string name = table.Rows["myname"].ToString();
string paw = table.Rows["mypaw"].ToString();
checkedListBox1.Items.Add(name + paw);
} .
for(i=;i<CheckedListBox.Items.Count;i++)
{
if(CheckedListBox.GetItemText(CheckedListBox.Items)=="你得到的值")
{
CheckedListBox.SetItemChecked(i,true);
}
} .
清除checkedListBox1中所有的选项
for (int i = ; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.Items.Clear();
} .
//设置索引为index的项为选中状态
for (int i = ; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
} .
for (int i = ; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetSelected(i))
{
MessageBox.Show(checkedListBox1.CheckedItems.ToString());
}
} .
//选中checkedListBox1所有的选项
for(int i = ; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
}
.
for (int i = ; i < checkedListBox1.Items.Count; i++)
{
//如果checkedListBox1的第i项被选中,
//则显示checkedListBox1对应的值
if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.Items.ToString());
}
} .
//反向选择checkedListBox1的选项
for (int i = ; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
checkedListBox1.SetItemChecked(i, false);
}
else
{
checkedListBox1.SetItemChecked(i, true);
}
}
.
//checkedListBox1中选定的项->checkedListBox2
for (int i = ; i < checkedListBox1.CheckedItems.Count; i++)
{
checkedListBox2.Items.Add(this.checkedListBox1.CheckedItems);
//remove是除去一个具体的值,不是index,注意了
this.checkedListBox1.Items.Remove(
this.checkedListBox1.CheckedItems);
}
.
//绑定数据
checkedListBox1.DataSource = dt;
checkedListBox1.DisplayMember = "item";
checkedListBox1.ValueMember = "code";
这个属性在checklistbox里是没有的,但是可以直接使用
.
for (int i = ; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
checkedListBox1.SelectedIndex = i;
//利用SelectedValue取得Value值时,只能取得当前焦点项的值。所以要对整个CheckedListBox中的所有勾选项,让其都做一次焦点项才能取得所有勾选的项的值。
str+= checkedListBox1.SelectedValue;
}
}
.
CheckedlistBox控件比较有用到两个属性分别为CheckOnClick为True:表示单击就选中当前行,为False :要点两下才可以选中。(默认值为False)。还有一个属性为ThreeDCheckBoxes为True:表示三维的选中标记,为False:表示表面的显示标记。(默认值为False)。

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

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

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

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

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

         /*
* 先修改checkonclick属性值,使得控件在单击时选中
*/
string s = string.Empty;
private void clbParameter_SelectedIndexChanged(object sender, EventArgs e)
{
// checkList.Clear(); QuotaModel Quota = new QuotaModel();
string ParameterText = "";//value值
string ParameterRowCol = "";//参数值 //string name=this.clbParameter.SelectedItem.ToString();
string name = this.clbParameter.Text;//当前checklistbox高亮文本值 string ParameterValue = string.Empty;
ParameterValue = this.clbParameter.SelectedValue.ToString();//当前checklistbox高亮value值 if (QuotaListCheck != null)
{ foreach (QuotaModel model in QuotaListCheck)
{
//获取行与列值
if (model.Quota_Id == ParameterValue)
{
Quota.Formext_Row = model.Formext_Row;
Quota.Formext_Col = model.Formext_Col;
ParameterRowCol = "${" + Quota.Formext_Col + Quota.Formext_Row + "}";
}
} } //选项状态改变时遍历判断是否被选中
for (int i = ; i < clbParameter.Items.Count; i++)
{
string ItemName = string.Empty;
ItemName = this.clbParameter.GetItemText(this.clbParameter.Items[i]);
if (clbParameter.GetItemChecked(i))
{
if (ParameterText != "")
{
ParameterText = ParameterText + "+";
}
ParameterText += ItemName; if (name.Equals(ItemName))
{
if (s != "")
{
s += "+";
}
s += ParameterRowCol;
}
}
else
{
if (name.Equals(ItemName))
{
int index = s.IndexOf(ParameterRowCol);
//if (s.Contains("+"))
if (index == ) //判断读取到的value值是否是在字符串的第一位
{ //若是第一位,判断后续是否还有其他字符
if (s.Contains("+")) //若有其他字符,连同后面的“+”号一块替换
{
ParameterRowCol += "+";
}
}
else
{ //如果不是在第一位,则删除该字符与之前的“+”号
ParameterRowCol = "+" + ParameterRowCol;
}
if (ParameterRowCol != "")
{
s = s.Replace(ParameterRowCol, "");
}
}
}
}
this.txtCheckValue.Text = ParameterText;
this.txtCheckParameter.Text = s;
}

C# checkboxlist的使用的更多相关文章

  1. MVC CheckBoxList的实现

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...

  2. HtmlHelper拓展实现CheckBoxList

    经过一番折腾(主要是SelectList这个类操作有些繁琐)实现了CheckBoxList,过程RadioList基本一样 拓展方法 public static MvcHtmlString Check ...

  3. CheckBoxList控件获取多选择,需要遍历

    CheckBoxList控件获取多选择,需要遍历,环境:vs2008 在页面上添加CheckBoxList控件,输入项值 a,b,c,d.然后添加按钮 Button2确定,如何获取CheckBoxLi ...

  4. C#遍历得到checkboxlist选中值和设置选中项

    得到选中项的value值并拼接成一个字符串返回 public string GetChecked(CheckBoxList checkList, string separator) { string ...

  5. checkboxlist 下拉框多选功能 ,模拟dropdownlist带复选框效果

    前台代码 01.<html xmlns="http://www.w3.org/1999/xhtml"> 02.<head runat="server&q ...

  6. .NET MVC3中扩展一个HtmlHelper方法CheckBoxList

    MVC中有DropDownList方法,挺好用,可是最常用的需求,一组checkboxlist咋没个类似方法呢?郁闷之余,自己做一个吧,直接上代码 public static MvcHtmlStrin ...

  7. ASP.NET MVC 3 CheckBoxList 的使用

    在以前的 ASP.NET MVC 中可以直接使用 CheckBoxList,但后来不知道什么原因在 MVC 中移除了 CheckBoxList,所以默认情况下 ASP.NET MVC 3 中是没有 C ...

  8. CheckBoxList 用法

    <asp:CheckBoxList ID="cblqf" ForeColor="#4d6fc8" runat="server" Rep ...

  9. GridView 控件中如何绑定 CheckBoxList

    需求:设计这样一个页面,在页面上可以选择和展示各省份对应的文明城市? 思路:一次性查询出所需的记录(查询数据库的操作不宜写到 C# 代码的循环语句中),并保存到全局变量中,之后根据条件过滤出需要的.可 ...

  10. 20151215单选按钮列表,复选框列表:CheckBoxList

    单选框:RadioButton GroupName:组名,如果要实现单选效果每个单选按钮的组名必须一样 是否被选中 RadioButton.checked 单选按钮列表:RadioButtonList ...

随机推荐

  1. DNS分别在什么情况下使用UDP和TCP

    DNS同时占用UDP和TCP端口53是公认的,这种单个应用协议同时使用两种传输协议的情况在TCP/IP栈也算是个另类.但很少有人知道DNS分别在什么情况下使用这两种协议.     如果用wiresha ...

  2. 用JS来控制 div的高度随浏览器变化而变化

    <div id="test" style=" border: solid 1px #f00; "></div> <script t ...

  3. SpringMVC学习系列(3) 之 URL请求到Action的映射规则

    在系列(2)中我们展示了一个简单的get请求,并返回了一个简单的helloworld页面.本篇我们来学习如何来配置一个action的url映射规则. 在系列(2)中我们在HelloWorldContr ...

  4. Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted

    1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...

  5. 2.在程序中如何实现Cookie信息的设置,读取和删除

    设置:你可以在IE的“工具/Internet选项”的“常规”选项卡中,选择“设置/查看文件”,查看所有保存到你电脑里的Cookies.这些文件通常是以user@domain格式命名的,user是你的本 ...

  6. Qt信号与槽应用实例一

    ..... connect(m_pGlobelWidget,signal(globeControlClick(object,object)),this,slot(globeControlClick(o ...

  7. 十二、Java基础---------异常处理机制

    异常 异常 异常就是程序在运行时产生的异常情况. 异常的由来 问题也是现实生活中的具体事物,也可以通过java 类的形式进行描述,并封装成对象.异常就是java 对不正常情况进行描述后的对象的体现. ...

  8. List,Set,Map用法以及区别(转)

    Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素.一些Collection允许相同的元素而另一些不行.一些能排序而另一些不行.Java ...

  9. 浅析firmware完整生存和使用流程 【转】

    转自:http://blog.csdn.net/zhenwenxian/article/details/4677604 浅析firmware完整生存和使用流程 1. http://blog.china ...

  10. Hudson(Jenkins)持续集成插件开发环境搭建

    Hudson持续集成插件开发环境搭建 第一步安装java jdk,至于版本的话推荐1.6以上吧.安装好jdk设置环境变量,确保你在cmd中输入java -version有提示你jdk的版本信息等,也就 ...