C#在winform项目中 多选下拉菜单自定义控件 。
由 ComboBox 和 CheckedListBox 组合形成。
效果:
自定义控件代码
MultiComboBox.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace HL_MultiComboBox { public partial class MultiComboBox : UserControl { public ComboBox ComboBox = new ComboBox(); public CheckedListBox CheckedListBox { get; set; } //为选项赋值的接口 public List<string> SelectedItems1 { get; set; } //传递已选择项目的接口 public MultiComboBox() { InitializeComponent(); 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 } }
MultiComboBox.Designer.cs
#region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.SuspendLayout(); // // MultiComboBox // this.Name = "MultiComboBox"; this.Size = new System.Drawing.Size(278, 50); this.ResumeLayout(false); } #endregion
使用方法
拖 multComboBox 控件至窗体中 名为 multiComboBox1
private void Form1_Load(object sender, EventArgs e) { this.multiComboBox1.ComboBox.Width = 300; //为下拉菜单添加选项 this.multiComboBox1.CheckedListBox.Items.Add("1"); this.multiComboBox1.CheckedListBox.Items.Add("2"); this.multiComboBox1.CheckedListBox.Items.Add("3"); this.multiComboBox1.CheckedListBox.Items.Add("4"); this.multiComboBox1.CheckedListBox.Items.Add("5"); this.multiComboBox1.CheckedListBox.Items.Add("6"); this.multiComboBox1.CheckedListBox.Items.Add("7"); this.multiComboBox1.CheckedListBox.Items.Add("8"); this.multiComboBox1.CheckedListBox.Items.Add("9"); this.multiComboBox1.CheckedListBox.Items.Add("10"); } private void button1_Click(object sender, EventArgs e) { var strSelected = multiComboBox1.SelectedItems1; string strResult = string.Join(",", strSelected); MessageBox.Show(strResult); }
标签:控件,自定义,ComboBox,System,multiComboBox1,Add,Items,CheckedListBox,下拉菜单 From: https://www.cnblogs.com/hailexuexi/p/18179765