Este artículo se tradujo de forma manual. Mueva el puntero sobre las frases del artículo para ver el texto original. |
Traducción
Original
|
ComboBox.DrawItem (Evento)
Se produce cuando cambia la apariencia de un control ComboBox dibujado por el propietario.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
Este evento lo utiliza un control ComboBox dibujado por el propietario. Se puede utilizar este evento para realizar las tareas necesarias para dibujar elementos en el control ComboBox. Si se trata de un elemento de tamaño variable (cuando la propiedad ComboBox.DrawMode se establece en el valor OwnerDrawVariablede System.Windows.Forms.DrawMode), antes de dibujar el elemento, se provoca el evento MeasureItem. Se puede crear un controlador de eventos para el evento MeasureItem a fin de especificar el tamaño del elemento que se va a dibujar en el controlador de eventos del evento DrawItem.
Para obtener más información sobre cómo controlar eventos, vea Utilizar eventos.
En el ejemplo de código siguiente se muestra cómo se crea un cuadro combinado dibujado por el propietario estableciendo el valor de la propiedad DrawMode en OwnerDrawnVariable y controlando los eventos DrawItem y MeasureItem. También se muestra cómo establecer el valor de las propiedades DropDownWidth y DropDownStyle. Para ejecutar el ejemplo, pegue el siguiente código en un formulario. Llame al método InitializeComboBox en el constructor del formulario o en el evento Load.
internal System.Windows.Forms.ComboBox ComboBox1; private string[] animals; // 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 void InitializeComboBox() { this.ComboBox1 = new ComboBox(); this.ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; this.ComboBox1.Location = new System.Drawing.Point(10, 20); this.ComboBox1.Name = "ComboBox1"; this.ComboBox1.Size = new System.Drawing.Size(100, 120); this.ComboBox1.DropDownWidth = 250; this.ComboBox1.TabIndex = 0; this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown; animals = new string[]{"Elephant", "c r o c o d i l e", "lion"}; ComboBox1.DataSource = animals; this.Controls.Add(this.ComboBox1); // Hook up the MeasureItem and DrawItem events this.ComboBox1.DrawItem += new DrawItemEventHandler(ComboBox1_DrawItem); this.ComboBox1.MeasureItem += new MeasureItemEventHandler(ComboBox1_MeasureItem); } // 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 void ComboBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { switch(e.Index) { case 0: e.ItemHeight = 45; break; case 1: e.ItemHeight = 20; break; case 2: e.ItemHeight = 35; break; } e.ItemWidth = 260; } // 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 void ComboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { float size = 0; System.Drawing.Font myFont; FontFamily family = null; System.Drawing.Color animalColor = new System.Drawing.Color(); switch(e.Index) { case 0: size = 30; animalColor = System.Drawing.Color.Gray; family = FontFamily.GenericSansSerif; break; case 1: size = 10; animalColor = System.Drawing.Color.LawnGreen; family = FontFamily.GenericMonospace; break; case 2: size = 15; animalColor = System.Drawing.Color.Tan; family = FontFamily.GenericSansSerif; break; } // 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. Rectangle 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(); }
Windows 7, Windows Vista SP1 o posterior, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (no se admite Server Core), Windows Server 2008 R2 (se admite Server Core con SP1 o posterior), Windows Server 2003 SP2
.NET Framework no admite todas las versiones de todas las plataformas. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.