I'm trying to create a WinForm with a Linear Gradient background.
That's easy enough, but any Labels I have on the form do not have their
background painted correctly. Even though I have Label.BackColor set to
Color.Transparent, it is using the original BackColor of the Form. Here
is my code, which is called in the Paint event for the Form ...

private void LoginForm_Paint(object sender,
System.Windows.Forms.PaintEventArgs e) {

LinearGradientBrush brush = new LinearGradientBrush(
this.ClientRectangle, Color.Black, Color.Blue,
LinearGradientMode.Vertical);
e.Graphics.FillRectangle(brush, this.ClientRectangle);
}

Am I going to have to override the Paint event for the Label components
as well and manually output the text?

--
Cheers,
David Clegg
dclegg_at_ebetonline_dot_com

"Where's the 'Any' Key?" - Homer Simpson

Re: Problem painting Linear Gradient onto WinForm by Stoitcho

Stoitcho
Thu Feb 26 08:59:10 CST 2004

Hi David,
I tried your code and it works. The problem might be in some other part of
your code. Could you post working example demonstrating your problem?

--
B\rgds
100

"David Clegg" <dclegg@nospam.ebetonline.com> wrote in message
news:xn0dezkbqcoe9400j@msnews.microsoft.com...
> I'm trying to create a WinForm with a Linear Gradient background.
> That's easy enough, but any Labels I have on the form do not have their
> background painted correctly. Even though I have Label.BackColor set to
> Color.Transparent, it is using the original BackColor of the Form. Here
> is my code, which is called in the Paint event for the Form ...
>
> private void LoginForm_Paint(object sender,
> System.Windows.Forms.PaintEventArgs e) {
>
> LinearGradientBrush brush = new LinearGradientBrush(
> this.ClientRectangle, Color.Black, Color.Blue,
> LinearGradientMode.Vertical);
> e.Graphics.FillRectangle(brush, this.ClientRectangle);
> }
>
> Am I going to have to override the Paint event for the Label components
> as well and manually output the text?
>
> --
> Cheers,
> David Clegg
> dclegg_at_ebetonline_dot_com
>
> "Where's the 'Any' Key?" - Homer Simpson



Re: Problem painting Linear Gradient onto WinForm by David

David
Thu Feb 26 16:57:21 CST 2004

Stoitcho Goutsev (100) [C# MVP] wrote:

> The problem might be in some other part of
> your code.

Interesting. I wrote a bare-bones app (one form, one Label) and it
worked as expected.

The form I'm having trouble with has a few third party controls on it
(UltraButton and UltraTextEdit from Infragistics), however the Label
controls are from the FCL. I'll have to investigate this one further...

--
Cheers,
David Clegg
dclegg_at_ebetonline_dot_com
http://cc.borland.com/codecentral/ccweb.exe/author?authorid=72299

Quality Central. The best way to bug Borland about bugs.
http://qc.borland.com

"I know I'm not usually a praying man, but if you're up there, please
Superman, help me!" - Homer Simpson

Re: Problem painting Linear Gradient onto WinForm by David

David
Thu Feb 26 17:54:04 CST 2004

David Clegg wrote:

> I'll have to investigate this one further...

Sorry, my bad :-(

After investigating further I discovered that my original Form_Paint
was not using the Graphics class that was passed in the PaintEventArgs
object, but was creating it e.g.

Graphics ga = this.CreateGraphics()
try {
LinearGradientBrush brush = new LinearGradientBrush(
this.ClientRectangle, Color.Black, Color.Blue,
LinearGradientMode.Vertical);
ga.FillRectangle(brush, this.ClientRectangle);
}
finally {
ga.Dispose();
}

This is what was causing the problem. Unfortunately, along the way, I'd
tried (unsuccessfully) to paint the labels manually, and this is what
was causing the same symptoms when using the Graphics instance from
PaintEventArgs.

Apologies for wasting your time.

--
Cheers,
David Clegg

"With $10,000, we can be millionaires!" - Homer Simpson

Re: Problem painting Linear Gradient onto WinForm by Stoitcho

Stoitcho
Fri Feb 27 08:25:47 CST 2004

Hi David,

Just a reminder. If you want to paint the background you can override
OnPaintBackground method. It is meant to be used for this. But be careful
calls to this method can be suppressed my setting some of the control
styles.

--
B\rgds
100

"David Clegg" <dclegg@nospam.ebetonline.com> wrote in message
news:xn0df0t3r5rxkj00b@msnews.microsoft.com...
> David Clegg wrote:
>
> > I'll have to investigate this one further...
>
> Sorry, my bad :-(
>
> After investigating further I discovered that my original Form_Paint
> was not using the Graphics class that was passed in the PaintEventArgs
> object, but was creating it e.g.
>
> Graphics ga = this.CreateGraphics()
> try {
> LinearGradientBrush brush = new LinearGradientBrush(
> this.ClientRectangle, Color.Black, Color.Blue,
> LinearGradientMode.Vertical);
> ga.FillRectangle(brush, this.ClientRectangle);
> }
> finally {
> ga.Dispose();
> }
>
> This is what was causing the problem. Unfortunately, along the way, I'd
> tried (unsuccessfully) to paint the labels manually, and this is what
> was causing the same symptoms when using the Graphics instance from
> PaintEventArgs.
>
> Apologies for wasting your time.
>
> --
> Cheers,
> David Clegg
>
> "With $10,000, we can be millionaires!" - Homer Simpson



Re: Problem painting Linear Gradient onto WinForm by David

David
Sun Feb 29 16:04:29 CST 2004

Stoitcho Goutsev (100) [C# MVP] wrote:

> If you want to paint the background you can override
> OnPaintBackground method.

Thanks for the heads up.

--
Cheers,
David Clegg
dclegg_at_ebetonline_dot_com

"You couldn't fool me on the foolingest day of the year with an
electrified
fooling machine." - Homer Simpson