Reputation: 896
I'm trying to change the dispaly color of a ComboBox
when the DropdownStyle
property is DropdownList
. When the property is changed to Dropdown
from DropdownList
the color changes.
How can I control the view color of the dropdown boxes ?
Thanks
Upvotes: 14
Views: 30561
Reputation: 125197
You can set FlatStyle
property to Popup
. This way the back color will use in both DropDown
and DropDownList
mode.
If you don't like flat style or you need more customization on rendering of ComboBox
, you can use an owner-drawn ComboBox
. For example you can set DrawMode
property to OwnerDrawFixed
and handle DrawItem
event and draw the combo box based on your logic.
You may also be interested in the following posts to customize ComboBox:
Upvotes: 27
Reputation: 191
I created my own Usercontrol. You have to set the dropdown to Flatstyle=Flat and change the Backcolor=White. Then the code below will draw the border which is missing. Below is code and a pic of what it looks like. You can copy and paste this into your own namespace somewhere and name it what you like.
Note: You will need to add System.Windows.Forms; System.ComponentModel; And System.Drawing; to your Class.
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
public class KDCombo : ComboBox
{
public KDCombo()
{
BorderColor = Color.DimGray;
}
[Browsable(true)]
[Category("Appearance")]
[DefaultValue(typeof(Color), "DimGray")]
public Color BorderColor { get; set; }
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))
{
// Uncomment this if you don't want the "highlight border".
/*
using (var p = new Pen(this.BorderColor, 1))
{
g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
}*/
using (var p = new Pen(this.BorderColor, 2))
{
g.DrawRectangle(p, 0, 0, Width , Height );
}
}
}
}
}
Upvotes: 1
Reputation: 41
I have been using stack overflow for a couple of years without subscribing or contributing. It's my first choice whe looking for a solution because it generally supplies a solution and I can read it without having to zoom. At 81 years of age, I am fossilized, but "It’s kind of fun to be extinct." Thanks, Ogden Nash.
When background shading is applied to text, the reduced contrast makes it difficult for my old eyes to read it. I Googled the problem, and the offered solutions scared me off. I even considered cobbling up the functionality using graphics, but I needed several instances. Gotta be a way.
Cover the text part of the combobox with a textbox, and change the textbox to multiline to make its height match the combobox. Add a couple of event handlers and Bob's your uncle.
Private Sub cmbPoints_SelectedIndexChanged(sender As Object, e As EventArgs
)HandlescmbPoints.SelectedIndexChanged
' Make the selection visible in the textbox
txtPoints.Text = cmbPoints.Text
End Sub
Private Sub txtPoints_GotFocus(sender As Object, e As EventArgs
) Handles txtPoints.GotFocus
' Prevent the user changing the text.
cmbPoints.Focus()
End Sub
Upvotes: 4
Reputation: 21
Just like mentioned above; You can set FlatStyle property to Popup/Flat. This way the back color will use in both DropDown and DropDownList mode.
But then you wont have the look you expected. There's a trick i do where i create a panel and change its border property to FixedSingle. Change the color of the panel to as desired and then change its size property to match the size of your ComboBox. Eg to 80, 22. On the position where you had your ComboBox, place your panel. Place your combobox on the Panel. If you can fine tune its position, When you debug, you will find that your ComboBox looks like it has a border.
Upvotes: 2