Winforms checkedlistbox检查一个项目

时间:2010-12-08 11:44:01

标签: c# winforms .net-3.5

我的表单上有一个CheckedListBox控件,我希望用户只能在此列表中一次检查一个项目(所以有效地我想要一些模仿“RadioListBox”的东西)。

这可能与CheckedListBox有关,还是我不得不通过其他方式即兴创作?

通过从数据库加载项目来填充表单加载CheckedListBox,以防万一。

由于

修改

我想我应该澄清一下,我不打算限制用户可以选择的数量(即SelectionMode属性),而不是他们可以检查多少。

4 个答案:

答案 0 :(得分:3)

您可以通过在CheckedListBox上为ItemCheck添加事件检查来实现,并使用如下函数:

    private static bool checkIfAllowed(CheckedListBox listBox) {
        if (listBox.CheckedItems.Count > 0) {
            return false;
        }
        return true;
    }

然后在你想要的情况下:

  if (checkIfAllowed) { 
     ...
  } else {

  }

此外,您可以通过添加另一个功能/方法来改进此功能,该功能/方法将在允许检查项目之前取消选中所有项目。因此,当用户单击某个复选框时,将取消选中所有其他复选框。

要取消选中所有选中的项目,请使用:

    private static void uncheckAll(CheckedListBox listBox) {
        IEnumerator myEnumerator;
        myEnumerator = listBox.CheckedIndices.GetEnumerator();
        int y;
        while (myEnumerator.MoveNext() != false) {
            y = (int)myEnumerator.Current;
            listBox.SetItemChecked(y, false);
        }
    }

因此,在ItemCheck事件中,您必须先运行uncheckAll(yourListBox),然后只需检查项目。

编辑: 我用下面的代码测试了它,它的工作原理。没有if它会抛出异常。

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
        if (e.NewValue == CheckState.Checked) {
            IEnumerator myEnumerator;
            myEnumerator = checkedListBox1.CheckedIndices.GetEnumerator();
            int y;
            while (myEnumerator.MoveNext() != false) {
                y = (int)myEnumerator.Current;
                checkedListBox1.SetItemChecked(y, false);
            }
        }

    }

答案 1 :(得分:1)

尝试设置.SelectionMode = SelectionMode.One属性。

答案 2 :(得分:0)

我必须做类似的事情从大量列表中选择一个用户。没有RadioListBox可以说,所以我只是手动编码...

很抱歉,在VB中,我只是粘贴它,逻辑是一样的。

Private m_NoFire As Boolean = False

Private Sub lstSource_ItemCheck( _
        ByVal sender As Object, _
        ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lstSource.ItemCheck

    'When the checked state is set programatically,
    'this event will still fire and cause the loop
    'to run - infinatly. This line prevents that.
    If m_NoFire Then Exit Sub
    m_NoFire = True

    'Ensure only one item is selected
    For i As Integer = 0 To lstSource.Items.Count - 1
        If Not lstSource.SelectedIndex = i Then
            lstSource.SetItemChecked(i, False)
        End If
    Next

    m_NoFire = False

End Sub '-- lstSource_ItemCheck

答案 3 :(得分:0)

这段代码可以简单得多,需要一个变量。 每次你检查一个新的盒子时,它都会取消选中最后一个盒子,导致当时没有两个被检查的项目或更多。 确保按照提及设置属性SelectionMode = SelectionMode.One;

    private int lastCheck = -1;
    private void CheckListBox_IndexChanged(object sender, EventArgs e) {
        int toUncheck = lastCheck;
        if (toUncheck != -1)
            CheckListBox.SetItemChecked(toUncheck, false);
        lastCheck = CheckListBox.SelectedIndex;
        CheckListBox.SetItemChecked(lastCheck, true);
    }

或者,如果需要,您可以将lastCheck设置为默认复选框,请执行以下操作:

    private void FormFoo_Load(object sender, EventArgs e) {
        CheckListBox.SelectedIndex = 0;
        lastCheck = CheckListBox.SelectedIndex;
    }

然后我在上面提到的其余代码如下所示。

注意*:我使用lastCheck = **CheckListBox.SelectedIndex;**来支持键盘移动,以防万一。

相关问题