Export (0) Print
Expand All

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 : EventArgs
public class MeasureItemEventArgs extends EventArgs
public class MeasureItemEventArgs extends EventArgs
Not applicable.

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 : System.Windows.Forms.Form
{
   private System.Windows.Forms.ListBox listBox1;
   private System.ComponentModel.Container components = null;


   protected override void Dispose(bool disposing)
   {
      if( disposing )
      {
         if ( components != null ) 
            components.Dispose();

         if ( foreColorBrush != null )
            foreColorBrush.Dispose();
      }
      base.Dispose(disposing);
   }

     #region Windows Form Designer generated code
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.SuspendLayout();
      // 
      // listBox1
      // 
      this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
      this.listBox1.Location = new System.Drawing.Point(16, 48);
      this.listBox1.Name = "listBox1";
      this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
      this.listBox1.Size = new System.Drawing.Size(256, 134);
      this.listBox1.TabIndex = 0;
      this.listBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.listBox1_MeasureItem);
      this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
      // 
      // Form1
      // 
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                   this.listBox1});
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayout(false);

   }
     #endregion

   [STAThread]
   static void Main() 
   {
      Application.Run(new Form1());
   }

   private void listBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
   {
      Font font = ((ListBoxFontItem)listBox1.Items[e.Index]).Font;
      SizeF stringSize = e.Graphics.MeasureString(font.Name, font);

      // Set the height and width of the item
      e.ItemHeight = (int)stringSize.Height;
      e.ItemWidth = (int)stringSize.Width;
   }

   // For efficiency, cache the brush to use for drawing.
   private SolidBrush foreColorBrush;

   private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
   {
      Brush brush;

      // Create the brush using the ForeColor specified by the DrawItemEventArgs
      if ( foreColorBrush == null )
         foreColorBrush = new SolidBrush(e.ForeColor);
      else if ( foreColorBrush.Color != e.ForeColor )
      {
         // The control's ForeColor has changed, so dispose of the cached brush and
         // create a new one.
         foreColorBrush.Dispose();
         foreColorBrush = new SolidBrush(e.ForeColor);
      }

      // 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 & DrawItemState.Selected) == DrawItemState.Selected )
         brush = SystemBrushes.HighlightText;
      else
         brush = foreColorBrush;

      // Perform the painting.
      Font font = ((ListBoxFontItem)listBox1.Items[e.Index]).Font;
      e.DrawBackground();
      e.Graphics.DrawString(font.Name, font, brush, e.Bounds);
      e.DrawFocusRectangle();
   }

   /// <summary>
   ///  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.
   /// </summary>
   public class ListBoxFontItem 
   {
      public Font Font;

      public ListBoxFontItem(Font f) 
      {
         Font = f;
      }

      public override string ToString() 
      {
         return Font.Name;
      }
   }
}

public class Form1 extends System.Windows.Forms.Form
{
    private System.Windows.Forms.ListBox listBox1;
    private System.ComponentModel.Container components = null;

    protected void Dispose(boolean disposing)
    {
        if (disposing) {
            if (components != null) {
                components.Dispose();
            }

            if (foreColorBrush != null) {
                foreColorBrush.Dispose();
            }
        }
        super.Dispose(disposing);
    } //Dispose
      
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.listBox1 = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        // 
        // listBox1
        // 
        this.listBox1.set_DrawMode(
            System.Windows.Forms.DrawMode.OwnerDrawVariable);
        this.listBox1.set_Location(new System.Drawing.Point(16, 48));
        this.listBox1.set_Name("listBox1");
        this.listBox1.set_SelectionMode(
            System.Windows.Forms.SelectionMode.MultiExtended);
        this.listBox1.set_Size(new System.Drawing.Size(256, 134));
        this.listBox1.set_TabIndex(0);
        this.listBox1.add_MeasureItem(
            new System.Windows.Forms.MeasureItemEventHandler(
            this.listBox1_MeasureItem));
        this.listBox1.add_DrawItem(
            new System.Windows.Forms.DrawItemEventHandler(
            this.listBox1_DrawItem));
        // 
        // Form1
        // 
        this.set_ClientSize(new System.Drawing.Size(292, 273));
        this.get_Controls().AddRange(new System.Windows.Forms.Control[] 
            { this.listBox1 });
        this.set_Name("Form1");
        this.set_Text("Form1");
        this.ResumeLayout(false);
    } //InitializeComponent
    #endregion
      
    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main

    private void listBox1_MeasureItem(Object sender, 
        System.Windows.Forms.MeasureItemEventArgs e)
    {
        Font font = ((ListBoxFontItem)listBox1.get_Items().
             get_Item(e.get_Index())).font;
        SizeF stringSize = 
            e.get_Graphics().MeasureString(font.get_Name(), font);

        // Set the height and width of the item
        e.set_ItemHeight((int)stringSize.get_Height());
        e.set_ItemWidth((int)stringSize.get_Width());
    } //listBox1_MeasureItem

    // For efficiency, cache the brush to use for drawing.
    private SolidBrush foreColorBrush;

    private void listBox1_DrawItem(Object sender, 
        System.Windows.Forms.DrawItemEventArgs e)
    {
        Brush brush;

        // Create the brush using the ForeColor specified by the 
        // DrawItemEventArgs
        if (foreColorBrush == null) {
            foreColorBrush = new SolidBrush(e.get_ForeColor());
        }
        else {
            if (!foreColorBrush.get_Color().Equals(e.get_ForeColor())) {
                // The control's ForeColor has changed, 
                // so dispose of the cached brush and
                // create a new one.
                foreColorBrush.Dispose();
                foreColorBrush = new SolidBrush(e.get_ForeColor());
            }
        }
        // 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.get_State() & DrawItemState.Selected).
            Equals(DrawItemState.Selected)) {
            brush = SystemBrushes.get_HighlightText();
        }
        else {
            brush = foreColorBrush;
        }

        // Perform the painting.
        Font font = ((ListBoxFontItem)listBox1.get_Items().
            get_Item(e.get_Index())).font;
        e.DrawBackground();
        e.get_Graphics().DrawString(font.get_Name(), font, brush, 
            new PointF((float)e.get_Bounds().get_X(), 
            (float)e.get_Bounds().get_Y()));
        e.DrawFocusRectangle();
    } //listBox1_DrawItem

    /// <summary>
    ///  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.
    /// </summary>
    public class ListBoxFontItem
    {
        public Font font;

        public ListBoxFontItem(Font f)
        {
            set_Font(f);
        } //ListBoxFontItem

        public String ToString()
        {
            return get_Font().get_Name();
        } //ToString
    } //ListBoxFontItem
} //Form1

System.Object
   System.EventArgs
    System.Windows.Forms.MeasureItemEventArgs

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft