C# Winform 工控机 多选下拉框 MultiComboBox

作为寄几第一个写的较复杂的控件,把整个规程完整的写下来。整体思路是用现成的ComboBox和CheckedListBox组合实现功能

平台:VS2022,Framework4.8

一、效果

 二、编写代码

1.新建“Windows窗体应用(.NET Framework),采用默认设置

2.添加新类,命名为MultiComboBox,在其中添加如下代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace MultiComboBox
{
    internal class MultiComboBox : UserControl
    {
        private ComboBox comboBox = new ComboBox();
        public CheckedListBox CheckedListBox { get; set; }  //为选项赋值的接口
        public List<string> SelectedItems1 { get; set; }  //传递已选择项目的接口

        public MultiComboBox()
        {
            this.VerticalScroll.Enabled = true;
            this.AutoSize = true;
            CheckedListBox=new CheckedListBox();
            CheckedListBox.CheckOnClick = true;
            CheckedListBox.Visible = false;
            comboBox=new ComboBox();
            comboBox.Width = 150;
            comboBox.DrawMode = DrawMode.OwnerDrawFixed;
            comboBox.IntegralHeight = false;
            comboBox.DroppedDown = false;
            comboBox.DropDownHeight = 1;
            comboBox.DropDownStyle = ComboBoxStyle.DropDown;
            comboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
            CheckedListBox.MouseUp += MouseUp1;
            CheckedListBox.MouseLeave += MouseLeave1;
            comboBox.MouseDown += MouseDown1;
            comboBox.DropDown += MouseLeave2;
            this.Controls.Add(comboBox);   //添加控件
        }
        //
        #region 订阅方法模块
        //
        private void MouseLeave1(object sender, EventArgs e)  //鼠标离开CheckedListBox,隐藏CheckedListBox
        {
            CheckedListBox.Hide();
        }
        private void MouseLeave2(object sender, EventArgs e)  //ComboBox下拉时,显示下拉框
        {
            // 显示下拉框
            CheckedListBox.Width = comboBox.Width;
            CheckedListBox.Size = new Size(comboBox.DropDownWidth, CheckedListBox.Items.Count*18);
            CheckedListBox.Location = new Point(comboBox.Left, comboBox.Height);
            Controls.Add(CheckedListBox);
            CheckedListBox.Visible = true;
        }
        private void MouseUp1(object sender, EventArgs e)  //在CheckedListBox中选择后,在ComboBox中显示相应项目
        {
            var list = new List<string>();
            foreach (var v in CheckedListBox.CheckedItems)  //将选择的项目加入list
            {
                list.Add(v.ToString());
            }
            comboBox.Text = String.Join(",", list);
            SelectedItems1 = list;  //把选项赋给传递接口
        }
        private void MouseDown1(object sender, EventArgs e) //在ComboBox的下拉三角按下鼠标时,不显示ComboBox的下拉框,显示CheckedListBox当作其下拉框
        {
            comboBox.DroppedDown = false;
        }
        #endregion 
        //
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // MultiComboBox
            // 
            this.Name = "MultiComboBox";
            this.Size = new System.Drawing.Size(414, 84);
            this.ResumeLayout(false);
        }
    }
}

3.重新生成解决方案后,在”Form1.cs[设计]“中拖入MultiComboBox、Button

 在Form1.cs中添加如下代码

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

namespace MultiComboBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //为下拉菜单添加选项
            multiComboBox1.CheckedListBox.Items.Add("1");
            multiComboBox1.CheckedListBox.Items.Add("2");
            multiComboBox1.CheckedListBox.Items.Add("3");
            multiComboBox1.CheckedListBox.Items.Add("4");
            multiComboBox1.CheckedListBox.Items.Add("5");
        }

        private void button1_Click(object sender, EventArgs e)  //单击button时,通过MultiComboBox的接口显示已选项目
        {
            var a = multiComboBox1.SelectedItems1;
            string b = string.Join(",", a);
            MessageBox.Show(b);
        }
    }
}

运行即可

注:

Form1.Designer.cs中自动添加的代码,有时候添加

this.multiComboBox1 = new MultiComboBox.MultiComboBox();

会报错,改为

this.multiComboBox1 = new MultiComboBox();

即可

  • 16
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值