Listbox manual DrawItem large font size

I am trying to draw elements that end with the * symbol in red (and delete the * symbol) and draw other elements in black.

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

In addition, the DrawMode property for the box list is OwnerDrawVariable .

My code works fine when the listbox font is the default font.

But when I change the font size from 8.25 (the default size) to 14, half of the text is drawn in the list. like this: My listbox when size is 14!

But with the default font size, everything is correct.

What is the problem?

+6
source share
1 answer

You need to handle the MeasureItem event and set the height of the elements there:

 private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e) { e.ItemHeight = listBox1.Font.Height; } 
+6
source

Source: https://habr.com/ru/post/905821/


All Articles