【发布时间】:2014-07-03 21:30:36
【问题描述】:
我很难让 CheckedListBox 以我想要的方式运行。我想要完成的是获得一个 CheckedListBox 并在 PageLoad 上选中第一个框。我只想在任何给定时间选中一个复选框,但我也不希望用户取消选中最后一个复选框。我可以做一个或另一个,但我似乎不能同时做。
这是我用来完成只选中一个复选框的任务的一些代码 sn-ps。这些问题是在没有选中复选框的情况下可以取消选中最后一个选择。
第一个片段:
if(e.NewValue == CheckState.Checked)
{
// Uncheck the other items
for (int i = 0; i < defCheckedListBox.Items.Count; i++)
{
if (e.Index != i)
{
this.defCheckedListBox.SetItemChecked(i, false);
}
}
}
第二个片段
// Ensure that we are checking an item
if (e.NewValue != CheckState.Checked)
{
return;
}
// Get the items that are selected
CheckedListBox.CheckedIndexCollection selectedItems = this.defCheckedListBox.CheckedIndices;
// Check that we have at least 1 item selected
if (selectedItems.Count > 0)
{
// Uncheck the other item
this.defCheckedListBox.SetItemChecked(selectedItems[0], false);
}
这是我用来防止最后一个复选框“未选中”的方法
if (laborLevelDefCheckedListBox.CheckedItems.Count == 1)
{
if (e.CurrentValue == CheckState.Checked)
{
e.NewValue = CheckState.Checked;
}
}
我知道这一定很简单,但我认为因为我已经度过了漫长的一周,而且我已经看了太久了,所以它只是不适合我。对此的任何帮助都非常感谢!如果我在周末解决这个问题,我一定会发布我的解决方案。顺便说一句,美国的人们节日快乐:)
【问题讨论】:
-
使用 RadioButtons 怎么样?
-
完全同意。我想使用单选按钮,但出于某种原因需要复选框。
标签: c# checkbox checked checkedlistbox