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