Combobox borderstyle
18,749
Solution 1
Put it in a panel, set Border on panel, Put cmb box in panel, set cmb box to dock fill and Border style flat.simple but not so elegant solution.
Solution 2
Create custom ComboBox control, and override it's WndProc
method. You can easily draw a border with ControlPaint.DrawBorder method:
public class ComboBoxWithBorder : ComboBox
{
private Color _borderColor = Color.Black;
private ButtonBorderStyle _borderStyle = ButtonBorderStyle.Solid;
private static int WM_PAINT = 0x000F;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
Graphics g = Graphics.FromHwnd(Handle);
Rectangle bounds = new Rectangle(0, 0, Width, Height);
ControlPaint.DrawBorder(g, bounds, _borderColor, _borderStyle);
}
}
[Category("Appearance")]
public Color BorderColor
{
get { return _borderColor; }
set
{
_borderColor = value;
Invalidate(); // causes control to be redrawn
}
}
[Category("Appearance")]
public ButtonBorderStyle BorderStyle
{
get { return _borderStyle; }
set
{
_borderStyle = value;
Invalidate();
}
}
}
BTW There is also overloaded DrawBorder method, which allows to set width of border. Use it if you need.
Author by
p0enkie
Updated on August 17, 2022Comments
-
p0enkie over 1 year
Hi I have set the combobox control's flatstyle to flat.
Is it possible to draw a border around this control?
The control does not have a borderstyle property. Any suggestions would be appreciated. Side note: I wish to keep the flatstyle flat if at all possible.
-
LarsTech over 11 yearsDoes this work? A ComboBox borders can't be overwritten in the paint event. I think you have to override WndProc and look for the non-client paint message. Of course, then it flickers rather ugly.
-
Sergey Berezovskiy over 11 years@HansPassant sorry, didn't have an ability to run it on VS. Now all is verified and working
-
p0enkie over 11 yearsThis is sufficient for my purposes thanx so much!