views:

3355

answers:

2

Hey. I have a problem with the highlighter in ComboBox. Recently I had to gray out certain items in a ComboBox and I did that by manually (programitically) drawing strings in the ComboBox. In a .NET combobox under the DrawMode.NORMAL, the lighlighter will automatically come when you click the arrow and the backcolor of the highlighter will be kinna blue by default. The problem is when we move the mouse over a item the forecolor of the hovered item changes to white, but when we draw the items manually (DrawMode.OwnerDrawVariable) it dosen't work. Can you help me with this ??

This is how I grayed out items,

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));
    }
}
+4  A: 

By default the text in a ComboBox is drawn in one of two colors:

SystemColors.WindowText

for non-highlighted items, or

SystemColors.HighlightText

for highlighted items.

These colors are not fixed, but can be configured by the user (e.g., through Control Panel). In a typical 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 that you get the right colors regardless of how the user has their system configured, and also to get the appropriate color for highlighted vs. non-highlighted text, instead of using Brushes.Black for your non-disabled text, use something like:

e.State == DrawItemState.Selected ?
    SystemBrushes.HighlightText : SystemBrushes.WindowText

This basically says, if the state of the item you're drawing (e.State) is Selected (highlighted), use SystemColors.HighlightText, otherwise use SystemColors.WindowText.

You may also want to use:

SystemBrushes.GrayText

instead of Brushes.Gray, again in case the user has a nonstandard color scheme and plain Gray doesn't look right. And, you probably should also use:

comboBox1.Font

instead of creating an Arial font, to make sure that the font matches the font defined for the ComboBox on the form. (Also creating a Font object and not disposing of it will cause a resource leak.)

Eric Rosenberger
A: 

Yeah. That 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 willdefinetly consider your suggestions on using the system brushes rather than the solid brush. Thanks for your solution.