Back to Devexpress

How to: Set Custom Height for Specific Items in ListBoxControl

windowsforms-9495-controls-and-libraries-editors-and-simple-controls-examples-how-to-set-custom-height-for-specific-items-in-listboxcontrol.md

latest1.3 KB
Original Source

How to: Set Custom Height for Specific Items in ListBoxControl

  • Oct 25, 2019

The following example shows how to set a custom height for specific items in a ListBoxControl by handling the BaseListBoxControl.MeasureItem event.

In this example, if a specific item contains a NewLine character (“\r\n” in C#), its height is doubled to fully display the item’s text.

csharp
using DevExpress.XtraEditors;

private void listBoxControl1_MeasureItem(object sender, MeasureItemEventArgs e) {
    ListBoxControl lb = sender as ListBoxControl;
    string itemValue = (string)lb.Items[e.Index];
    if (itemValue.Contains("\r\n"))
        e.ItemHeight = e.ItemHeight * 2;            
}
vb
Imports DevExpress.XtraEditors

Private Sub ListBoxControl1_MeasureItem(ByVal sender As System.Object, _
  ByVal e As MeasureItemEventArgs) Handles ListBoxControl1.MeasureItem
    Dim lb As ListBoxControl = sender
    Dim itemValue As String = lb.Items(e.Index)
    If (itemValue.Contains(vbCrLf)) Then e.ItemHeight = e.ItemHeight * 2
End Sub