Linear gradient background does not repaint correctly.

B

Bryon

My current code paints a rectangular area at the top (under the menu and
tool strip) and bottom (top of the status strip) of the main form with a
linear gradient. The problem is that when the form is resized or maximized
for that matter, the form still displays the last areas that were painted
with gradient. Basically the form does not erase the last graphics that
were painted to the form and just paints over it with the new. My code is
as follows in the form paint event.

----------------------

Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint

'declare rectangle dimensions for areas to be painted. Areas will be 64
pixels high.
Dim rectTop As New Rectangle(0, (mainMenu.Height + tsSearchBar.Height),
Me.Width, 64)
Dim rectBottom As New Rectangle(0, (Me.Height - (64 +
ssMessages.Height)), Me.Width, 64)

'declare brushes for linear gradient.
Dim brushTop As New LinearGradientBrush(rectTop, Color.SlateGray,
Color.Transparent, LinearGradientMode.Vertical)
Dim brushBottom As New LinearGradientBrush(rectBottom,
Color.Transparent, Color.DarkGray, LinearGradientMode.Vertical)

'paint areas with gradients upon form refreshes and repaints
e.Graphics.FillRectangle(brushTop, rectTop)
e.Graphics.FillRectangle(brushBottom, rectBottom)

brushTop.Dispose()
brushBottom.Dispose()

End Sub
 
A

Armin Zingler

Bryon said:
My current code paints a rectangular area at the top (under the menu
and tool strip) and bottom (top of the status strip) of the main
form with a linear gradient. The problem is that when the form is
resized or maximized for that matter, the form still displays the
last areas that were painted with gradient. Basically the form does
not erase the last graphics that were painted to the form and just
paints over it with the new. My code is as follows in the form
paint event.
[...]

Only revealed areas are repainted. Try

Me.SetStyle(ControlStyles.ResizeRedraw, True)

in the Form's constructor. Anywhere else, additionally call
Me.Updatestyles.


Armin
 

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