如何禁用checkedlistbox中的复选框?

时间:2010-12-06 16:29:17

标签: c# winforms checkboxlist

我在核对清单框中有一些项目,我想禁用其中第一项的复选框 即我想禁用CheckedListBox中的第一项,因为我想直观地告诉用户该选项不可用。

11 个答案:

答案 0 :(得分:13)

结合上述部分答案中的2个对我来说非常有用。 将您的商品添加到列表中:

myCheckedListBox.Items.Add(myItem, myState);

其中myState是CheckState.Indeterminate表示应该禁用的项目。 然后添加一个事件处理程序以防止这些项被更改:

myCheckedListBox.ItemCheck += (s, e) => { if (e.CurrentValue == CheckState.Indeterminate) e.NewValue = CheckState.Indeterminate; };

这不允许您在此列表中使用“Indeterminate”作为正常用途,但它确实看起来非常类似于对禁用项目的预期,并且它提供了正确的行为!

答案 1 :(得分:9)

禁用项目不是一个好主意,用户将没有好的反馈,单击复选框将没有任何效果。您不能使用自定义绘图使其显而易见。最好的办法是简单地省略该项目。

然而,您可以使用ItemCheck事件轻松击败用户:

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
        if (e.Index == 0) e.NewValue = e.CurrentValue;
    }

答案 2 :(得分:7)

要禁用任何特定项目,请使用以下内容:

checkedListBox1.SetItemCheckState(0, CheckState.Indeterminate);

SetItemCheckState获取项目索引和CheckState枚举 不确定用于显示阴影外观

答案 3 :(得分:7)

虽然这篇文章相当陈旧,但最后添加的答案已于今年4月提交 我希望这会对某人有所帮助 我追求的是类似的东西:一个行为类似的复选列表框 许多安装程序,它提供了一些需要某些功能的选项列表 因此,检查都被禁用 感谢这篇文章(Can I use a DrawItem event handler with a CheckedListBox?) 我设法做到这一点,继承CheckedListBox控件。
作为链接帖子中的OP状态,在CheckedListBox控件中, OnDrawItem 事件永远不会被触发, 所以子类化是必要的 它非常基础,但它有效。 这就是它的样子(上面的CheckBox用于比较):

checked list box

注意:禁用的项目确实已禁用:点击它无论如何都没有任何效果 (据我所知)。 这就是代码:

public class CheckedListBoxDisabledItems : CheckedListBox {
    private List<string> _checkedAndDisabledItems = new List<string>();
    private List<int> _checkedAndDisabledIndexes = new List<int>();

    public void CheckAndDisable(string item) {
        _checkedAndDisabledItems.Add(item);
        this.Refresh();
    }

    public void CheckAndDisable(int index) {
        _checkedAndDisabledIndexes.Add(index);
        this.Refresh();
    }

    protected override void OnDrawItem(DrawItemEventArgs e) {
        string s = Items[e.Index].ToString();

        if (_checkedAndDisabledItems.Contains(s) || _checkedAndDisabledIndexes.Contains(e.Index)) {
            System.Windows.Forms.VisualStyles.CheckBoxState state = System.Windows.Forms.VisualStyles.CheckBoxState.CheckedDisabled;
            Size glyphSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, state);
            CheckBoxRenderer.DrawCheckBox(
                e.Graphics,
                new Point(e.Bounds.X + 1, e.Bounds.Y + 1), // add one pixel to align the check gliph properly
                new Rectangle(
                    new Point(e.Bounds.X + glyphSize.Width + 3, e.Bounds.Y), // add three pixels to align text properly
                    new Size(e.Bounds.Width - glyphSize.Width, e.Bounds.Height)),
                s,
                this.Font,
                TextFormatFlags.Left, // text is centered by default
                false,
                state); 
        }
        else {
            base.OnDrawItem(e);
        }
    }

    public void ClearDisabledItems() {
        _checkedAndDisabledIndexes.Clear();
        _checkedAndDisabledItems.Clear();
        this.Refresh();
    }
}

像这样使用:

checkedListBox.Items.Add("Larry");
checkedListBox.Items.Add("Curly");
checkedListBox.Items.Add("Moe");

// these lines are equivalent
checkedListBox.CheckAndDisable("Larry");
checkedListBox.CheckAndDisable(0);

希望这可以帮助某人。

答案 4 :(得分:5)

我知道它已经有一段时间了,但我在搜索列表框时发现了这一点,并认为我会将其添加到讨论中。

如果您有一个列表框并且想要禁用所有复选框以便无法单击它们,但不禁用该控件以便用户仍然可以滚动等等,您可以执行此操作:

listbox.SelectionMode = SelectionMode.None

答案 5 :(得分:3)

CheckedListBox不会以这种方式工作。 CheckedListBox.Items是字符串的集合,因此它们不能被“禁用”。

以下是一些可能对您有所帮助的解决方案的讨论:herehere

答案 6 :(得分:1)

解决方案是使用事件ItemChecking

_myCheckedListBox.ItemChecking += (s, e) => e.Cancel = true;

这将取消对每个项目的所有检查,但您可以随时执行更精细的解决方案,但测试当前的.SelectedItem

答案 7 :(得分:0)

以下是我在我写的帮助台应用程序中的表现:

首先,我做了这个,所以当我在表单加载过程中将它添加到列表中时,复选框显示为灰色:

    private void frmMain_Load(object sender, EventArgs e)
    {
        List<string> grpList = new List<string>();
        ADSI objADSI = new ADSI();

        grpList = objADSI.fetchGroups();

        foreach (string group in grpList)
        {
            if (group == "SpecificGroupName")
            {
                chkLst.Items.Add(group, CheckState.Indeterminate);

            }
            else
            {
                chkLst.Items.Add(group);
            }

        }

然后我使用了一个事件,以便在点击时确保它被点击:

    private void chkLst_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (chkLst.SelectedItem.ToString() == "SpecificGroupName")
        {
            chkLst.SetItemCheckState(chkLst.SelectedIndex, CheckState.Indeterminate);
        }
    }

这里的想法是在我的表单上设置它以便框检查项目点击/选择。这样我就可以一石二鸟。在表单加载期间首次检查和添加项目时,我可以防止此事件导致问题。加上select选中允许我使用此事件而不是项目检查事件。最终的想法是防止它在加载过程中弄乱。

你还会注意到索引号是什么并不重要,该变量是未知的,因为在我的应用程序中它正在从特定OU中存在的AD组中获取一组。

至于这是否是一个好主意,这取决于具体情况。我有另一个应用程序,其中要禁用的项目取决于另一个设置。在这个应用程序中,我只是希望帮助台看到这个组是必需的,所以他们不会去除它们。

答案 8 :(得分:0)

尝试以下代码:

Private Sub CheckedListBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CheckedListBox1.MouseUp
        If (Condition) Then
         Me.CheckedListBox1.SelectedIndex = -1
        End If
End Sub

答案 9 :(得分:0)

我认为另一种解决方案是使用Telerik组件。

RadListControl可以为您提供该选项:

enter image description here

答案 10 :(得分:0)

这对我有用:

"... So in the figure \ref{fig:3-\d+} we see ... similar to figure \ref{fig:3-\d+}..."

这意味着无法选择任何项目

  

无:无法选择任何项目。

有关详情,请点击此处查看:SelectionMode Enumeration