• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
稻园
帮助1

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

平台:VS2022,Framework4.8

一、效果

学新通

 二、编写代码

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

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

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Drawing;
  4.  
    using System.Windows.Forms;
  5.  
     
  6.  
    namespace MultiComboBox
  7.  
    {
  8.  
    internal class MultiComboBox : UserControl
  9.  
    {
  10.  
    private ComboBox comboBox = new ComboBox();
  11.  
    public CheckedListBox CheckedListBox { get; set; } //为选项赋值的接口
  12.  
    public List<string> SelectedItems1 { get; set; } //传递已选择项目的接口
  13.  
     
  14.  
    public MultiComboBox()
  15.  
    {
  16.  
    this.VerticalScroll.Enabled = true;
  17.  
    this.AutoSize = true;
  18.  
    CheckedListBox=new CheckedListBox();
  19.  
    CheckedListBox.CheckOnClick = true;
  20.  
    CheckedListBox.Visible = false;
  21.  
    comboBox=new ComboBox();
  22.  
    comboBox.Width = 150;
  23.  
    comboBox.DrawMode = DrawMode.OwnerDrawFixed;
  24.  
    comboBox.IntegralHeight = false;
  25.  
    comboBox.DroppedDown = false;
  26.  
    comboBox.DropDownHeight = 1;
  27.  
    comboBox.DropDownStyle = ComboBoxStyle.DropDown;
  28.  
    comboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
  29.  
    CheckedListBox.MouseUp = MouseUp1;
  30.  
    CheckedListBox.MouseLeave = MouseLeave1;
  31.  
    comboBox.MouseDown = MouseDown1;
  32.  
    comboBox.DropDown = MouseLeave2;
  33.  
    this.Controls.Add(comboBox); //添加控件
  34.  
    }
  35.  
    //
  36.  
    #region 订阅方法模块
  37.  
    //
  38.  
    private void MouseLeave1(object sender, EventArgs e) //鼠标离开CheckedListBox,隐藏CheckedListBox
  39.  
    {
  40.  
    CheckedListBox.Hide();
  41.  
    }
  42.  
    private void MouseLeave2(object sender, EventArgs e) //ComboBox下拉时,显示下拉框
  43.  
    {
  44.  
    // 显示下拉框
  45.  
    CheckedListBox.Width = comboBox.Width;
  46.  
    CheckedListBox.Size = new Size(comboBox.DropDownWidth, CheckedListBox.Items.Count*18);
  47.  
    CheckedListBox.Location = new Point(comboBox.Left, comboBox.Height);
  48.  
    Controls.Add(CheckedListBox);
  49.  
    CheckedListBox.Visible = true;
  50.  
    }
  51.  
    private void MouseUp1(object sender, EventArgs e) //在CheckedListBox中选择后,在ComboBox中显示相应项目
  52.  
    {
  53.  
    var list = new List<string>();
  54.  
    foreach (var v in CheckedListBox.CheckedItems) //将选择的项目加入list
  55.  
    {
  56.  
    list.Add(v.ToString());
  57.  
    }
  58.  
    comboBox.Text = String.Join(",", list);
  59.  
    SelectedItems1 = list; //把选项赋给传递接口
  60.  
    }
  61.  
    private void MouseDown1(object sender, EventArgs e) //在ComboBox的下拉三角按下鼠标时,不显示ComboBox的下拉框,显示CheckedListBox当作其下拉框
  62.  
    {
  63.  
    comboBox.DroppedDown = false;
  64.  
    }
  65.  
    #endregion
  66.  
    //
  67.  
    private void InitializeComponent()
  68.  
    {
  69.  
    this.SuspendLayout();
  70.  
    //
  71.  
    // MultiComboBox
  72.  
    //
  73.  
    this.Name = "MultiComboBox";
  74.  
    this.Size = new System.Drawing.Size(414, 84);
  75.  
    this.ResumeLayout(false);
  76.  
    }
  77.  
    }
  78.  
    }
学新通

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

学新通

 在Form1.cs中添加如下代码

  1.  
    using System.Data;
  2.  
    using System.Drawing;
  3.  
    using System.Linq;
  4.  
    using System.Text;
  5.  
    using System.Threading.Tasks;
  6.  
    using System.Windows.Forms;
  7.  
     
  8.  
    namespace MultiComboBox
  9.  
    {
  10.  
    public partial class Form1 : Form
  11.  
    {
  12.  
    public Form1()
  13.  
    {
  14.  
    InitializeComponent();
  15.  
    }
  16.  
     
  17.  
    private void Form1_Load(object sender, EventArgs e)
  18.  
    {
  19.  
    //为下拉菜单添加选项
  20.  
    multiComboBox1.CheckedListBox.Items.Add("1");
  21.  
    multiComboBox1.CheckedListBox.Items.Add("2");
  22.  
    multiComboBox1.CheckedListBox.Items.Add("3");
  23.  
    multiComboBox1.CheckedListBox.Items.Add("4");
  24.  
    multiComboBox1.CheckedListBox.Items.Add("5");
  25.  
    }
  26.  
     
  27.  
    private void button1_Click(object sender, EventArgs e) //单击button时,通过MultiComboBox的接口显示已选项目
  28.  
    {
  29.  
    var a = multiComboBox1.SelectedItems1;
  30.  
    string b = string.Join(",", a);
  31.  
    MessageBox.Show(b);
  32.  
    }
  33.  
    }
  34.  
    }
学新通

运行即可

注:

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

this.multiComboBox1 = new MultiComboBox.MultiComboBox();

会报错,改为

this.multiComboBox1 = new MultiComboBox();

即可

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbigcb
系列文章
更多 icon
同类精品
更多 icon
继续加载