Creating a Linear Gradient Panel


G

Guest

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
LinearGradientBrush(this.ClientRectangle,
gradientColorOne, gradientColorTwo, lgm);
e.Graphics.FillRectangle(lgb, this.ClientRectangle);

lgb.Dispose();
e.Dispose();
gfx.Dispose();
}


protected override void OnPaintBackground(PaintEventArgs e)
{
using(LinearGradientBrush lgb = new
LinearGradientBrush(this.ClientRectangle,
this.GradientColorOne,
this.GradientColorTwo,
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.
 
Ad

Advertisements

G

Guest

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:
http://pocketpollster.com/downloads/PanelGradient.zip

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

Top