1

I am trying to add a custom message to the background of a listview when there is no data to display. I have turned on the UserPaint flag via the call below which has given me exactly what I want.

SetStyle(ControlStyles.UserPaint, True)

However, this then disables all of the Item and SubItem drawing for the entire ListView. I am only assuming that with this set to true, I am now responsible for drawing all items myself (I could be wrong).

If I am correct, does anyone have a tutorial on how to custom draw list items manually, or give me some examples as I am very new to working with messages and would love to get my head around it all.

Below is my code for drawing my custom message via the OnPaint method:

        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            Dim MessageFont As Font = _EmptyMessageFont
            Dim MessageColour As Color = _EmptyMessageColour
            Dim MessageText As String = _EmptyMessageString

            If Not Me.Items.Count > 0 Then
                Dim flags As TextFormatFlags
                Dim rec As New Rectangle(Me.Location.X + 2, Me.Location.Y + 2, Me.Width - 4, Me.Height - 4)
                flags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter Or TextFormatFlags.SingleLine
                TextRenderer.DrawText(e.Graphics, MessageText, MessageFont, rec, MessageColour, flags)
            End If

            MyBase.OnPaint(e)
        End Sub

Not sure how to add code in this method to handle the normal drawing of list items.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Riples
  • 1,167
  • 2
  • 21
  • 54
  • Try overriding the `OnPaint` handler and call `Paint` of each item, i'll try to throw in some code in the next few minutes. But first, tell us how you paint your `custom` message. Maybe a more prudent course would be to disable the ListView and Shadow it with a Label of the same size, when there is no Data? – MrPaulch Feb 13 '14 at 13:13
  • the LV will supports a BG image. perhaps you could display a message as an image rather than taking over all the drawing? – Ňɏssa Pøngjǣrdenlarp Feb 13 '14 at 13:24
  • Ok poked around on SO for a while, and found the answer i was preparing to tinker: http://stackoverflow.com/questions/1621044/painting-on-a-listview-disables-the-redraw-on-listview-items – MrPaulch Feb 13 '14 at 13:34
  • Thanks MrPaulch. I have updated my post to include my OnPaint event code. I checked your link that you posted, but it doesn't make much sense to me with the CDDS_POSTPAINT as I am not that familiar with notification intercepting. – Riples Feb 13 '14 at 23:17

0 Answers0