C# CHECKEDLISTBOX控件用法总结
来源:互联网 发布:软件授权码后台 编辑:程序博客网 时间:2024/03/29 15:33
一般认为:foreach (object obj in checkedListBox1.SelectedItems)即可遍历选中的值。
其实这里遍历的只是高亮的值并不是打勾的值。遍历打勾的值要用下面的代码:
参考:
from:http://dotnet.5d6d.com/thread-486-1-1.html
其实这里遍历的只是高亮的值并不是打勾的值。遍历打勾的值要用下面的代码:
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]));
}
}
{
if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]));
}
}
最近用到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, true); //true改为false为没有选中。
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, 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);
}
}
}
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.
for(i=0;i<CheckedListBox.Items.Count;i++)
{
if(CheckedListBox.GetItemText(
CheckedListBox.Items)=="你得到的值")
{
CheckedListBox.SetItemChecked(i,true);
}
}
11.
清除checkedListBox1中所有的选项
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.Items.Clear();
}
12.
//设置索引为index的项为选中状态
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
13.
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetSelected(i))
{
MessageBox.Show(checkedListBox1.CheckedItems.ToString());
}
}
14.
//选中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());
}
}
16.
//反向选择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);
}
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, true); //true改为false为没有选中。
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, 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);
}
}
}
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.
for(i=0;i<CheckedListBox.Items.Count;i++)
{
if(CheckedListBox.GetItemText(
CheckedListBox.Items)=="你得到的值")
{
CheckedListBox.SetItemChecked(i,true);
}
}
11.
清除checkedListBox1中所有的选项
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.Items.Clear();
}
12.
//设置索引为index的项为选中状态
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
13.
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetSelected(i))
{
MessageBox.Show(checkedListBox1.CheckedItems.ToString());
}
}
14.
//选中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());
}
}
16.
//反向选择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);
}
- C# CHECKEDLISTBOX控件用法总结
- C# CHECKEDLISTBOX用法总结
- C# CheckedListBox控件的用法
- C# CheckedListBox控件的用法
- C# CheckedListBox控件的用法
- C# CheckedListBox控件的用法 .
- C# CheckedListBox控件的用法
- C# CheckedListBox控件的用法
- C# CheckedListBox控件的用法
- C#学习笔记:CheckedListBox控件的用法
- C# CHECKEDLISTBOX控件用法总结(怎样得到多选的值)
- C# CHECKEDLISTBOX控件用法总结(怎样得到多选的值)
- CheckedListBox控件的用法
- C# CheckedListBox控件用法
- Visual Basic 2008 中 checkedlistbox控件用法
- c# winform---checkedListBox控件應用
- CheckedListBox用法
- C# WinForm CheckedListBox 使用的相关简单总结
- 雷军谈互联网七字诀:天下武功唯快不破
- 993 - Product of digits
- 谁是中国移动互联网的王者?360发布定制手机消息随感
- poj1323(Game Prediction)
- AndroidManifest.xml文件详解(activity)(二)
- C# CHECKEDLISTBOX控件用法总结
- 高内聚、低耦合
- AndroidManifest.xml文件详解(activity)(三)
- C#中Dictionary的用法及用途
- div 居中
- 孙鑫vc++视频教程笔记之mfc输出卡拉ok变色字幕
- c#的treeview的具体介绍
- bmp try
- 悲催程序员