C# DrawMode.OwnerDrawFixed e.DrawFocusRectangle() not contrasting text

Art Hansen 566 Reputation points
2022-09-20T09:55:11.64+00:00

Application: C# WinForm .NETFramework,Version=v4.7.2; VS 2022 on Win10 21H2 Pro

I have a combobox (named RetrieveCBX) on which I'm attempting to use a DrawMode of OwnerDrawFixed in order to differentiate a specific item from the rest of the listing. When an item in the dropdown list has mouse-over or is selected the focus rectangle highlighting is not contrasting the text as is normal:

243025-drawmode-problem.png

The result is clearly unreadable. I've tried with and without e.DrawFocusRectangle() but that doesn't change behaviour and MSDN examples for e.DrawFocusRectangle() merely show it as a line item in the code with no explanation in the Remarks. Following is (I believe) the relevant code. The DrawItem event handler contains my attempt at solving the problem.

        private void Select4Trkg_Load(object sender, EventArgs e)  
        {  
            _disinctProfs = DBconnect.LoadProfilesDistinctNames(_identity);  
            if (_disinctProfs.Count > 0) RetrieveCBX_NormalSetup();  
  
            if (multiple conditions)  // use Drawmode.Normal  
            else  RetrvCBX_Draw();  
 }  
        private int RetrieveCBX_NormalSetup()  
        {  
            _profiles = new List<string>();  
            foreach (var pname in _disinctProfs) _profiles.Add(pname.ProfName);  
            _profiles.Sort();  
            if (_state == Terms.Trk_Rvw) _profiles.Insert(0, "[Add New Profile]");  
  
            foreach (var p in _profiles) RetrieveCBX.Items.Add(p);  
        }  
        private void RetrieveCBX_Draw()  
        {  
            RetrieveCBX.DrawMode = DrawMode.OwnerDrawFixed;  
            RetrieveCBX.DrawItem += RetrieveCBX_DrawItem;  
        }  
        private void RetrieveCBX_DrawItem(object sender, DrawItemEventArgs e)  
        {  
            e.DrawBackground();  
            Font iFont;  
            for (int i = 0; i < _profiles.Count; i++)  
            {  
                if (i == 0)  
                {  
                    if (e.Index == i && (e.State == DrawItemState.Selected  
                                         || e.State == DrawItemState.Focus  
                                         || e.State == DrawItemState.HotLight))  
                    {  
                        iFont = new Font("Segoe UI", 9, FontStyle.Italic);  
                        e.Graphics.DrawString(_profiles[i], iFont, Brushes.White, new RectangleF(  
                            e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));  
                    }  
                    else if (e.Index == i)  
                    {  
                        iFont = new Font("Segoe UI", 9, FontStyle.Italic);  
                        e.Graphics.DrawString(_profiles[i], iFont, Brushes.Teal, new RectangleF(  
                            e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));  
                    }  
                }  
                else   
                {  
                    if (e.Index == i && (e.State == DrawItemState.Selected  
                                         || e.State == DrawItemState.Focus  
                                         || e.State == DrawItemState.HotLight))  
                    {  
                        iFont = new Font("Segoe UI", 9, FontStyle.Regular);  
                        e.Graphics.DrawString(_profiles[i], iFont, Brushes.White, new RectangleF(  
                            e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));  
                    }  
                    else if (e.Index == i)  
                    {  
                        iFont = new Font("Segoe UI", 9, FontStyle.Regular);  
                        e.Graphics.DrawString(_profiles[i], iFont, Brushes.Black, new RectangleF(  
                            e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));  
                    }  
                }  
            }  
           e.DrawFocusRectangle();  
        }  
  

There must be some parameter I'm missing. Any assistance will be appreciated.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,825 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,222 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,636 Reputation points
    2022-09-20T14:25:35.393+00:00

    This works for me to change the brush (then DrawString with myBrush ):

            Brush myBrush = System.Drawing.Brushes.Black;         
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)  
                myBrush = System.Drawing.Brushes.White;  
    

    with sample from MSDN :

    243037-combobox-od.gif


0 additional answers

Sort by: Most helpful