Click here to Skip to main content
15,851,711 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating an application in which the color of TabPage is composed of a color gradient, and there will be several buttons that set the color of TabPage. I want that when the button is clicked to change the color of TabPage, this is my code below but when I click the button color is not changed. Anyone could tell what is wrong, or how can I do this?


VB
Dim cor1 As Color = Color.SkyBlue
  Dim cor2 As Color = Color.Lime
  Private Sub TabPageExecutarPrograma_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TabPageExecutarPrograma.Paint
    Dim FormGraphics As Graphics = e.Graphics
    Dim GradientBrush As Brush
    GradientBrush = New LinearGradientBrush(New Point(0, 0), New Point(0, Me.Height), cor1, cor2)
    FormGraphics.FillRectangle(GradientBrush, ClientRectangle)
  End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   cor1 = Color.Blue
   cor2 = Color.Green
End Sub
Posted
Updated 4-Aug-11 15:35pm
v2

1 solution

I assume cor1 and cor2 are members of the Form of Control, so you share these values between button click and Paint handler.

All correct, but… how do you think control knows when the colors are changed? The missing piece is: one button click, after the colors are changed also call TabPageExecutarPrograma.Invalidate.

Invalidation is the mechanism that trigger rendering via sending the WM_PAINT event with the region or rectangle to be invalidated (no parameters means to invalidate full client area). This is the way to implement, animation, interactive drawing and any other dynamic changes in graphics.

—SA
 
Share this answer
 
v2
Comments
Edson Rodrigues da Silva 5-Aug-11 17:24pm    
Thanks worked perfectly, was exactly what I wanted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900