11

In My Application i have added Combobox as shown in below picture

enter image description here

i have set the combobox property as

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

And now my question is how to set border style to combobox so that it will look nice.

I verified in below link

Flat style Combo box

My question is different from below link's.

Generic ComboBox in Windows Forms Application

How to override UserControl class to draw a custom border?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Dinesh Reddy Alla
  • 1,677
  • 9
  • 23
  • 47
  • Possible duplicate of [How to override UserControl class to draw a custom border?](http://stackoverflow.com/questions/3908384/how-to-override-usercontrol-class-to-draw-a-custom-border) – CodingGorilla Jan 19 '16 at 12:56
  • Read the question carefully. – Dinesh Reddy Alla Jan 19 '16 at 12:57
  • Windows forms controls don't have border styles, so if you want one while keeping the flat style, you will have to draw it yourself. – CodingGorilla Jan 19 '16 at 12:58
  • first check the question what you have put it as duplicate. There is no correct answer for that one. @CodingGorilla – Dinesh Reddy Alla Jan 19 '16 at 13:01
  • I withdrew the close vote, I still think that is your best solution though. – CodingGorilla Jan 19 '16 at 13:06
  • ok i just want to apply border only to combobox not all the controls. I already tried what you have suggested. – Dinesh Reddy Alla Jan 19 '16 at 13:07
  • 1
    I don't get it. You set the flat style and you don't like it, so you want to change it? Great, set it back to the system style so it looks like all other standard comboboxes. The flat style is an abomination in the first place. – Cody Gray - on strike Jan 19 '16 at 14:09
  • Since the combo box doesn't use `OnPaint` method, you should handle `WM_PAINT` message yourself in `WndProc` and paint the borders yourself. For more information take a look at [`ComboBox.FlatComboAdapter`](http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ComboBox.cs,79ca61e52b2766da). I shared a simple implementation in my answer. – Reza Aghaei Jan 20 '16 at 11:52
  • It seems you have to use magic to get a normally looking ComboBox. – Anders Lindén Jul 22 '19 at 22:52
  • Related post - [How to change the BackColor of a ComboBox when DropdownStyle is DropDownList?](https://stackoverflow.com/q/36345082/465053) – RBT Jan 01 '22 at 09:16

3 Answers3

16

You can inherit from ComboBox and override WndProc and handle WM_PAINT message and draw border for your combo box:

enter image description here

using System;
using System.Drawing;
using System.Windows.Forms;

public class FlatCombo : ComboBox
{
    private const int WM_PAINT = 0xF;
    private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
    Color borderColor = Color.Blue;
    public Color BorderColor
    {
        get { return borderColor; }
        set { borderColor = value; Invalidate(); }
    }
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_PAINT && DropDownStyle != ComboBoxStyle.Simple)
        {
            using (var g = Graphics.FromHwnd(Handle))
            {
                using (var p = new Pen(BorderColor))
                {
                    g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);

                    var d = FlatStyle == FlatStyle.Popup ? 1 : 0;
                    g.DrawLine(p, Width - buttonWidth - d,
                        0, Width - buttonWidth - d, Height);
                }
            }
        }
    }
}

Note:

  • In the above example I used fore color for border, you can add a BorderColor property or use another color.
  • If you don't like the left border of dropdown button, you can comment that DrawLine method.
  • You need to draw line when the control is RightToLeft from (0, buttonWidth) to (Height, buttonWidth)
  • To learn more about how to render a flat combo box, you can take a look at source code of internal ComboBox.FlatComboAdapter class of .Net Framework.

Flat ComboBox

You may also like Flat ComboBox:

enter image description here enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
4

CodingGorilla has the right answer, derive your own control from ComboBox and then paint the border yourself.

Here's a working example that paints a 1 pixel wide dark gray border:

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 border example
Normal on the left, my example on the right.

Equalsk
  • 7,954
  • 2
  • 41
  • 67
3

Another option is to draw the border yourself in the Parent control's Paint Event:

    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
PKanold
  • 139
  • 7
  • 1
    In my case the parent container was the form itself. C# equivalent code: `this.CreateGraphics().DrawRectangle(Pens.Gray, combo1EClientVersion.Left - 1, combo1EClientVersion.Top - 1, combo1EClientVersion.Width + 1, combo1EClientVersion.Height + 1);` – RBT Jan 01 '22 at 09:17
  • I guess we need to use `CreateGraphics()` instead of `CreateGraphics`. – Gray Programmerz Sep 08 '22 at 09:56