C#实现多选下拉框

1、创建多选下拉框控件

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DFT_FFTApp.userCtrl
{
    /// <summary>
    /// 多选下拉控件
    /// </summary>
    public class MultiComboBox:UserControl
    {
        public ComboBox ComboBox { get; set; }
        public CheckedListBox CheckedListBox { get; set; }
        public ComboBox.ObjectCollection Items
        {
            get
            {
                return ComboBox?.Items;
            }
        }

        public MultiComboBox()
        {
            //面板
            this.VerticalScroll.Enabled = true;
            this.AutoSize = true;
            //多项列表
            CheckedListBox = new CheckedListBox();
            CheckedListBox.CheckOnClick = true;
            CheckedListBox.BorderStyle = BorderStyle.Fixed3D;
            CheckedListBox.Visible = false;
            CheckedListBox.Margin=new Padding(0);
            CheckedListBox.MouseUp += (ss, se) =>
            {
                //更新ComboBox显示文本
                var list = new List<string>();
                foreach(var item in CheckedListBox.CheckedItems)
                {
                    list.Add(item.ToString());
                }
                ComboBox.Text = string.Join(",", list);
                ComboBox.Tag = list;
            };
            CheckedListBox.MouseLeave += (ss, se) =>
            {
                //隐藏多选框
                CheckedListBox.Hide();
            };
            //下拉框
            ComboBox=new ComboBox();
            ComboBox.Width = 150;
            ComboBox.DrawMode = DrawMode.OwnerDrawFixed;
            ComboBox.IntegralHeight = false;
            ComboBox.DroppedDown = false;
            ComboBox.DropDownHeight = 1;
            ComboBox.Margin=new Padding(0);
            ComboBox.Location=new System.Drawing.Point(0,0);
            ComboBox.DropDownStyle = ComboBoxStyle.DropDown;
            ComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
            ComboBox.MouseDown += (ss, se) =>
            {
                ComboBox.DroppedDown = false;
            };
            ComboBox.MouseLeave += (ss, se) =>
            {
                //不在下拉区时隐藏多项列表
                var curMousePos = this.PointToClient(Control.MousePosition);
                var downArea = CheckedListBox.Location;
                if (curMousePos.X < downArea.X || curMousePos.X>(downArea.X+CheckedListBox.Width)
                || curMousePos.Y<downArea.Y || curMousePos.Y>(downArea.Y+CheckedListBox.Height))
                {
                    CheckedListBox.Hide();
                }
            };
            ComboBox.DropDown += (ss, se) =>
            {
                //显示下拉多选框
                CheckedListBox.Items.Clear();
                //添加并设置选中项
                var lastChecked = ComboBox.Tag as List<string>;
                ComboBox.BeginUpdate();
                foreach(var item in this.Items)
                {
                    var ck = false;
                    if(lastChecked!=null && lastChecked.Contains(item.ToString()))
                    {
                        ck = true;
                    }
                    CheckedListBox.Items.Add(item, ck);
                }
                //显示下拉框
                CheckedListBox.Width=ComboBox.Width;
                CheckedListBox.ItemHeight = ComboBox.ItemHeight;
                CheckedListBox.Size=new Size(ComboBox.DropDownWidth,this.Items.Count*18);
                CheckedListBox.Location = new Point(ComboBox.Left, ComboBox.Height);
                this.Controls.Add( CheckedListBox );
                CheckedListBox.Visible = true;
                ComboBox.EndUpdate();
            };
            //添加控件
            this.Controls.Add(ComboBox);
        }
    }
}

2、程序调用

csharp 复制代码
using DFT_FFTApp.userCtrl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DFT_FFTApp
{
    public partial class MainForm : Form
    {
        MultiComboBox multiComboBox;
        public MainForm()
        {
            InitializeComponent();

            multiComboBox = new MultiComboBox();
            multiComboBox.Items.AddRange(new string[] { "AIN1", "AIN2", "AIN3", "AIN4" });
            multiComboBox.Location = new Point(175, 15);
            this.Controls.Add(multiComboBox);
        }

        private void Btn_ComboBoxTest_Click(object sender, EventArgs e)
        {
            //显示选择项
            CheckedListBox CheckedListBox =multiComboBox.CheckedListBox;
            foreach(var item in CheckedListBox.CheckedItems)
            {
                Console.WriteLine(item);
            }
            //显示选项列表
            //foreach (string item in multiComboBox.Items)
            //{
            //    Console.WriteLine(item);
            //}
        }
    }
}

3、运行结果

相关推荐
夫琅禾费米线11 分钟前
[有趣的JavaScript] 为什么typeof null返回 object
开发语言·前端·javascript
三掌柜66611 分钟前
【腾讯云产品最佳实践】腾讯云CVM入门技术与实践:通过腾讯云快速构建云上应用
开发语言·腾讯云·perl
多来米199612 分钟前
小白学多线程(持续更新中)
java·开发语言
程序leo源2 小时前
C语言:操作符详解1
android·java·c语言·c++·青少年编程·c#
小柯J桑_3 小时前
C++:探索AVL树旋转的奥秘
开发语言·c++·avl树
skaiuijing5 小时前
Sparrow系列拓展篇:消息队列和互斥锁等IPC机制的设计
c语言·开发语言·算法·操作系统·arm
yngsqq5 小时前
c#注册机制作(根据机器码生成注册码和注册文件)
c#
雯0609~6 小时前
c#:winform调用bartender实现打印(学习整理笔记)
开发语言·c#
胜天半子_王二_王半仙7 小时前
c++源码阅读__smart_ptr__正文阅读
开发语言·c++·开源