C#实现多选下拉框

1、创建多选下拉框控件

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、程序调用

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、运行结果
在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值