I'm trying to draw items that end of them is an *
character in red (and remove that *
character) and draw other items in black color.
this is my code:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground() ; //Draw our regular background
if (Microsoft.VisualBasic.Strings.Right(listBox1.Items[e.Index].ToString(), 1) == "*")
{
e.Graphics.DrawString(Microsoft.VisualBasic.Strings.Mid(listBox1.Items[e.Index].ToString(),1,listBox1.Items[e.Index].ToString().Length - 1), e.Font, Brushes.Red, e.Bounds); //Draw the item text in red!
}
else
{
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds); //Draw the item text in its regular color
}
}
Also DrawMode
property of the listbox is set to OwnerDrawVariable
.
My code is working fine when the font of listbox is the default font.
But When I change the font size from 8.25 (default size) to 14, half of the text draws on the listbox. like this:
But with default font size, everything is correct.
What is the problem?
You have to handle the MeasureItem event and set the height of the items there:
private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = listBox1.Font.Height;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With