This is the code from a YouTube video on how to make a modern looking ComboBox. It is also an informative video on how to use already existing classes and the functions they have.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Drawing.Design;
namespace Test
{
[ToolboxItem(true)]
[DefaultEvent("OnSelectedIndexChanged")]
class MyComboBox : UserControl
{
private Color backColor = Color.MediumSlateBlue;
private Color iconColor = Color.WhiteSmoke;
private Color listBackColor = Color.FromArgb(230, 228, 245);
private Color listTextColor = Color.DimGray;
private Color borderColor = Color.MediumSlateBlue;
private int borderSize = 1;
// Item
private ComboBox cmbList;
private Label lblText;
private Button btnIcon;
// Properties
//-> Appearance
[Category("Appearance")]
public new Color BackColor
{
get => backColor;
set
{
backColor = value;
lblText.BackColor = backColor;
btnIcon.BackColor = backColor;
}
}
[Category("Appearance")]
public Color IconColor
{
get => iconColor;
set
{
iconColor = value;
btnIcon.Invalidate();
}
}
[Category("Appearance")]
public Color ListBackColor
{
get => listBackColor;
set
{
listBackColor = value;
cmbList.BackColor = listBackColor;
}
}
[Category("Appearance")]
public Color ListTextColor
{
get => listTextColor;
set
{
listTextColor = value;
cmbList.ForeColor = listTextColor;
}
}
[Category("Appearance")]
public Color BorderColor
{
get => borderColor;
set
{
borderColor = value;
base.BackColor = borderColor;
}
}
[Category("Appearance")]
public int BorderSize
{
get => borderSize;
set
{
borderSize = value;
this.Padding = new Padding(borderSize);
AdjustComboBoxDimensions();
}
}
[Category("Appearance")]
public override Color ForeColor
{
get => base.ForeColor;
set
{
base.ForeColor = value;
lblText.ForeColor = value;
}
}
[Category("Appearance")]
public override Font Font
{
get => base.Font;
set
{
base.Font = value;
lblText.Font = value;
cmbList.Font = value;
}
}
[Category("Appearance")]
public string Texts
{
get { return lblText.Text; }
set { lblText.Text = value; }
}
[Category("Appearance")]
public ComboBoxStyle DropDownStyle
{
get => cmbList.DropDownStyle;
set
{
if (cmbList.DropDownStyle != ComboBoxStyle.Simple)
cmbList.DropDownStyle = value;
}
}
//-> Data
[Category("Data")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[Localizable(true)]
[MergableProperty(false)]
public ComboBox.ObjectCollection Items
{
get => cmbList.Items;
}
[Category("Data")]
[AttributeProvider(typeof(IListSource))]
[DefaultValue(null)]
[RefreshProperties(RefreshProperties.Repaint)]
public object DataSource
{
get { return cmbList.DataSource; }
set { cmbList.DataSource = value; }
}
[Category("Data")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[EditorBrowsable(EditorBrowsableState.Always)]
[Localizable(true)]
public AutoCompleteStringCollection AutoCompleteCustomSource
{
get { return cmbList.AutoCompleteCustomSource; }
set { cmbList.AutoCompleteCustomSource = value; }
}
[Category("Data")]
[Browsable(true)]
[DefaultValue(AutoCompleteSource.None)]
[EditorBrowsable(EditorBrowsableState.Always)]
public AutoCompleteSource AutoCompleteSource
{
get { return cmbList.AutoCompleteSource; }
set
{
cmbList.AutoCompleteSource = value;
}
}
[Category("Data")]
[Browsable(true)]
[DefaultValue(AutoCompleteMode.None)]
[EditorBrowsable(EditorBrowsableState.Always)]
public AutoCompleteMode AutoCompleteMode {
get { return cmbList.AutoCompleteMode; }
set
{
cmbList.AutoCompleteMode = value;
}
}
[Category("Data")]
[Bindable(true)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public object SelectedItem
{
get => cmbList.SelectedItem;
set => cmbList.SelectedItem = value;
}
[Category("Data")]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int SelectedIndex
{
get => cmbList.SelectedIndex;
set => cmbList.SelectedIndex = value;
}
//Events
public event EventHandler OnSelectedIndexChanged;
//constructor
public MyComboBox()
{
cmbList = new ComboBox();
lblText = new Label();
btnIcon = new Button();
this.SuspendLayout();
//ComoBox: Dropdown List;
cmbList.BackColor = listBackColor;
cmbList.Font = new Font(this.Font.Name, 10F);
cmbList.ForeColor = listTextColor;
cmbList.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChagned);
cmbList.TextChanged += new EventHandler(ComboBox_TextChanged);
//Buton: icon
btnIcon.Dock = DockStyle.Right;
btnIcon.FlatStyle = FlatStyle.Flat;
btnIcon.FlatAppearance.BorderSize = 0;
btnIcon.BackColor = backColor;
btnIcon.Size = new Size(30, 30);
btnIcon.Cursor = Cursors.Hand;
btnIcon.Click += new EventHandler( IconClick);
btnIcon.Paint += new PaintEventHandler( Icon_Paint);
// Lable:Text
lblText.Dock = DockStyle.Fill;
lblText.AutoSize = false;
lblText.BackColor = BackColor;
lblText.TextAlign = ContentAlignment.MiddleLeft;
lblText.Padding = new Padding(8, 0, 0, 0);
lblText.Font = new Font(this.Font.Name, 10F);
lblText.Click += new EventHandler(Surface_Click);
//User Control:
this.Controls.Add(lblText);
this.Controls.Add(btnIcon);
this.Controls.Add(cmbList);
this.MinimumSize = new Size(200, 30);
this.Size = new Size(200, 30);
this.ForeColor = Color.DimGray;
this.Padding = new Padding(borderSize);
base.BackColor = borderColor;
this.ResumeLayout();
AdjustComboBoxDimensions();
}
// Private Methods
private void AdjustComboBoxDimensions()
{
cmbList.Width = lblText.Width;
cmbList.Location = new Point()
{
X = this.Width - this.Padding.Right - cmbList.Width,
Y = lblText.Bottom - cmbList.Height
};
}
private void Surface_Click(object sender, EventArgs e)
{
// Select a combo box
cmbList.Select();
if (cmbList.DropDownStyle == ComboBoxStyle.DropDown)
cmbList.DroppedDown = true;
}
private void Icon_Paint(object sender, PaintEventArgs e)
{
//Field
int iconWidth = 14;
int iconHeight = 6;
var rectIcon = new Rectangle((btnIcon.Width - iconWidth) / 2, (btnIcon.Height - iconHeight) / 2, iconWidth, iconHeight);
Graphics graph = e.Graphics;
//Craw down arrow
using (GraphicsPath path = new GraphicsPath())
using (Pen pen = new Pen(iconColor, 2))
{
graph.SmoothingMode = SmoothingMode.AntiAlias;
path.AddLine(rectIcon.X, rectIcon.Y, rectIcon.X + (iconWidth / 2), rectIcon.Bottom);
path.AddLine(rectIcon.X +(iconWidth / 2), rectIcon.Bottom, rectIcon.Right, rectIcon.Y);
graph.DrawPath( pen, path);
}
}
private void IconClick(object sender, EventArgs e)
{
cmbList.Select();
cmbList.DroppedDown = true;
}
private void ComboBox_TextChanged(object sender, EventArgs e)
{
lblText.Text = cmbList.Text;
}
private void ComboBox_SelectedIndexChagned(object sender, EventArgs e)
{
if (OnSelectedIndexChanged != null)
OnSelectedIndexChanged.Invoke( sender, e);
lblText.Text = cmbList.Text;
}
}
}