C#多选下拉菜单自定义控件

C#在winform项目中 多选下拉菜单自定义控件 。

由 ComboBox 和 CheckedListBox 组合形成。

效果:

自定义控件代码

MultiComboBox.cs

    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

使用方法

        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);

        }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值