ListView.DrawSubItem 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
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
事件类型
示例
下面的代码示例提供了 事件处理程序的 DrawSubItem 实现。 有关完整示例,请参阅 OwnerDraw 参考主题。
// 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
注解
此事件允许你使用所有者绘图自定义控件的外观 ListView 。 仅当 属性设置为 且 属性设置为 View.Detailstrue
时OwnerDraw,View才会引发该属性。 有关所有者绘图的详细信息,请参阅 OwnerDraw 属性参考主题。
注意
子项信息通常显示在磁贴视图和详细信息视图中,但在磁贴视图中,必须在事件的处理程序 DrawItem 中绘制子项信息。
每个DrawSubItemListView子项都可能发生 该事件。 可以处理 DrawItem 事件以绘制所有子项(如背景)共有的元素,并处理 DrawSubItem 事件以绘制单个子项(如文本值)的元素。 还可以仅使用两个事件中的一个来绘制控件中的所有 ListView 项,尽管这可能不太方便。 若要在详细信息视图中绘制列标题,必须处理 事件 DrawColumnHeader 。
注意
对于DrawSubItem未向集合中添加Columns任何ColumnHeader对象的子项,不会发生 该事件。 另请注意,每个 ListViewItem 对象的第一个子项表示父项本身,并显示在第一列中。
有关处理事件的详细信息,请参阅 处理和引发事件。