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

  • Thread starter Thread starter Guest
  • Start date Start date
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,
 
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>
 
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.
 
Back
Top