Creating a Linear Gradient Panel



In my WinForms app I've long been able to create a linear gradient effect by
just intercepting the PaintEventHandler event. But I'd now like to create an
inherited user control, specifically of a panel.

I thought I was on the right track and did achieve the effect ... until I
started putting some controls on the panel. Then I got an "Invalid
parameter" error but don't understand what it's referring to.

Based on examples I've found, I've tried two approaches but neither work.
Hopefully someone will know why.

Here are the two approaches:

protected override void OnPaintBackground(PaintEventArgs e)
Graphics gfx = e.Graphics;
Rectangle rect = new Rectangle (0, 0, this.Width, this.Height);

LinearGradientBrush lgb = new
gradientColorOne, gradientColorTwo, lgm);
e.Graphics.FillRectangle(lgb, this.ClientRectangle);


protected override void OnPaintBackground(PaintEventArgs e)
using(LinearGradientBrush lgb = new
this.GradientMode)) //.Rotation))
{e.Graphics.FillRectangle(lgb, this.ClientRectangle);

base.OnPaint(e); //right, want anything handled to be drawn too.

Incidentally, dragging a TextBox onto the panel DOES work. But none of the
other basic controls seem to. I don't know if that gives a clue.




I seem to have solved the problem. I'm not sure how but it works properly
now, at least in run-time mode. If anyone wants a copy of the source code
they can download it here:

Something I don't understand is what makes an inherited user control like
this one display correctly in design-mode. Before it was but now it isn't.
Maybe someone could explain this to me.

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