Problem painting Linear Gradient onto WinForm

D

David Clegg

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
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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?
 
D

David Clegg

Stoitcho said:
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
 
D

David Clegg

David said:
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.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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.
 
D

David Clegg

Stoitcho said:
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
 

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