By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

Creating a Linear Gradient Panel

P: n/a
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.

--
Robert W.
Vancouver, BC
www.mwtech.com

Apr 15 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

--
Robert W.
Vancouver, BC
www.mwtech.com

"Robert W." wrote:
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.

--
Robert W.
Vancouver, BC
www.mwtech.com

Apr 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.