Category / Section
How to customize the item height based on number of lines in WinForms EditableList?
1 min read
Customize the item height
In EditableList, it is possible to specify its item height with respect to its Text length, by handling its events named MeasureItem and DrawItem.
MeasureItem
This event helps to customize the Item height.
DrawItem
This event helps to customize the Item content appearance.
Following code example demonstrates the same.
C#
//Customize the Visibility of Items. void ListBox_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index >= 0) { e.DrawBackground(); e.DrawFocusRectangle(); e.Graphics.DrawString(item[e.Index], new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold), new SolidBrush(color[e.Index]), e.Bounds); } } //Determines the Size of the Item. void ListBox_MeasureItem(object sender, MeasureItemEventArgs e) { //Numlines is total number of lines in a text e.ItemHeight = 25*numLines; }
VB
'Customize the Visibility of Items. Private Sub ListBox_DrawItem(sender As Object, e As DrawItemEventArgs) If e.Index >= 0 Then e.DrawBackground() e.DrawFocusRectangle() e.Graphics.DrawString(item(e.Index), New Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold), New SolidBrush(color(e.Index)), e.Bounds) End If End Sub 'Determines the Size of the Item. Private Sub ListBox_MeasureItem(sender As Object, e As MeasureItemEventArgs) 'Numlines is total number of lines in a text e.ItemHeight = 25 * numLines End Sub
Figure 1. Item Height based on List of Items in EditableList with Image.
Samples:
C#: Determine Item Height Based on number of Item Lines C#
VB: Determine Item Height Based on number of Item Lines VB