Custom Drawn ComboBox: DrawFocusRectangle doesn't always work???

G

Guest

Hi there,

I made a custom drawn ComboBox. The DrawItem event handler is (explanation
follows)

<code>
private void OnDrawItem (
object sender,
System.Windows.Forms.DrawItemEventArgs e )
{
ComboBox theCombo = (ComboBox) sender;
DataRowView drv = (DataRowView) theCombo.Items[ e.Index ];
string comboTxt = (string) drv[ theCombo.DisplayMember ];

bool configOk = PropertyValidation.ValidateEntries( comboTxt );
Brush bBrush = configOk ? Brushes.LimeGreen : Brushes.Tomato;

e.Graphics.FillRectangle (
bBrush,
e.Bounds
);
e.Graphics.DrawString (
comboTxt,
e.Font,
new SolidBrush( e.ForeColor ),
e.Bounds,
StringFormat.GenericDefault
);
e.DrawFocusRectangle( );
}
</code>

The code retrieves the text that is displayed in the combo box, and checks
whether the configuration for that item is ok. Depending on this I use a
green color when the configuration for the item is ok, and a red color if the
configuration is not ok (in my case, colors LimeGreen and Tomato). So what I
do is fill the display rectangle with a color depending on configuration,
draw the text and then call DrawFocusRectangle.

The problem is that sometimes the Focus Rectangle is not drawn (say 50% of
the time). Is there an issue with the DrawFocusRectangle method call? Is
there something I'm doing wrong? Examples on the net all do it like that...

Kind regards,
 
H

Herfried K. Wagner [MVP]

TT (Tom Tempelaere) said:
I made a custom drawn ComboBox. The DrawItem event handler is (explanation
follows)

<code>
private void OnDrawItem (
object sender,
System.Windows.Forms.DrawItemEventArgs e )
{
[...]
e.DrawFocusRectangle( );
}
</code>
[...]
The problem is that sometimes the Focus Rectangle is not drawn (say 50% of
the time). Is there an issue with the DrawFocusRectangle method call? Is
there something I'm doing wrong? Examples on the net all do it like
that...

You may want to follow the approach taken in the sample below:

<URL:http://dotnet.mvps.org/dotnet/samples/controls/FontList.zip>
 
G

Guest

Herfried K. Wagner said:
TT (Tom Tempelaere) said:
I made a custom drawn ComboBox. The DrawItem event handler is (explanation
follows)

<code>
private void OnDrawItem (
object sender,
System.Windows.Forms.DrawItemEventArgs e )
{
[...]
e.DrawFocusRectangle( );
}
</code>
[...]
The problem is that sometimes the Focus Rectangle is not drawn (say 50% of
the time). Is there an issue with the DrawFocusRectangle method call? Is
there something I'm doing wrong? Examples on the net all do it like
that...

You may want to follow the approach taken in the sample below:

<URL:http://dotnet.mvps.org/dotnet/samples/controls/FontList.zip>

Thank you very much Herfried, your suggestion works just fine.

Kind regards,
Tom Tempelaere.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top