CheckedListBox启用/禁用问题 [英] CheckedListBox Enablde/Disabled problem

查看:111
本文介绍了CheckedListBox启用/禁用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

推荐答案

据我所知,此控件仅提供对已检查状态的访问每个项目的状态,均为三态,但不能启用/禁用单个项目.

需要解决方法?创建具有类似行为但具有附加功能的自定义控件并不难:访问每个复选框.您需要做的就是:1)创建一个从System.Windows.Forms.Panel派生的自定义控件,2)用System.Windows.Forms.CheckBox的实例填充它,3)使用具有适当自动滚动边距的属性ScrollableControl.AutoScroll允许滚动,4)高级功能:对于复选框的每个实例,添加事件KeyDown的处理程序以模拟将焦点转移到下一个/上一个子复选框,覆盖Panel.OnPaint以便通过绘制对应的部分来显示所选项目的视觉反馈具有不同选择"颜色的背景.

这将是一项艰巨的工作,需要进行良好的自定义控件编程练习,但是这需要时间,因此,如果您真的想要它,请自己考虑一下.

—SA
To best of my understanding, this control provides only the the access to the checked status of each item, which is 3-state, but not to enable/disable individual items.

Need a work-around? It''s not too hard to create a custom control with somewhat similar behavior but with additional feature: access to each individual check box. All you need is: 1) create a custom control derived from System.Windows.Forms.Panel, 2) populate it with the instances of System.Windows.Forms.CheckBox, 3) use the property ScrollableControl.AutoScroll with proper auto-scroll margins to allow scrolling, 4) advanced feature: for each instance of the check box, add a handler of the event KeyDown to simulate shifting focus to next/previous child check box, override Panel.OnPaint to show some visual feed back of the selected item by painting corresponding part of the background in different "selection" color.

It would be quite a piece of work, good custom control programming exercise, but it will take time, so think by yourself if you really want it.

—SA


对于原始问题, SAKryukov 给出的解决方案2很好.
满足要求

使用选项checkedListBox1.CheckedItems.Count以及超过一定值的更多项将不会被选择.

OP可以在解决方案2的注释中指出,可以使用以下选项之一.

1.通过源自CheckedListBox

CheckedListBox 子类化为LimitedSelectCheckedListBox.在子类中添加属性MaxSelectCount以容纳允许的最大选择数.
在构造函数中,设置此属性的默认值.说1.
如果CheckedItems.Count == MaxSelectCount.
,请覆盖OnItemCheck 方法并将NewValue 设置为Unchecked 在设计器或代码中设置LimitedSelectCheckedListBox MaxSelectCount 属性.

For the original question the Solution 2 given by SAKryukov is good.

For the requirement

use the option checkedListBox1.CheckedItems.Count and beyond a certain value of more items will not be selected.

stated by OP in the comment to Solution 2, one of the following Options can be used.

1. By deriving from the CheckedListBox class

Subclass the CheckedListBox as LimitedSelectCheckedListBox. In the subclass add a Property MaxSelectCount to hold the maximum number of selections allowed.
In the constructor set the default value of this property. Say 1.
Override the OnItemCheck method and set the NewValue to Unchecked if the CheckedItems.Count == MaxSelectCount.
Set the MaxSelectCount property of the LimitedSelectCheckedListBox either in the designer or in the Code.

public class LimitedSelectCheckedListBox : CheckedListBox{
    public int MaxSelectCount { get; set; }
    public LimitedSelectCheckedListBox() {
        CheckOnClick = true;
        MaxSelectCount = 1;
    }
    protected override void OnItemCheck(ItemCheckEventArgs ice) {
        if (CheckedItems.Count == MaxSelectCount)
            ice.NewValue = CheckState.Unchecked;
        else
            base.OnItemCheck(ice);
    }
}



2.订阅CheckedListBox.ItemCheck并禁用选择



2. Subscribe to the CheckedListBox.ItemCheck and disable selection

//Declare a field to hold the maximum number of selections allowed.
int maxSelectCount
//Subscribe to the <code>ItemCheck </code>event say in the Form.Load event
checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
//Set the maximum select count.
maxSelectCount = 3;


private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
//Check if the CheckedItems Count equals the maximum number of 
//selections, if so disable selection
    if ((sender as CheckedListBox).CheckedItems.Count == maxSelectCount) 
        e.NewValue = CheckState.Unchecked;
}


这篇关于CheckedListBox启用/禁用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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