Results 1 to 3 of 3

Thread: [RESOLVED] Gradient PictureBox not displaying

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Resolved [RESOLVED] Gradient PictureBox not displaying

    Hello.

    I've got all the code figured out to make a picture box appear with a gradient inside of it. My problem is that the gradient doesn't appear in the picture box when the form loads, paints, or when the picture box is painted. I can only make the gradient appear if i click the picture box after the form appears.

    Code:
        Dim xFormTopColour As Color = Color.Black
        Dim xFormBottomColour As Color = Color.DarkOrchid
    
        Private Sub frmTestGradientBars3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Call GradientPictureBox(xFormTopColour, xFormBottomColour, picSample)
        End Sub
    
        Private Sub frmTestGradientBars3_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Call GradientPictureBox(xFormTopColour, xFormBottomColour, picSample)
        End Sub
    
        Private Sub picSample_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picSample.Paint
            Call GradientPictureBox(xFormTopColour, xFormBottomColour, picSample)
        End Sub
    
        Private Sub picSample_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles picSample.Click
            Call GradientPictureBox(xFormTopColour, xFormBottomColour, picSample)
        End Sub
    
        Public Sub GradientPictureBox(ByVal xTopColour As Color, ByVal xBottomColour As Color, ByRef picBox As PictureBox)
            Dim xDirection As Drawing2D.LinearGradientMode = Drawing2D.LinearGradientMode.Vertical  'One of the four directions of the fade
            Dim xBrush As New Drawing2D.LinearGradientBrush(picBox.DisplayRectangle, xTopColour, xBottomColour, xDirection)
            Dim xGraphics As Graphics = picBox.CreateGraphics()
            xGraphics.FillRectangle(xBrush, picBox.DisplayRectangle)
            xBrush.Dispose()
            xGraphics.Dispose()
        End Sub
    So what am i doing wrong? It seems like there's a secret step that i'm missing.
    .
    ~Peter


  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Gradient PictureBox not displaying

    Transfer lines 1, 2, 4 and 5 from your GradientPictureBox sub to your picSample Paint event sub, and replace xGraphics by e.Graphics. Then you can get rid of GradientPictureBox and all the other subs. BB

  3. #3

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up Re: Gradient PictureBox not displaying

    Interesting. So i was just using the wrong object. That certainly solved it.

    I streamlined it so that now i can re-use the function on other forms.

    Code:
        Dim xFormTopColour As Color = Color.Black
        Dim xFormBottomColour As Color = Color.DarkOrchid
    
        Private Sub picSample_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picSample.Paint
            Call GradientPictureBox(xFormTopColour, xFormBottomColour, picSample, e)
        End Sub
    
        Public Sub GradientPictureBox(ByVal xTopColour As Color, ByVal xBottomColour As Color, ByRef picBox As PictureBox, ByVal e As System.Windows.Forms.PaintEventArgs)
            Dim xDirection As Drawing2D.LinearGradientMode = Drawing2D.LinearGradientMode.Vertical  'One of the four directions of the fade
            Dim xBrush As New Drawing2D.LinearGradientBrush(picBox.DisplayRectangle, xTopColour, xBottomColour, xDirection)
            e.Graphics.FillRectangle(xBrush, picBox.DisplayRectangle)
            xBrush.Dispose()
        End Sub
    Thanks for the tip!
    ~Peter


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width