Classe DrawItemEventArgs
Aggiornamento: novembre 2007
Fornisce i dati per l'evento DrawItem.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
L'evento DrawItem è generato dai controlli creati dal proprietario, come i controlli ListBox e ComboBox. Contiene tutte le informazioni necessarie all'utente per disegnare l'elemento specificato, compreso l'indice dell'elemento, l'oggetto Rectangle e l'oggetto Graphics sul quale dovrà essere eseguito il disegno.
Nell'esempio riportato di seguito viene illustrato come realizzare elementi ListBox creati dal proprietario. Nel codice viene utilizzata la proprietà DrawMode per specificare che gli elementi disegnati sono di dimensioni fisse e l'evento DrawItem per eseguire il disegno di ogni elemento contenuto nell'oggetto ListBox. Vengono utilizzate le proprietà e i metodi della classe DrawItemEventArgs passati come parametro al gestore eventi per il disegno degli elementi. Nell'esempio si presuppone che un controllo ListBox denominato listBox1 sia stato aggiunto a un form e che l'evento DrawItem sia gestito dal gestore eventi definito nel codice stesso. Inoltre, si presuppone che siano stati aggiunti all'oggetto ListBox elementi contenenti, nell'ordine, il testo "Apple", "Orange" e "Plum".
private ListBox ListBox1 = new ListBox(); private void InitializeListBox() { ListBox1.Items.AddRange(new Object[] { "Red Item", "Orange Item", "Purple Item" }); ListBox1.Location = new System.Drawing.Point(81, 69); ListBox1.Size = new System.Drawing.Size(120, 95); ListBox1.DrawMode = DrawMode.OwnerDrawFixed; ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem); Controls.Add(ListBox1); } private void ListBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { // Draw the background of the ListBox control for each item. e.DrawBackground(); // Define the default color of the brush as black. Brush myBrush = Brushes.Black; // Determine the color of the brush to draw each item based // on the index of the item to draw. switch (e.Index) { case 0: myBrush = Brushes.Red; break; case 1: myBrush = Brushes.Orange; break; case 2: myBrush = Brushes.Purple; break; } // Draw the current item text based on the current Font // and the custom brush settings. e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); // If the ListBox has focus, draw a focus rectangle around the selected item. e.DrawFocusRectangle(); }
System.EventArgs
System.Windows.Forms.DrawItemEventArgs
System.Windows.Forms.StatusBarDrawItemEventArgs
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition , Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework e .NET Compact Framework non supportano tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.