C#多选下拉框

创建窗体,拖以下控件
效果:
鼠标点击comboBox箭头显示下拉框,鼠标移开下拉框隐藏,
可单选,全选,选中的数据显示在右边框,默认textBox为隐藏,当有选中的时候textBox显示
在这里插入图片描述

创建ComboBoxMul 类继承ComboBox
在这里插入图片描述

ComboBoxMutiSelect类 代码

 class ComboBoxMutiSelect : ComboBox
    {
        private bool _IsMultiSelect;
        public bool IsMultiSelect { get => _IsMultiSelect; set => _IsMultiSelect = value; }
        public ComboBoxMutiSelect()
        {
            SetStyle(ControlStyles.DoubleBuffer |
            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.AllPaintingInWmPaint, true);
            UpdateStyles();
        }
    }

打开对话框的设计代码:对话框名字.Desiger.cs
在这里插入图片描述

private System.Windows.Forms.ComboBox comboBox1;
改为
 private ComboBoxMutiSelect comboBox1;
this.comboBox1 = new System.Windows.Forms.ComboBox();
改为
 this.comboBox1 = new ProductWindowsMultiCombo/*命名空间名称*/.ComboBoxMutiSelect();
//修改comboBox1控件绘制代码
// comboBox1
this.comboBox1.DropDownHeight = 1;
this.comboBox1.IntegralHeight = false;
this.comboBox1.IsMultiSelect = false;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(83, 5);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(79, 20);
this.comboBox1.TabIndex = 41;

打开窗体名.cs
在这里插入图片描述

窗体加载事件
.cs代码

 private void Form1_Load(object sender, EventArgs e)
        {
            #region 初始化
            this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
            comboBox1.IsMultiSelect = true;
            ListBoxPaint();
            textBox1.Visible = false;
            #endregion
        }
  private void ListBoxPaint()
        {

            #region 添加事件
            checkedListBox1.BorderStyle = BorderStyle.Fixed3D;
            checkedListBox1.Visible = false;
            checkedListBox1.CheckOnClick = true;
            checkedListBox1.ItemCheck += new ItemCheckEventHandler(this.checkedListBox1_ItemCheck_1);
            checkedListBox1.MouseLeave += new EventHandler(this.checkedListBox1_MouseLeave_1);
            #endregion

            #region 給checkedListBox1添加數據
            checkedListBox1.Items.Add("1"); 
            checkedListBox1.Items.Add("2");
            checkedListBox1.Items.Add("3");
            checkedListBox1.Items.Add("4");
            checkedListBox1.Items.Add("5");
            #endregion
        }
 //多選事件
        private void checkedListBox1_ItemCheck_1(object sender, EventArgs e)
        {
            if (comboBox1.IsMultiSelect == true)//如果需要多選
            {
                checkedListBox1.BeginUpdate();//開始繪製
                //定位comBox的坐標
                Point point = new Point();
                point.X = comboBox1.Left;
                point.Y = comboBox1.Bottom;
                //繪製 checkedListBox 
                checkedListBox1.FormattingEnabled = true;
                checkedListBox1.Location = point;//将checkedListBox定位到comboBox1的左下角
                checkedListBox1.Name = "checkedListBox1";
                checkedListBox1.TabIndex = 2;
                checkedListBox1.Visible = true;//繪製完成後,屬性設置為可見    
                panel1.Controls.Add(checkedListBox1);//将控件掛載到父控件
                checkedListBox1.EndUpdate();//停止绘制
            }
        }
//鼠標離開事件
        private void checkedListBox1_MouseLeave_1(object sender, EventArgs e)
        {
            //統計選中的item
            string str = "";
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                {
                    str += $"{checkedListBox1.Items[i].ToString()};";
                    textBox1.Visible = true;
                }
                
            }
            if (str == "")
            {
                textBox1.Visible = false;
            }
            //鼠標離開後,將內容顯示到控件
            textBox1.Text = str;
            comboBox1.Text = str;
            checkedListBox1.Visible = false;
        }
      
 //combox點擊事件
        private void comboBox1_Click(object sender, EventArgs e)
        { 
                checkedListBox1.Visible = true;
        }
//全選點擊事件
        private void lbSelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, true);
            }
        }
 //取消點擊事件
        private void lbSelectNo_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, false);
            }
        }

点击跳转至原作者

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值