What's the reason for this?
I override OnPaintBackground and draw a string. It doesn't show up until I call this in the constructor:
this.SetStyle ( ControlStyles.UserPaint, true );
But then I don't see the items in the listview.
Why and how to solve this?
EDIT: code
protected override void OnPaintBackground ( PaintEventArgs pevent )
{
base.OnPaintBackground ( pevent );
// Create string to draw.
String drawString = "76";
// Create font and brush.
Font drawFont = new Font ( "Arial", 36 );
SolidBrush drawBrush = new SolidBrush ( Color.Blue );
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF ( 150.0F, 150.0F );
// Draw string to screen.
pevent.Graphics.DrawString ( drawString, drawFont, drawBrush, drawPoint );
//pevent.Graphics.FillRectangle ( drawBrush, this.ClientRectangle );
}`enter code here`
As I said in the last thread about this subject,
OnPaint()
andUserPaint
don't work with ListView. The painting is handled by the underlying control and cannot be intercepted in that fashion. This is different to other ControlsSo, when
ControlStyles.UserPaint
istrue
, the underlying control is not told to redraw itself. Instead, all drawing is routed to theOnPaintBackground()
andOnPaint()
methods, which -- as you have found -- do nothing.There are two ways to do what you asked (the second is better than the first):
First way: Intercept the
WM_PAINT
, do the base processing, and then draw onto the listview. Something like this:But this gives repaint problems when what you draw is outside the area that the listview thinks holds the control's contents -- it doesn't trigger paint events.
Second manner: Intercept the CustomDraw notification (this is not the same as OwnerDraw), and listen for the
CDDS_POSTPAINT
stage. In that stage you can safely draw onto the list view. You can look at the code of ObjectListView to see how it is done.You could also just save yourself a lot of bother and use an ObjectListView directly :)