大家好,又见面了,我是你们的朋友全栈君。
# 前言 ChecklistBox看起来一个很简单的控件,使用的时候想要达到理想的效果是需要费一番功夫的,这篇博客对于近一两周来项目中主界面设计用到的ChecklistBox方法做了一点小小的总结。 ##图示
checkedListBox1.Items.Add("吃饭");
checkedListBox1.Items.Add("睡觉");
checkedListBox1.Items.Add("打豆豆");
private void ChecklistBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (ChecklistBox.CheckedItems.Count > 0)
{
for (int i = 0; i < ChecklistBox.Items.Count; i++)
{
if (i != e.Index)
{
ChecklistBox.SetItemChecked(i, false);
}
}
}
}
ChecklistBox.Enabled = false;
//4.1 判断是否有选中项
if (ChecklistBox1.CheckedItems.Count == 0 )
{
MessageBox.Show("请先选中要执行的计划");
}
//4.2 判断第i项是否选中 ,选中返回true,否则选false
if(checkedListBox1.GetItemChecked(i))
{
return true;
}
else
{
return false;
}
//true选中,false为未选中
checkedListBox1.SetItemChecked(i, true);
//6.1得到一个选中项的值
string strPlan = checkedListBox1.SelectedItem.ToString();
//6.2得到所有选中项的值
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]);
}
}
}
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.Items.Clear();
}
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
checkedListBox1.SetItemChecked(i, false);
}
else
{
checkedListBox1.SetItemChecked(i, true);
}
}
for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{
checkedListBox2.Items.Add(this.checkedListBox1.CheckedItems);
//remove是除去一个具体的值,不是index
this.checkedListBox1.Items.Remove(this.checkedListBox1.CheckedItems);
}
for (int i = 0; i < clbOver.Items.Count; i++)
{
clbOver.GetItemChecked(i);
clbOver.SetItemCheckState(i, CheckState.Indeterminate);
}
以上是最近一段时间对ChecklistBox实践过程中常用方法的总结,希望能对大家有所帮助。有一点感受是:在使用一个控件之前查看官方文档遍历一遍它的所有属性,方法和事件很有必要,有了这样一层宏观之后可以帮助大家在具体实践过程中少走弯路。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/148214.html原文链接:https://javaforall.cn