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不出来啊
需要个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();
}