Results 1 to 7 of 7

Thread: How do i paint a rectangle onto a panel when a button is clicked?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2005
    Location
    Bulawayo, Zimbabwe
    Posts
    576

    How do i paint a rectangle onto a panel when a button is clicked?

    I know how to paint a rectangle i.e.

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
    Dim R As New Rectangle(120, 142, 440, 56)
    e.Graphics.DrawRectangle(Pens.BlueViolet, R)
    End Sub

    But how do i get this to take place only when the user clicks a button?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: How do i paint a rectangle onto a panel when a button is clicked?

    try this:

    Code:
    Public Class Form1
    
        Dim paint As Boolean = False
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            paint = Not paint
            Panel1.Invalidate()
        End Sub
    
        Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
            If paint Then
                Dim R As New Rectangle(120, 142, 440, 56)
                e.Graphics.DrawRectangle(Pens.BlueViolet, R)
            End If
        End Sub
    
    End Class

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2005
    Location
    Bulawayo, Zimbabwe
    Posts
    576

    Re: How do i paint a rectangle onto a panel when a button is clicked?

    Thanks, I will give it a try, so the only way is to invalidate the panel?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: How do i paint a rectangle onto a panel when a button is clicked?

    invalidating the panel forces a repaint, + runs the code in the Panel1_Paint event

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2005
    Location
    Bulawayo, Zimbabwe
    Posts
    576

    Re: How do i paint a rectangle onto a panel when a button is clicked?

    Thanks

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How do i paint a rectangle onto a panel when a button is clicked?

    Invalidating doesn't quite force a repaint. One could argue the point, as it's largely a matter of the word 'force'. Invalidating the control tells the app that the control is dirty and needs to be painted again, but it allows the app to get around to it when it can. Calling .Refresh will force an immediate painting of the control, which is MUCH more preemptive. There are times when this is essential to do, such as when you need the control to paint IMMEDIATELY. Invalidate will cause the control to paint, but....eventually. If you are updating a label in a loop, Refresh would cause the display to update as soon as you call it, but Invalidate would cause the display to update once the loop is completed. This is a distinction that shows up at times when people complain that their control isn't showing the changes they are making in a loop.
    My usual boring signature: Nothing

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: How do i paint a rectangle onto a panel when a button is clicked?

    Shaggy is correct. I stand corrected

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