Custom Drawn ComboBox: DrawFocusRectangle doesn't work properly?

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,
 
G

Guest

:

[...]
<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>
[...]

I replaced the call to DrawFocusRectangle with the following:

if( ( e.State & DrawItemState.Selected ) != 0 )
ControlPaint.DrawFocusRectangle( e.Graphics, e.Bounds );

Thanks to MVP Herfried K. Wagner for pointing that out.

I really wonder why the regular call to DrawFocusRectangle doesn't work.
Would this be a .NET 1.1 issue?

Thanks,
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