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?
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
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?
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
Re: How do i paint a rectangle onto a panel when a button is clicked?
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.
Re: How do i paint a rectangle onto a panel when a button is clicked?
Shaggy is correct. I stand corrected:D