How to apply border to combo box in windows form application?

In my application, I added Combobox, as shown in the image below.

enter image description here

I set the combobox property as

cmbDatefilter.FlatStyle = System.Windows.Forms.FlatStyle.Flat; 

And now my question is how to set the border style in combobox so that it looks good.

I checked the link below

Flat style combo box

My question is different from below link.

Generic ComboBox in a Windows Forms Application

How to override the UserControl class to draw a user border?

+9
source share
3 answers

You can inherit from ComboBox and redefine WndProc , process the WM_PAINT message and draw a border for your combo box:

enter image description here enter image description here

 public class FlatCombo:ComboBox { private const int WM_PAINT = 0xF; private int buttonWidth= SystemInformation.HorizontalScrollBarArrowWidth; protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_PAINT) { using (var g = Graphics.FromHwnd(Handle)) { using (var p = new Pen(this.ForeColor)) { g.DrawRectangle(p, 0, 0, Width - 1, Height - 1); g.DrawLine(p, Width - buttonWidth, 0, Width - buttonWidth, Height); } } } } } 

Note:

  • In the above example, I used the foreground color for the border, you can add the BorderColor or use a different color.
  • If you do not like the left border of the dropdown menu, you can comment on this DrawLine method.
  • You need to draw a line when the RightToLeft control is from (0, buttonWidth) to (Height, buttonWidth)
  • To learn more about how to display a flat combo box, you can take a look at the source code of the internal ComboBox.FlatComboAdapter of the .Net Framework class.
+7
source

CodingGorilla has the correct answer, gets its own control from the ComboBox, and then paint the border on its own.

Here is a working example that draws a dark gray border 1 pixel wide:

 class ColoredCombo : ComboBox { protected override void OnPaintBackground(PaintEventArgs e) { base.OnPaintBackground(e); using (var brush = new SolidBrush(BackColor)) { e.Graphics.FillRectangle(brush, ClientRectangle); e.Graphics.DrawRectangle(Pens.DarkGray, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1); } } } 

Custom combobox frame example
Normal on the left, my example on the right.

+2
source

Another option is to draw the border yourself in the Paint event of the parent control:

  Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint Panel1.CreateGraphics.DrawRectangle(Pens.Black, ComboBox1.Left - 1, ComboBox1.Top - 1, ComboBox1.Width + 1, ComboBox1.Height + 1) End Sub 

-O-O-

+1
source

All Articles