首页 新闻 会员 周边

C# WinForm 自定义ComboBox控件,下拉框显示不出来

0
[已关闭问题] 关闭于 2021-04-06 10:08

WinForm 自定义控件 ComboBox,但是下拉框怎么显示出来?下拉框使用的是 UserControl

        private void Btn_Click(object sender, EventArgs e)
        {
            DisplayItemControl itemsControl = new DisplayItemControl(this, 20, _Items, BackColor, _BtnHoverColor, _BtnForeColor);
            itemsControl.Show();
        }

DisplayItemControl

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace OverrideComponents
{
    public partial class DisplayItemControl : UserControl
    {
        public DisplayItemControl(ComboBoxControl comboBox, int height, List<string> items, Color backColor, Color hoverColor, Color btnForeColor)
        {
            InitializeComponent();
            SetControlStyle(comboBox, height, items, backColor, hoverColor, btnForeColor);
        }

        private void SetControlStyle(ComboBoxControl comboBox, int height, List<string> items, Color backColor, Color hoverColor, Color btnForeColor)
        {
            int width = comboBox.Width;
            this.Location = new Point(this.Location.X, this.Location.Y + Height);
            this.Size = new Size(width, height * items.Count);
            AddBtns(width, height, items, backColor, hoverColor, btnForeColor);
        }

        private void AddBtns(int width, int height, List<string> items, Color backColor, Color hoverColor, Color btnForeColor)
        {
            Size btnSize= new Size(width, height);
            for (int i = 0; i < items.Count; i++)
            {
                Button btn = new Button();
                SetBtnStyle(btn);
                btn.Location = new Point(0,i*height);
                btn.Size = btnSize;
                btn.Text = items[i];
                btn.BackColor = backColor;
                btn.ForeColor = btnForeColor;
                btn.Tag = i;
                btn.MouseEnter += new EventHandler(item_MouseEnter);
                btn.MouseLeave += new EventHandler(item_MouseLeave);
                btn.MouseDown += new MouseEventHandler(item_MouseDown);
                btn.MouseUp += new MouseEventHandler(item_MouseUp);
                this.Controls.Add(btn);
            }
        }
        private void SetBtnStyle(Button btn)
        {
            btn.Margin = new Padding(0);
            btn.Padding = new Padding(0);
            btn.FlatStyle = FlatStyle.Flat;
        }
    }
}

Show不出来啊

echo_lovely的主页 echo_lovely | 小虾三级 | 园豆:1409
提问于:2021-04-06 09:57
< >
分享
所有回答(1)
0

需要个Form 来承载展示出来的UserControl

        private void Btn_Click(object sender, EventArgs e)
        {
            DisplayItemControl itemsControl = new DisplayItemControl(this, 20, _Items, BackColor, _BtnHoverColor, _BtnForeColor);
            itemsControl.Parent = this.ParentForm;
            itemsControl.Show();
        }

位置偏了,还好,我看看怎么调整

echo_lovely | 园豆:1409 (小虾三级) | 2021-04-06 10:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册