Drawing an image in a subItem in a ListView

My Listview is a detail view setting with the following column headings:

Image Title || Image || Image Size || Image Preview

I would like to know if there is a way to draw an image in the 4th column. The only way I know is to install

this.listview1.OwnerDraw = true
this.listView1.DrawColumnHeader += new System.Windows.Forms.DrawListViewColumnHeaderEventHandler(listView1_DrawColumnHeader);
this.listView1.DrawItem += new System.Windows.Forms.DrawListViewItemEventHandler(listView1_DrawItem);
this.listView1.DrawSubItem += new System.Windows.Forms.DrawListViewSubItemEventHandler(listView1_DrawSubItem);

The problem with this is that I have to handle ALL the listview drawing myself ... I was wondering if there is a better way to draw an image in subItem, or if there is a way to handle the DrawSubItem event?

+5
source share
4 answers

In event handlers listView1_DrawColumnHeaderand listView1_DrawItemyou should put this

e.DrawDefault = true;

, , , .

0

.

@zidane . , , , , .


DrawColumnHeader e.DrawDefault = true; . , e.DrawDefault = true; DrawItem, DrawSubItem , , , .

DrawSubItem, :

if (/* condition to determine if you want to draw this subitem */)
{
    // Draw it
}
else
    e.DrawDefault = true;
+6
+2

, VB.NET:

Public Class MyListView : Inherits System.Windows.Forms.ListView

Public Sub New()
    MyBase.New()
    MyBase.OwnerDraw = True
End Sub

Protected Overrides Sub OnDrawSubItem(ByVal e As DrawListViewSubItemEventArgs)
    If x Then ' condition to determine if you want to draw this subitem
        ' draw code goes here
    Else
        e.DrawDefault = True
    End If
    MyBase.OnDrawSubItem(e)
End Sub

Protected Overrides Sub OnDrawColumnHeader(ByVal e As DrawListViewColumnHeaderEventArgs)
    e.DrawDefault = True
    MyBase.OnDrawColumnHeader(e)
End Sub

Protected Overrides Sub OnDrawItem(e As System.Windows.Forms.DrawListViewItemEventArgs)
    e.DrawDefault = True
    MyBase.OnDrawItem(e)
End Sub

End Class
0

All Articles