C#多选下拉菜单自定义控件
C#在winform项目中 多选下拉菜单自定义控件 。
由 ComboBox 和 CheckedListBox 组合形成。
效果:
自定义控件代码
HL_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 HL_MultiComboBox : UserControl { public ComboBox ComboBox = new ComboBox(); public CheckedListBox CheckedListBox { get; set; } //为选项赋值的接口 public List<string> SelectedItems { get; set; } //传递已选择项目的接口 public string Text { get { return this.ComboBox.Text; } set { this.ComboBox.Text=Text; } } public HL_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); //添加控件 //默认样式 this.ComboBox.Width = 200; this.ComboBox.Cursor = System.Windows.Forms.Cursors.Hand; this.ComboBox.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.ComboBox.Font = new System.Drawing.Font("微软雅黑", 10.28571F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.ComboBox.FormattingEnabled = true; this.ComboBox.Size = new System.Drawing.Size(200, 31); this.CheckedListBox.Font = new System.Drawing.Font("宋体", 10.28571F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.CheckedListBox.Cursor = System.Windows.Forms.Cursors.Hand; } // #region 订阅方法模块 // private void MouseLeave1(object sender, EventArgs e) //鼠标离开CheckedListBox,隐藏CheckedListBox { CheckedListBox.Hide(); } private void HL_MultiComboBox_Leave(object sender, EventArgs e) { CheckedListBox.Hide(); } private void MouseLeave2(object sender, EventArgs e) //ComboBox下拉时,显示下拉框 { // 显示下拉框 CheckedListBox.Width = ComboBox.Width-1; //if (CheckedListBox.Items.Count > 30) //{ CheckedListBox.Size = new Size(ComboBox.DropDownWidth, 300);//固定 最大显示 高度 // //this.CheckedListBox.Height = 300;//下拉菜单 最大显示 高度 //} //else //{ // 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); this.SelectedItems = list; //把选项赋给传递接口 } private void MouseDown1(object sender, EventArgs e) //在ComboBox的下拉三角按下鼠标时,不显示ComboBox的下拉框,显示CheckedListBox当作其下拉框 { ComboBox.DroppedDown = false; } #endregion } }
HL_MultiComboBox.Designer.cs
#region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.SuspendLayout(); // // MultiComboBox // this.Name = "HL_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 = 200; //为下拉菜单添加选项 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"); 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");//DataTable 赋值方式 //string strSqlList = "SELECT value_name FROM item_value where deleted=0 and value_name<>'' and item_name='数据来源' order by orderby+0 asc "; //DataTable dtList = clsDBHelperMySQL.m_Query(strSqlList);//执行sql //foreach (DataRow row in dtList.Rows) //{ // this.multiComboBox1.CheckedListBox.Items.Add(row[0]); //} } private void button1_Click(object sender, EventArgs e) { string strResult = ""; //取值方式 //一、 seleced方式 //if (this.multiComboBox1.SelectedItems != null) //{ // var strSelected = multiComboBox1.SelectedItems; // strResult = string.Join(",", strSelected); //} //二、 ComboBox.Text方式 strResult = this.multiComboBox1.Text; MessageBox.Show(strResult); //生成 SQL 语句 用 In 方式 //if (strContactSource != "") //{ // if (strContactSource.Contains(",") == false)// 在文字两侧加上单引号 // { // strContactSource = "'" + strContactSource + "'"; // } // else // { // strContactSource = strContactSource.Replace(",", "','"); // strContactSource = "'" + strContactSource + "'"; // } // strWhere = strWhere + " and ( contact_source in ( " + strContactSource + " ) ) "; //} }