2010-12-06 182 views

回答

3

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

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

8

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

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

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

thnx,但我想通过视觉告诉用户该选项不可用 – 2010-12-06 17:06:10

+1

“无法使用自定义绘图” - 不正确,但这会有点困难。自定义将* text *绘制为禁用,*和*使用此处提供的禁用代码会更容易。 – 2010-12-06 17:07:20

+1

重新实现自定义绘图失败了使用CheckedListBox的点,不妨使用ListBox。如上所述,使该选项不可用的简单方法是将其从Items集合中删除。 – 2010-12-06 17:11:45

1

的解决方案是使用事件ItemChecking

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

这将取消对每一个项目的所有检查,但你总是可以做更精细的解决方案,但测试目前.SelectedItem

7

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

checkedListBox1.SetItemCheckState(0, CheckState.Indeterminate); 

SetItemCheckState花费项目和CheckState枚举 不确定的指数是用来显示阴影外观

10

结合上述部分答案的2对我来说真是棒极了。 添加您的物品清单与:

myCheckedListBox.Items.Add(myItem, myState); 

哪里myState是CheckState.Indeterminate对于应禁用物品。 然后添加事件处理程序,以保持这些项目被改变:

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

这不允许你使用“不确定”在此列表中其正常目的,但它确实给一个看起来非常相似,什么人会期待一个残疾人物品,并提供正确的行为!

5

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

如果您有一个列表框,并且想要禁用所有的复选框,以便它们不能被点击,但不禁用控件,以便用户仍然可以滚动等等。你可以这样做:

listbox.SelectionMode = SelectionMode.None 
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); 
     } 
    } 

钍这里的一个主意是在我的表单上设置,以便该框检查项目点击/选择。这样我就可以一箭双雕杀死两只鸟。当表单加载期间第一次检查并添加项目时,我可以避免此事件引发问题。另外使它检查选择允许我使用此事件,而不是项目检查事件。最终的想法是防止在装载过程中发生混乱。

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

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

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 
0

我觉得一个替代的解决方案,使用Telerik组件。

一个RadListControl可以给你选项:

enter image description here

4

虽然这个职位是很老,最后添加的答案已经在今年四月提交,
,我希望这会帮助别人。
我是在类似之后:一个检查列表框,行为像 很多安装程序,它提供了一些选项,其中一些功能是必需的,因此都被禁用。
感谢这篇文章(Can I use a DrawItem event handler with a CheckedListBox?) 我设法做到这一点,子类CheckedListBox控件。
由于链接帖子中的OP状态,在CheckedListBox控件中,OnDrawItem事件永远不会被触发, 因此子类化是必需的。
这是非常基本的,但它的工作原理。 这是什么样子的(上面的复选框是比较):

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); 

希望这可以帮助别人。

0

这个工作对我来说:

checkedListBox1.SelectionMode = SelectionMode.None; 

这意味着没有可以选择项

无:无项目可以选择。

欲了解更多信息,你可以在这里检查:SelectionMode Enumeration