Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ComboBox Border Color in Windows Forms

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?

like image 586
Dinesh Reddy Alla Avatar asked Jan 19 '16 12:01

Dinesh Reddy Alla


People also ask

How do I change the ComboBox border color in VB net?

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)


3 Answers

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

like image 198
Reza Aghaei Avatar answered Oct 12 '22 22:10

Reza Aghaei


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.

like image 25
Equalsk Avatar answered Oct 12 '22 22:10

Equalsk


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

-OO-

like image 3
PKanold Avatar answered Oct 12 '22 23:10

PKanold