Evento ListView.DrawSubItem (System.Windows.Forms)

Cambia visualizzazione:
ScriptFree
Riferimento a .NET Framework
Evento ListView.DrawSubItem

Nota: questo evento è stato introdotto con .NET Framework versione 2.0.

Si verifica quando viene creata una visualizzazione di dettaglio di una classe ListView e la proprietà OwnerDraw è impostata su true.

Spazio dei nomi: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Sintassi

Visual Basic - (Dichiarazione)
Public Event DrawSubItem As DrawListViewSubItemEventHandler
Visual Basic (Utilizzo)
Dim instance As ListView
Dim handler As DrawListViewSubItemEventHandler

AddHandler instance.DrawSubItem, handler

C#
public event DrawListViewSubItemEventHandler DrawSubItem
C++
public:
event DrawListViewSubItemEventHandler^ DrawSubItem {
	void add (DrawListViewSubItemEventHandler^ value);
	void remove (DrawListViewSubItemEventHandler^ value);
}
J#
/** @event */
public void add_DrawSubItem (DrawListViewSubItemEventHandler value)

/** @event */
public void remove_DrawSubItem (DrawListViewSubItemEventHandler value)

JScript
JScript supporta l'utilizzo di eventi ma non la dichiarazione di nuovi.
Note

Questo evento consente di personalizzare l'aspetto di un controllo ListView mediante il disegno personalizzato. Viene generato solo se la proprietà OwnerDraw è impostata su true e la proprietà View è impostata su View.Details. Per ulteriori informazioni sul disegno personalizzato, vedere l'argomento relativo alla proprietà OwnerDraw.

NotaNota

Le informazioni relative agli elementi secondari solitamente vengono visualizzate sia nella visualizzazione affiancata che in quella in dettaglio ma nel primo caso è necessario crearle in un gestore per l'evento DrawItem.

L'evento DrawSubItem si può verificare per ciascun elemento secondario ListView. È possibile gestire l'evento DrawItem per creare parti comuni a tutti gli elementi secondari, ad esempio lo sfondo, nonché gestire l'evento DrawSubItem per creare parti per i singoli elementi secondari, ad esempio valori di testo. È inoltre possibile creare tutti gli elementi nel controllo ListView utilizzando solo uno dei due eventi, sebbene tale procedura possa risultare più complessa. Per creare le intestazioni di colonna nella visualizzazione di dettaglio, è necessario gestire l'evento DrawColumnHeader.

NotaNota

L'evento DrawSubItem non si verifica per gli elementi secondari per cui non è stato aggiunto alcun oggetto ColumnHeader all'insieme Columns. Tenere inoltre presente che il primo elemento secondario di ciascun oggetto ListViewItem rappresenta l'elemento padre e viene visualizzato nella prima colonna.

Per ulteriori informazioni sulla gestione di eventi, vedere Utilizzo degli eventi.

Esempio

Nell'esempio di codice riportato di seguito viene illustrata un'implementazione di un gestore eventi DrawSubItem. Per un esempio completo, vedere l'argomento relativo a OwnerDraw.

Visual Basic
' Draws subitem text and applies content-based formatting.
Private Sub listView1_DrawSubItem(ByVal sender As Object, _
    ByVal e As DrawListViewSubItemEventArgs) _
    Handles listView1.DrawSubItem

    Dim flags As TextFormatFlags = TextFormatFlags.Left

    Dim sf As New StringFormat()
    Try

        ' Store the column text alignment, letting it default
        ' to Left if it has not been set to Center or Right.
        Select Case e.Header.TextAlign
            Case HorizontalAlignment.Center
                sf.Alignment = StringAlignment.Center
                flags = TextFormatFlags.HorizontalCenter
            Case HorizontalAlignment.Right
                sf.Alignment = StringAlignment.Far
                flags = TextFormatFlags.Right
        End Select

        ' Draw the text and background for a subitem with a 
        ' negative value. 
        Dim subItemValue As Double
        If e.ColumnIndex > 0 AndAlso _
            Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
            NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
            subItemValue < 0 Then

            ' Unless the item is selected, draw the standard 
            ' background to make it stand out from the gradient.
            If (e.ItemState And ListViewItemStates.Selected) = 0 Then
                e.DrawBackground()
            End If

            ' Draw the subitem text in red to highlight it. 
            e.Graphics.DrawString(e.SubItem.Text, _
                Me.listView1.Font, Brushes.Red, e.Bounds, sf)

            Return

        End If

        ' Draw normal text for a subitem with a nonnegative 
        ' or nonnumerical value.
        e.DrawText(flags)

    Finally
        sf.Dispose()
    End Try

End Sub

C#
// Draws subitem text and applies content-based formatting.
private void listView1_DrawSubItem(object sender,
    DrawListViewSubItemEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Left;

    using (StringFormat sf = new StringFormat())
    {
        // Store the column text alignment, letting it default
        // to Left if it has not been set to Center or Right.
        switch (e.Header.TextAlign)
        {
            case HorizontalAlignment.Center:
                sf.Alignment = StringAlignment.Center;
                flags = TextFormatFlags.HorizontalCenter;
                break;
            case HorizontalAlignment.Right:
                sf.Alignment = StringAlignment.Far;
                flags = TextFormatFlags.Right;
                break;
        }

        // Draw the text and background for a subitem with a 
        // negative value. 
        double subItemValue;
        if (e.ColumnIndex > 0 && Double.TryParse(
            e.SubItem.Text, NumberStyles.Currency,
            NumberFormatInfo.CurrentInfo, out subItemValue) &&
            subItemValue < 0)
        {
            // Unless the item is selected, draw the standard 
            // background to make it stand out from the gradient.
            if ((e.ItemState & ListViewItemStates.Selected) == 0)
            {
                e.DrawBackground();
            }

            // Draw the subitem text in red to highlight it. 
            e.Graphics.DrawString(e.SubItem.Text,
                listView1.Font, Brushes.Red, e.Bounds, sf);

            return;
        }

        // Draw normal text for a subitem with a nonnegative 
        // or nonnumerical value.
        e.DrawText(flags);
    }
}

C++
   // Draws subitem text and applies content-based formatting.
private:
   void listView1_DrawSubItem( Object^ /*sender*/, DrawListViewSubItemEventArgs^ e )
   {
      TextFormatFlags flags = TextFormatFlags::Left;
      StringFormat^ sf = gcnew StringFormat;
      try
      {
         // Store the column text alignment, letting it default
         // to Left if it has not been set to Center or Right.
         switch ( e->Header->TextAlign )
         {
            case HorizontalAlignment::Center:
               sf->Alignment = StringAlignment::Center;
               flags = TextFormatFlags::HorizontalCenter;
               break;

            case HorizontalAlignment::Right:
               sf->Alignment = StringAlignment::Far;
               flags = TextFormatFlags::Right;
               break;
         }

         // Draw the text and background for a subitem with a 
         // negative value. 
         double subItemValue;
         if ( e->ColumnIndex > 0 && Double::TryParse( e->SubItem->Text, NumberStyles::Currency, NumberFormatInfo::CurrentInfo, subItemValue ) && subItemValue < 0 )
         {
            // Unless the item is selected, draw the standard 
            // background to make it stand out from the gradient.
            if ( (e->ItemState & ListViewItemStates::Selected) == (ListViewItemStates)0 )
            {
               e->DrawBackground();
            }

            // Draw the subitem text in red to highlight it. 
            e->Graphics->DrawString( e->SubItem->Text, listView1->Font, Brushes::Red, e->Bounds, sf );
            return;
         }

         // Draw normal text for a subitem with a nonnegative 
         // or nonnumerical value.
         e->DrawText( flags );
      }
      finally
      {
         if ( sf )
            delete (IDisposable^)sf;
      }
   }

J#
// Draws subitem text and applies content-based formatting.
private void myListView_DrawSubItem(Object sender,
    DrawListViewSubItemEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Left;
    StringFormat sf = new StringFormat();
    try {
        // Store the column text alignment, letting it default
        // to Left if it has not been set to Center or Right.
        if (e.get_Header().get_TextAlign().
            Equals(HorizontalAlignment.Center)) {

            sf.set_Alignment(StringAlignment.Center);
            flags = TextFormatFlags.HorizontalCenter;
        }
        else {
            if (e.get_Header().get_TextAlign().
                Equals(HorizontalAlignment.Right)) {

                sf.set_Alignment(StringAlignment.Far);
                flags = TextFormatFlags.Right;
            }
        }
        // Draw the text for a column header.
        if (e.get_ItemIndex() == -1) {
            Font myFont = new Font("Helvetica", 12, FontStyle.Bold);
            try {
                e.get_Graphics().DrawString(e.get_Item().get_Text(), 
                    myFont, Brushes.get_White(), 
                    new PointF((float)e.get_Bounds().get_X(), 
                    (float)e.get_Bounds().get_Y()), sf);
            }
            finally {
                myFont.Dispose();
            }
            return;
        }

        // Draw the text and background for a subitem with a 
        // negative value. 
        double subItemValue = 0;
        if (e.get_ColumnIndex() > 0 && System.Double.TryParse(
            e.get_Item().get_SubItems().get_Item(e.get_ColumnIndex()).
            get_Text(), NumberStyles.Currency, 
            NumberFormatInfo.get_CurrentInfo(), subItemValue) 
            && subItemValue < 0) {

            // Unless the item is selected, draw the standard 
            // background to make it stand out from the gradient.
            if (Convert.ToInt32(e.get_ItemState() 
                & ListViewItemStates.Selected) == 0) {
                e.DrawBackground();
            }
            // Draw the subitem text in red to highlight it. 
            e.get_Graphics().DrawString(e.get_Item().get_SubItems().
                get_Item(e.get_ColumnIndex()).get_Text(), 
                ((ListView)sender).get_Font(), Brushes.get_Red(), 
                RectangleF.op_Implicit(e.get_Bounds()), sf);

            return;
        }
        // Draw normal text for a subitem with a nonnegative 
        // or nonnumerical value.
        e.DrawText(flags);
    }
    finally {
        sf.Dispose();
    }
} //myListView_DrawSubItem

Piattaforme

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

.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.

Informazioni sulla versione

.NET Framework

Supportato in: 2.0
Vedere anche