Changing the highlight color of a ComboBox in c #
Hey. I have a problem with a marker in a ComboBox. I recently had to blacken some items in the ComboBox, and I did it manually (programmatically) by drawing lines in the ComboBox . In the .NET combobox under DrawMode.NORMAL , the lighlighter will automatically come when you click the arrow, and the highlight color will be the default kinn. The problem is that we move the mouse over the element that changed the forecolor of the hover to white, but when we draw the elements by hand ( DrawMode.OwnerDrawVariable ) it doesn't work. Can you help me with this?
This is how I colored the elements,
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
int index = e.Index;
CombinationEntry aFunction = comboBox1.Items[index] as CombinationEntry; //CombinationEntry is a custom object to hold the gray info. Gray if not available and black if available
if (aFunction.myIsAvailable)
{
e.Graphics.DrawString(aFunction.ToString(), new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.Black, new Point(e.Bounds.X, e.Bounds.Y));
}
else
{
e.Graphics.DrawString(aFunction.ToString(), new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.Gray, new Point(e.Bounds.X, e.Bounds.Y));
}
}
By default, text in a ComboBox is drawn in one of two colors:
SystemColors.WindowText
for unselected items, or
SystemColors.HighlightText
for selected items.
These colors are not fixed, but can be customized by the user (for example, via the control panel). In the normal Windows color scheme, WindowText is black and HighlightText is white, but this is not always the case if the color scheme has been reconfigured.
To ensure the correct colors no matter how the user has configured their system, and also get the correct color for selected or unselected text, instead of using Brushes.Black for your text without disabling, use something like:
e.State == DrawItemState.Selected ? SystemBrushes.HighlightText : SystemBrushes.WindowText
Basically it says that if the state of the item you are drawing (e.State) is selected (highlighted) use SystemColors.HighlightText, otherwise use SystemColors.WindowText.
You can also use:
SystemBrushes.GrayText
instead of Brushes.Gray, again if the user has a custom color scheme and the usual gray doesn't look like this. And you should probably also use:
comboBox1.Font
instead of creating an Arial font, so that the font matches the font defined for the ComboBox on the form. (Also creating a Font object rather than deleting it will leak the resource.)
source to share
Yes. It was really helpful. Also I tried to do:
if (e.State == ((DrawItemState.NoAccelerator | DrawItemState.NoFocusRect) |
(DrawItemState.Selected |
DrawItemState.NoAccelerator |
DrawItemState.NoFocusRect)))
{
e.Graphics.DrawString(aFunction.ToString(),
new Font("Arial", 10, FontStyle.Regular,
GraphicsUnit.Pixel),
SystemBrushes.HighlightText,
new Point(e.Bounds.X, e.Bounds.Y));
}
Which gave me what I expected. I will definitely review your suggestions for using system brushes rather than solid brushes. Thanks for your decision.