In My Application i have added Combobox as shown in below picture
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?
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)
You can inherit from ComboBox
and override WndProc
and handle WM_PAINT
message and draw border for your combo box:
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:
BorderColor
property or use another color.DrawLine
method.RightToLeft
from (0, buttonWidth)
to (Height, buttonWidth)
ComboBox.FlatComboAdapter
class of .Net Framework.Flat ComboBox
You may also like Flat ComboBox:
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);
}
}
}
Normal on the left, my example on the right.
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-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With