C# Checklistbox的用法
类别:编程学习 浏览量:230
时间:2015-5-9 C# Checklistbox的用法
C# Checklistbox的用法1、添加项
checkedListBox1.Items.Add("蓝色");
checkedListBox1.Items.Add("红色");
checkedListBox1.Items.Add("黄色");
2、判断第i项是否选中,选中为true,否则为false
if(checkedListBox1.GetItemChecked(i))
{
return true;
}
else
{
return false;
}
3、设置第i项是否选中
checkedListBox1.SetItemChecked(i,
4、设置全选
添加一个名为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);
}
}
5、得到全部选中的值
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]);
}
}
}
6、设置CheckedListBox中第i项的Checked状态
checkedListBox1.SetItemCheckState(i,
7、 为checklistbox绑定数据源
/// <summary>
/// 为checklistbox绑定数据源
/// </summary>
/// <typeparam name="T">数据源类型</typeparam>
/// <param name="checklistbox">checklistbox对象</param>
/// <param name="list">数据源</param>
/// <param name="texfield">显示值字段名称</param>
/// <param name="valuefield">值字段名称</param>
public void CheckListboxDatabind<T>(CheckBoxList checklistbox, List<T> list, string texfield, string valuefield)
{
checklistbox.DataSource = list;
checklistbox.DataTextField = texfield;
checklistbox.DataValueField = valuefield;
checklistbox.DataBind();
}
8、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);
}
}
}
}
9、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);
}
10、清除checkedListBox1中所有的选项
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.Items.Clear();
}
11、设置checklistboxbox选中的值
public void SetCheckListBoxChecked(CheckBoxList checklistbox, string checkvale)
{
string[] checkstrs;
checkstrs = checkvale.Split(',');
for (int i = 0; i < checklistbox.Items.Count; i++)
{
for (int j = 0; j < checkstrs.Length; j++)
{
if (checklistbox.Items[i].Value == checkstrs[j])
{
checklistbox.Items[i].Selected = true;
break;
}
}
}
}
12、获取checklistbox选中的值
/// <summary>
/// 获取checklistbox选中的值
/// </summary>
/// <param name="checklistbox">checklistbox对象</param>
/// <returns>string字符串</returns>
public string GetChecklistvalue(CheckBoxList checklistbox)
{
string listvalue = "";
for (int i = 0; i < checklistbox.Items.Count; i++)
{
if (checklistbox.Items[i].Selected)
{
listvalue = listvalue + checklistbox.Items[i].Value + ",";
}
}
return listvalue;
}
标签:C#选择框
热门推荐
- python在mysql创建数据库(python3对接mysql数据库实例详解)
- stylus、sass、less区别
- mysql 8.0.22 winx64安装配置方法图文教程(mysql 8.0.22 winx64安装配置方法图文教程)
- flex用法(详解关于flex-shrink如何计算的冷知识)
- nginx 怎么避免options请求(详解nginx 的 default_server 定义及匹配规则)
- jquery中find()与filter()的区别
- python中mod函数的使用方法(详解Python3中ceil函数用法)
- css3特性动画图(CSS3+DIV实现漂亮的动画彩色标签)
- numpy的核心是n维数组对象(利用ctypes获取numpy数组的指针方法)
- aws提供了哪些云服务(AWS与阿里云服务器在国内使用的简单对比评测)