如何变灰项目在经过列表框 [英] How to grey out item in a Checked List Box

查看:134
本文介绍了如何变灰项目在经过列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示一些项目的检查列表框。在框中的一些项目应该被禁用,但可见(取决于几个单选按钮的状态)。

I have a checked list box that displays a number of items. Some items in the box should be disabled but visible (depending on the state of a couple of radio buttons).

我已经能够禁用它们,让你无法选中或取消选中它们,通过使用ItemCheck事件处理程序,所以这不是一个问题。

I have been able to disable them so that you cannot check or uncheck them, by using the ItemCheck event handler, so that's not a problem.

所有我现在需要的是找到一种方法来变灰的项目。 我也尝试这样的:

All I need now is to find a way to grey out the items. I did try this:

myCheckedListBox.SetItemCheckState(index, CheckState.Indeterminate)

这使得项目的复选框灰色但检查。我需要它是灰色的,要么选中或取消选中,这取决于项目的状态。

This makes the item checkbox grey BUT checked. I need it to be grey and either checked or unchecked, depending on the state of the item.

有没有办法改变单一项目的出现在的CheckedListBox

Is there a way to change the appearance of a single item in a CheckedListBox

推荐答案

您必须创建自己的的CheckedListBox 是这样的:

You have to create your own CheckedListBox like this:

public class CustomCheckedList : CheckedListBox
{
    public CustomCheckedList()
    {
        DisabledIndices = new List<int>();
        DoubleBuffered = true;
    }
    public List<int> DisabledIndices { get; set; }
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        base.OnDrawItem(e);
        Size checkSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, System.Windows.Forms.VisualStyles.CheckBoxState.MixedNormal);
        int d = (e.Bounds.Height - checkSize.Height) / 2;                        
        if(DisabledIndices.Contains(e.Index)) CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(d,e.Bounds.Top + d), GetItemChecked(e.Index) ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedDisabled : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedDisabled);
    }
    protected override void OnItemCheck(ItemCheckEventArgs ice)
    {
        base.OnItemCheck(ice);
        if (DisabledIndices.Contains(ice.Index)) ice.NewValue = ice.CurrentValue;
    }
}
//To disable the item at index 0:
customCheckedList.DisableIndices.Add(0);

您可能想使用另一种类型的结构来存储禁用索引,因为名单,其中;&GT; 我用我的code也不行,它可以包含复制指数。但是,这对于示范的目的。我们只是需要一种方法来知道哪些项目应该被禁用相应呈现。

You may want to use another type of structure to store disabled indices because the List<> I used in my code is not OK, it can contain duplicated indices. But that's for demonstrative purpose. We just need a way to know which items should be disabled to render accordingly.

这篇关于如何变灰项目在经过列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆