Resolved ListBox OnMeasureItem

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I have a custom ListBox in which I'm inheriting the Framework's ListBox and am allowing the items to be displayed on more than 1 line, so I'm using the OnMeasureItem internally, which is working fine except if it's using a larger font, I want the item's height to take font size into consideration too. Here's my code:
VB.NET:
Public Class MultilineListbox
    Inherits System.Windows.Forms.ListBox

    Protected Overrides Sub OnMeasureItem(ByVal e As System.Windows.Forms.MeasureItemEventArgs)
        MyBase.OnMeasureItem(e)
        If Me.Items.Count > 0I Then
            Dim lines() As String = Me.GetItemText(Me.Items(e.Index)).Split(CChar(Environment.NewLine))
            e.ItemHeight *= lines.Length
        End If
    End Sub
End Class
I was thinking that e.ItemHeight would be the height of a single line of text (like normal) with the font's height already taken into consideration and I simply need to multiply it by the current item's number of lines since that's all I'm really doing, but that doesn't seem to be the case and I thought I would ask while researching. Any ideas?
 
omg, so simple:
VB.NET:
Protected Overrides Sub OnMeasureItem(ByVal e As System.Windows.Forms.MeasureItemEventArgs)
    If Me.Items.Count > 0I Then
        e.ItemHeight = CInt(e.Graphics.MeasureString(Me.GetItemText(Me.Items(e.Index)), Me.Font).Height + 0.499!)
    End If
    MyBase.OnMeasureItem(e)
End Sub
 
Back
Top