This documentation is archived and is not being maintained.

MeasureItemEventArgs Class

Provides data for the MeasureItem event of the ListBox, ComboBox, CheckedListBox, and MenuItem controls.


Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Class MeasureItemEventArgs _
	Inherits EventArgs

The MeasureItemEventArgs type exposes the following members.

Public methodMeasureItemEventArgs(Graphics, Int32)Initializes a new instance of the MeasureItemEventArgs class.
Public methodMeasureItemEventArgs(Graphics, Int32, Int32)Initializes a new instance of the MeasureItemEventArgs class providing a parameter for the item height.

Public propertyGraphicsGets the Graphics object to measure against.
Public propertyIndexGets the index of the item for which the height and width is needed.
Public propertyItemHeightGets or sets the height of the item specified by the Index.
Public propertyItemWidthGets or sets the width of the item specified by the Index.

Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

This event is sent when the OwnerDraw property of ListBox, ComboBox, CheckedListBox, or MenuItem is set to true. It is used to tell the drawing function how to size an item.

For information about the event model, see Events and Delegates.

The following code example demonstrates how to use the Graphics property to perform custom drawing of the items in a ListBox.

Public Class Form1
   Inherits System.Windows.Forms.Form
   Private WithEvents listBox1 As System.Windows.Forms.ListBox
   Private components As System.ComponentModel.Container = Nothing

   Private FontSize As Single = 12.0F

   '  This sample displays a ListBox that contains a list of all the fonts
   '  installed on the system and draws each item in its respective font.
   Public Sub New()

      ' Populate control with the fonts installed on the system.
      Dim families As FontFamily() = FontFamily.Families

      Dim family As FontFamily
      For Each family In families
         Dim style As FontStyle = FontStyle.Regular

         ' Monotype Corsiva is only available in italic
         If family.Name = "Monotype Corsiva" Then
            style = style Or FontStyle.Italic
         End If

         listBox1.Items.Add(New ListBoxFontItem(New Font(family.Name, FontSize, style, GraphicsUnit.Point)))
      Next family
   End Sub

   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
         If (components IsNot Nothing) Then
         End If

         If (foreColorBrush IsNot Nothing) Then
         End If
      End If

   End Sub

   Private Sub InitializeComponent()
      Me.listBox1 = New System.Windows.Forms.ListBox()
      ' listBox1
      Me.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
      Me.listBox1.Location = New System.Drawing.Point(16, 48)
      Me.listBox1.Name = "listBox1"
      Me.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended
      Me.listBox1.Size = New System.Drawing.Size(256, 134)
      Me.listBox1.TabIndex = 0
      ' Form1
      Me.ClientSize = New System.Drawing.Size(292, 273)
      Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.listBox1})
      Me.Name = "Form1"
      Me.Text = "Form1"
   End Sub

   <STAThread()> Shared Sub Main()
      Application.Run(New Form1())
   End Sub

   Private Sub listBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles listBox1.MeasureItem
      Dim font As Font = CType(listBox1.Items(e.Index), ListBoxFontItem).Font
      Dim stringSize As SizeF = e.Graphics.MeasureString(font.Name, font)

      ' Set the height and width of the item
      e.ItemHeight = CInt(stringSize.Height)
      e.ItemWidth = CInt(stringSize.Width)
   End Sub

   ' For efficiency, cache the brush used for drawing.
   Private foreColorBrush As SolidBrush

   Private Sub listBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles listBox1.DrawItem
      Dim brush As Brush

      ' Create the brush using the ForeColor specified by the DrawItemEventArgs
      If foreColorBrush Is Nothing Then
         foreColorBrush = New SolidBrush(e.ForeColor)
         If Not foreColorBrush.Color.Equals(e.ForeColor) Then
            ' The control's ForeColor has changed, so dispose of the cached brush and
            ' create a new one.
            foreColorBrush = New SolidBrush(e.ForeColor)
         End If
      End If

      ' Select the appropriate brush depending on if the item is selected.
      ' Since State can be a combinateion (bit-flag) of enum values, you can't use
      ' "==" to compare them.
      If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
         brush = SystemBrushes.HighlightText
         brush = foreColorBrush
      End If

      ' Perform the painting.
      Dim font As Font = CType(listBox1.Items(e.Index), ListBoxFontItem).Font
      e.Graphics.DrawString(font.Name, font, brush, e.Bounds.X, e.Bounds.Y)
   End Sub

   '  A wrapper class for use with storing Fonts in a ListBox.  Since ListBox uses the
   '  ToString() of its items for the text it displays, this class is needed to return
   '  the name of the font, rather than its ToString() value.
   Public Class ListBoxFontItem
      Public Font As Font

      Public Sub New(ByVal f As Font)
         Font = f
      End Sub

      Public Overrides Function ToString() As String
         Return Font.Name
      End Function
   End Class
End Class

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.