Articles in this section

How to customize the item height based on number of lines in WinForms EditableList?

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       

           Item height based on list of items in EditableList

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

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied