Problem with ComboBox in "OwnerDrawFixed" mode ...

  • Thread starter Thread starter Bert Hyman
  • Start date Start date
B

Bert Hyman

Using VS.2008, dotNet 3.5 and C# under WinXP Pro SP3.

The background color of the selection and the text box comes out dark
blue when I use the code from the examples in the doc.

I want right-justified text in some ComboBoxes in my application, so I'm
using the DrawItem event and using the code from the samples in the doc.
The DrawMode is set to ""DropDownList". Here's the exact code: namely:

StringFormat s = new StringFormat();
s.Alignment = StringAlignment.Far;
s.LineAlignment = StringAlignment.Far;

if (e.Index < 0)
return;

e.DrawBackground();

e.Graphics.DrawString(((ComboBox)sender).Items[e.Index].ToString(),
e.Font, Brushes.Black, e.Bounds, s);
e.DrawFocusRectangle();

With the code as is, when the cursor is placed over items in the
drop-down list, the background is dark blue and the black text is nearly
invisible.

Once the selection is made, the background of the text box is also dark
blue whenever the control is selected, but returns to white when another
control is active.

If I comment out the "e.DrawBackground()" call, the background color
remains white throughout the process.

Are all the examples simply wrong, or is there something else going on
here?
 
In Bert Hyman
Are all the examples simply wrong, or is there something else going on
here?

Looks like the examples are wrong, or at least incomplete.

After playing around a bit, I now see that I have to set the text color
based on the event state, to either HighlightText or WindowText
depending on whether the item is selected.
 
Back
Top