ListView.DrawSubItem Evento
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
public:
event System::Windows::Forms::DrawListViewSubItemEventHandler ^ DrawSubItem;
public event System.Windows.Forms.DrawListViewSubItemEventHandler DrawSubItem;
public event System.Windows.Forms.DrawListViewSubItemEventHandler? DrawSubItem;
member this.DrawSubItem : System.Windows.Forms.DrawListViewSubItemEventHandler
Public Custom Event DrawSubItem As DrawListViewSubItemEventHandler
Tipo de evento
Exemplos
O exemplo de código a seguir fornece uma implementação de um DrawSubItem manipulador de eventos. Para obter o exemplo completo, consulte o tópico de OwnerDraw referência.
// 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);
}
}
' 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
Comentários
Esse evento permite personalizar a aparência de um ListView controle usando o desenho do proprietário. Ele é gerado somente quando a OwnerDraw propriedade é definida true e a View propriedade é definida como View.Details. Para obter mais informações sobre o desenho do proprietário, consulte o tópico de referência da OwnerDraw propriedade.
Observação
Normalmente, as informações de subitem aparecem no modo de exibição do bloco, bem como na exibição de detalhes, mas no modo de exibição do bloco, elas devem ser desenhadas em um manipulador para o DrawItem evento.
O DrawSubItem evento pode ocorrer para cada ListView subitem. Você pode manipular o DrawItem evento para desenhar elementos comuns a todos os subitem, como a tela de fundo, e manipular o DrawSubItem evento para desenhar elementos para subitens individuais, como valores de texto. Você também pode desenhar todos os itens no ListView controle usando apenas um dos dois eventos, embora isso possa ser menos conveniente. Para desenhar cabeçalhos de coluna no modo de exibição de detalhes, você deve manipular o DrawColumnHeader evento.
Observação
O DrawSubItem evento não ocorre para nenhum subitem para o qual nenhum ColumnHeader objeto foi adicionado à Columns coleção. Observe também que o primeiro subitem de cada ListViewItem objeto representa o item pai em si e é exibido na primeira coluna.
Para obter mais informações sobre como manipular eventos, consulte Manipulando e gerando eventos.