日期:2014-05-18  浏览次数:21290 次

C# checklistbox控件用法总结(怎样得到多选的值,以及动态加载数据)

C# checklistbox控件用法总结(怎样得到多选的值,以及动态加载数据)

一般认为:foreach (object obj in checkedListBox1.SelectedItems)即可遍历选中的值。
其实这里遍历的只是高亮的值并不是打勾的值。遍历打勾的值要用下面的代码:
for?<wbr>(<span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(0,0,255); WORD-BREAK: normal">int</span>?<wbr>i?<wbr>=?<wbr><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(128,0,128); WORD-BREAK: normal">0</span>;?<wbr>i?<wbr>&lt;?<wbr>checkedListBox1.Items.Count;?<wbr>i++)<br>{<br>?<wbr>?<wbr>?<wbr>?<wbr><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(0,0,255); WORD-BREAK: normal">if</span>?<wbr>(checkedListBox1.GetItemChecked(i))<br>?<wbr>?<wbr>?<wbr>?<wbr>{<br>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]));<br>?<wbr>?<wbr>?<wbr>?<wbr>}<br>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
参考:
最近用到checklistbox控件,在使用其过程中,花了较多的时间,这里我收集了其相关的代码段,希望对大家有所帮助。
1.
添加项
checkedListBox1.Items.Add("蓝色");?<wbr><br>checkedListBox1.Items.Add(<span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(128,0,0); WORD-BREAK: normal">"</span><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(128,0,0); WORD-BREAK: normal">红色</span><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(128,0,0); WORD-BREAK: normal">"</span>);?<wbr><br>checkedListBox1.Items.Add(<span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(128,0,0); WORD-BREAK: normal">"</span><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(128,0,0); WORD-BREAK: normal">黄色</span><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(128,0,0); WORD-BREAK: normal">"</span>);<br>?<wbr><br><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(128,0,128); WORD-BREAK: normal">2</span>.?<wbr><br>判断第i项是否选中,选中为true,否则为false<br><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(0,0,255); WORD-BREAK: normal">if</span>(checkedListBox1.GetItemChecked(i))<br>{<br>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(0,0,255); WORD-BREAK: normal">return</span>?<wbr><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(0,0,255); WORD-BREAK: normal">true</span>;<br>}?<wbr><br><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(0,0,255); WORD-BREAK: normal">else</span><br>{<br>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(0,0,255); WORD-BREAK: normal">return</span>?<wbr><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(0,0,255); WORD-BREAK: normal">false</span>;?<wbr><br>}<br>?<wbr><br><span style="LINE-HEIGHT: 21px; WORD-WRAP: normal; COLOR: rgb(