ListView with Gradient Background

D

David Gouge

Hi All, appreciate that this is more gdi+ than c# but always seem to get
good answers here, so here goes...

In a nutshell, all i want to do is add a gradient background to a
ListView. I have gotten so far by:

Adding the following to the constructor:

this.SetStyle(ControlStyles.UserPaint, true);

And then overriding OnPaintBackground with the following:

protected override void OnPaintBackground(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
Rectangle rBackground = new Rectangle(0, 0, this.Width, this.Height);

System.Drawing.Drawing2D.LinearGradientBrush bBackground = new
System.Drawing.Drawing2D.LinearGradientBrush(rBackground,
Color.LightGoldenrodYellow, Color.White, (float)90);

g.FillRectangle(bBackground, rBackground);
}

Which works to a point; the listview has a nice gradient background, but
when items are added to the listview they do not show up and
subsequently when the listview is scrolled, the gradient gets 'messed
up'. I do not want to do anything special with the listview items
themselves, and as i scroll i would expect the gradient to stay put.

Complete beginner at gdi+, so any pointers / help would be appreciated.

Cheers,

Dave
 
Joined
Aug 24, 2012
Messages
1
Reaction score
0
Hi All, appreciate that this is more gdi+ than c# but always seem to get
good answers here, so here goes...

In a nutshell, all i want to do is add a gradient background to a
ListView. I have gotten so far by:

Adding the following to the constructor:

this.SetStyle(ControlStyles.UserPaint, true);

And then overriding OnPaintBackground with the following:

protected override void OnPaintBackground(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
Rectangle rBackground = new Rectangle(0, 0, this.Width, this.Height);

System.Drawing.Drawing2D.LinearGradientBrush bBackground = new
System.Drawing.Drawing2D.LinearGradientBrush(rBackground,
Color.LightGoldenrodYellow, Color.White, (float)90);

g.FillRectangle(bBackground, rBackground);
}

Which works to a point; the listview has a nice gradient background, but
when items are added to the listview they do not show up and
subsequently when the listview is scrolled, the gradient gets 'messed
up'. I do not want to do anything special with the listview items
themselves, and as i scroll i would expect the gradient to stay put.

Complete beginner at gdi+, so any pointers / help would be appreciated.

Cheers,

Dave

Bump from the past!!!! :thumb:

Correction: The easiest way to accomplish this is to create a gradient image and set it as the background image of your listview.

xreply said:
This question is flailing around on the internet with no solid answers anywhere... So here's the answer:

In your constructor:
Code:
C#: 
this.SetStyle(ControlStyles.UserPaint, True);

VB.NET:
Me.SetStyle(ControlStyles.UserPaint, True)

And then build the following:
Code:
C#:
protected void nPaintBackground(object sender, PaintEventArgs p)
{
Graphics g = listview_control.CreateGraphics();
Rectangle rect = new Rectangle(0, 0, listview_control.Width, listview_control.Height);

System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.LightGoldenrodYellow, Color.White, 90);

g.FillRectangle(brush, rect);
}

VB.NET:
Protected Sub nPaintBackground(ByVal sender As Object, ByVal p As PaintEventArgs) Handles Me.Paint
Dim g As Graphics = listview_control.CreateGraphics()
Dim rect As Rectangle = New Rectangle(0, 0, listview_control.Width, listview_control.Height)

Dim brush As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.LightGoldenrodYellow, Color.White, 90)

g.FillRectangle(brush, rect)
End Sub

The issue was that you were covering your ListView with the gradient because you were using your main form's graphics instead of the ListView's graphics. The ListView itself does not have a Paint handler, so you must use the Windows Form Paint handler, then create the ListView's graphics. Make sure you replace the "listview_control" with the name of your control and you should be good!

*edit* Also, make sure you use the same method every time your listview's data changes.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top