Instead of a Paint event handler, I'd do:
Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
MyBase.OnPaintBackground(e)
' Insert your code here
End Sub
It doesn't seem to get called as often as OnPaint does. However there are a
few problems you'll run into with this method and your code below: first and
foremost, you'll get an exception if you minimize then restore your form.
You'll need additional code to protect against that. Also, if the form gets
resized, the gradient doesn't get re-drawn unless you also override OnResize
(or OnResizeEnd if you want fewer screen re-draws) with a call to Me.Refresh
or something similar.
[quoted text, click to view] "Jay Pondy" wrote:
> Is the code below the proper way to achieve a LinearGradient
> Background on a form?
>
>
> Private Sub MyForm_Paint(ByVal sender As Object, ByVal e As
> PaintEventArgs) Handles Me.Paint
>
> Dim oBrush As New LinearGradientBrush(Me.ClientRectangle,
> Color.AliceBlue, Color.CornflowerBlue, LinearGradientMode.Vertical)
>
> e.Graphics.FillRectangle(oBrush, Me.ClientRectangle)
> oBrush.Dispose()
>
> end sub
>
>
> The reason I ask is because it looks like the Paint event is fired one
> time for each control on the form.
>
It's possible the parent PaintBackground will be invoked for each control if
you've made the controls transparent. Transparent controls actually use a
transform to offset the graphics to correspond with the window rectangle of
the parent form and then call the parents draw routines directly...
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
[quoted text, click to view] "Jay Pondy" <jpondy@AugustaNewsprint.com> wrote in message
news:iocvt1l1o4hv5s4gq45b11ek2mucguqi21@4ax.com...
> Is the code below the proper way to achieve a LinearGradient
> Background on a form?
>
>
> Private Sub MyForm_Paint(ByVal sender As Object, ByVal e As
> PaintEventArgs) Handles Me.Paint
>
> Dim oBrush As New LinearGradientBrush(Me.ClientRectangle,
> Color.AliceBlue, Color.CornflowerBlue, LinearGradientMode.Vertical)
>
> e.Graphics.FillRectangle(oBrush, Me.ClientRectangle)
> oBrush.Dispose()
>
> end sub
>
>
> The reason I ask is because it looks like the Paint event is fired one
> time for each control on the form.
>
Thanks - that explains why the event is occurring so often - my
controls DO have a transparent background.
How can I tell the difference between when the Forms background paint
is called and the controls? I've tried using "if type of sender is
form" but it is always the Form.
What is happening is that the transparent controls backgrounds are
also being drawn with their own gradient background so I'm curious as
to how to tell them apart.
On Wed, 1 Feb 2006 19:12:28 +0100, "Bob Powell [MVP]"
[quoted text, click to view] <bob@_spamkiller_.bobpowell.net> wrote:
>It's possible the parent PaintBackground will be invoked for each control if
>you've made the controls transparent. Transparent controls actually use a
>transform to offset the graphics to correspond with the window rectangle of
>the parent form and then call the parents draw routines directly...
There is no effective way to tell them apart. The transparency mechanism is
a bit of a pig in that respect.
The sender will be the form because the control just manipulates the
graphics object and tells the form to paint it's background...ergo it's
always the form that calls the method.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
[quoted text, click to view] "Jay Pondy" <jpondy@AugustaNewsprint.com> wrote in message
news:q972u1hvt3o6noo5a52paf3nb7oa12qheq@4ax.com...
> Thanks - that explains why the event is occurring so often - my
> controls DO have a transparent background.
>
> How can I tell the difference between when the Forms background paint
> is called and the controls? I've tried using "if type of sender is
> form" but it is always the Form.
>
> What is happening is that the transparent controls backgrounds are
> also being drawn with their own gradient background so I'm curious as
> to how to tell them apart.
>
>
>
> On Wed, 1 Feb 2006 19:12:28 +0100, "Bob Powell [MVP]"
> <bob@_spamkiller_.bobpowell.net> wrote:
>
>>It's possible the parent PaintBackground will be invoked for each control
>>if
>>you've made the controls transparent. Transparent controls actually use a
>>transform to offset the graphics to correspond with the window rectangle
>>of
>>the parent form and then call the parents draw routines directly...
>
Thanks for your help Bob.
On Thu, 2 Feb 2006 19:37:45 +0100, "Bob Powell [MVP]"
[quoted text, click to view] <bob@_spamkiller_.bobpowell.net> wrote:
>There is no effective way to tell them apart. The transparency mechanism is
>a bit of a pig in that respect.
>
>The sender will be the form because the control just manipulates the
>graphics object and tells the form to paint it's background...ergo it's
>always the form that calls the method.
Don't see what you're looking for? Try a search.