Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ComboBox.MeasureItem Event

Occurs each time an owner-drawn ComboBox item needs to be drawn and when the sizes of the list items are determined.

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

'Declaration
Public Event MeasureItem As MeasureItemEventHandler

You can create an event handler for this event to specify the size an item is made before it is drawn in the DrawItem event.

For more information about handling events, see Handling and Raising Events.

The following code example demonstrates how to create an owner-drawn combo box by setting the DrawMode property to OwnerDrawnVariable and handling the DrawItem and MeasureItem events. It also demonstrates setting the DropDownWidth and DropDownStyle properties. To run the example, paste the following code in a form. Call the InitializeComboBox method in the form's constructor or Load event.


    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Private animals() As String 

    ' This method initializes the owner-drawn combo box. 
    ' The drop-down width is set much wider than the size of the combo box 
    ' to accomodate the large items in the list.  The drop-down style is set to  
    ' ComboBox.DropDown, which requires the user to click on the arrow to  
    ' see the list. 
    Private Sub InitializeComboBox()
        Me.ComboBox1 = New ComboBox
        Me.ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
        Me.ComboBox1.Location = New System.Drawing.Point(10, 20)
        Me.ComboBox1.Name = "ComboBox1" 
        Me.ComboBox1.Size = New System.Drawing.Size(100, 120)
        Me.ComboBox1.DropDownWidth = 250
        Me.ComboBox1.TabIndex = 0
        Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
        animals = New String() {"Elephant", "c r o c o d i l e", "lion"}
        ComboBox1.DataSource = animals
        Me.Controls.Add(Me.ComboBox1)
    End Sub 

    ' If you set the Draw property to DrawMode.OwnerDrawVariable,  
    ' you must handle the MeasureItem event. This event handler  
    ' will set the height and width of each item before it is drawn.  
     Private Sub ComboBox1_MeasureItem(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
            Handles ComboBox1.MeasureItem

        Select Case e.Index
            Case 0
                e.ItemHeight = 45
            Case 1
                e.ItemHeight = 20
            Case 2
                e.ItemHeight = 35
        End Select
        e.ItemWidth = 260

    End Sub 

    ' You must handle the DrawItem event for owner-drawn combo boxes.   
    ' This event handler changes the color, size and font of an  
    ' item based on its position in the array. 
    Private Sub ComboBox1_DrawItem(ByVal sender As Object,  _ 
        ByVal e As System.Windows.Forms.DrawItemEventArgs) _
        Handles ComboBox1.DrawItem

        Dim size As Single 
        Dim myFont As System.Drawing.Font
        Dim family As FontFamily

        Dim animalColor As New System.Drawing.Color
        Select Case e.Index
            Case 0
                size = 30
                animalColor = System.Drawing.Color.Gray
                family = FontFamily.GenericSansSerif
            Case 1
                size = 10
                animalColor = System.Drawing.Color.LawnGreen
                family = FontFamily.GenericMonospace
            Case 2
                size = 15
                animalColor = System.Drawing.Color.Tan
                family = FontFamily.GenericSansSerif
        End Select 

        ' Draw the background of the item.
        e.DrawBackground()

        ' Create a square filled with the animals color. Vary the size 
        ' of the rectangle based on the length of the animals name. 
        Dim rectangle As Rectangle = New Rectangle(2, e.Bounds.Top + 2, _
            e.Bounds.Height, e.Bounds.Height - 4)
        e.Graphics.FillRectangle(New SolidBrush(animalColor), rectangle)

        ' Draw each string in the array, using a different size, color, 
        ' and font for each item.
        myFont = New Font(family, size, FontStyle.Bold)
        e.Graphics.DrawString(animals(e.Index), myFont, System.Drawing.Brushes.Black, _
            New RectangleF(e.Bounds.X + rectangle.Width, e.Bounds.Y, _ 
            e.Bounds.Width, e.Bounds.Height))

        ' Draw the focus rectangle if the mouse hovers over an item.
        e.DrawFocusRectangle()
    End Sub

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft