选中“全部”项目将取消选中核对表框中选择的其他项目 [英] Checking 'All' item shall unselect other items selected in the checklist box

查看:58
本文介绍了选中“全部”项目将取消选中核对表框中选择的其他项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为多选组合框添加了自定义控件(Checked List box + Combo box)。我将全部作为项目之一,始终在选中的列表框中进行检查。在下拉列表中,如果选择了其他任何项目,则选择全部,然后取消选中自动全部。

I have added custom control for multiselect combo box(Checked List box + Combo box). I have 'All' as one of the item which is always checked in the checked list box. On drop down, If any other item is selected other then 'All' then automaticall 'All' should be unchecked.

推荐答案

我看不懂你怎么做这与ComboBox一起。

我建议你只使用一个CheckedListBox。

无论如何,这里是创建一个使用CheckedListBox控件并按要求执行的自定义用户控件的代码。



I can't see how you can do this with ComboBox.
I suggest you use only a CheckedListBox.
Anyway, here is the code that creates a custom user control that uses a CheckedListBox control and does as requested.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class MyListBox : UserControl
    {
        public CheckedListBox.ObjectCollection Items
        {
            get
            {
                return checkedListBox.Items;
            }

            set
            {
                checkedListBox.Items.AddRange(value);
            }
        }

        public MyListBox()
        {
            InitializeComponent();
        }

        private void checkedListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (checkedListBox.SelectedIndex == 0)
            {
                bool isChecked = checkedListBox.GetItemChecked(0);
                for (int i = 1; i < checkedListBox.Items.Count; i++)
                {
                    checkedListBox.SetItemChecked(i, isChecked);
                }
            }

            if (checkedListBox.SelectedIndex != 0)
            {
                bool isChecked = checkedListBox.GetItemChecked(checkedListBox.SelectedIndex);
                if (!isChecked)
                {
                    checkedListBox.SetItemChecked(0, isChecked);
                }
            }
        }
    }
}





备注:您还应该将CheckOnClick属性更改为true。



Remark: You should also change the CheckOnClick property to true.


这篇关于选中“全部”项目将取消选中核对表框中选择的其他项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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