如何禁用checkedlistbox中的复选框?

我在一个选中的列表框中有一些项目,我想禁用其中第一项的复选框。
即我想禁用CheckedListBox中的第一项,因为我想用视觉告诉用户该选项不可用。

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

myCheckedListBox.Items.Add(myItem, myState); 

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

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

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

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

但是,您可以使用ItemCheck事件轻松击败用户:

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

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

 checkedListBox1.SetItemCheckState(0, CheckState.Indeterminate); 

SetItemCheckState获取item的索引, CheckState Enum Indeterminate用于显示阴影外观

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

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

 listbox.SelectionMode = SelectionMode.None 

虽然这篇文章已经很老了,但最后一个补充的答案已于今年4月提交,
我希望这会对某人有所帮助。
我追求的是类似的东西:一个检查列表框,其行为类似于许多安装程序,它提供了一些选项列表,其中需要某些function,因此都会进行检查禁用。
感谢这篇文章( 我可以使用带有CheckedListBox的DrawItem事件处理程序吗? )我设法做到这一点,inheritance了CheckedListBox控件。
由于链接post中的OP状态,在CheckedListBox控件中, OnDrawItem事件永远不会被触发,因此子类化是必要的。
这是非常基本的,但它的工作原理。 这就是它的样子(上面的CheckBox用于比较):

选中列表框

注意 :禁用的项目确实被禁用:点击它没有任何影响(据我所知)。 这是代码:

 public class CheckedListBoxDisabledItems : CheckedListBox { private List _checkedAndDisabledItems = new List(); private List _checkedAndDisabledIndexes = new List(); 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); 

希望这可以帮助别人。

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

以下是有关可能对您有所帮助的解决方案的一些讨论: 此处和此处 。

解决方案是使用事件ItemChecking

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

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

这是我在我写的帮助台应用程序中的方式:

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

  private void frmMain_Load(object sender, EventArgs e) { List grpList = new List(); 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选中允许我使用此事件而不是项目检查事件。 最终的想法是防止它在加载过程中弄乱。

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

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

尝试以下代码:

 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 

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

RadListControl可以为您提供该选项:

在此处输入图像描述

这对我有用:

 checkedListBox1.SelectionMode = SelectionMode.None; 

这意味着不能选择任何项目

无:无法选择任何项目。

有关详细信息,请在此处进行检查: SelectionMode Enumeration 。