|
-
May 5th, 2013, 01:33 AM
#1
Thread Starter
Fanatic Member
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?
-
May 5th, 2013, 02:15 AM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 5th, 2013, 02:26 AM
#3
Thread Starter
Fanatic Member
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?
-
May 5th, 2013, 02:30 AM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 5th, 2013, 07:35 AM
#5
Thread Starter
Fanatic Member
Re: How do i paint a rectangle onto a panel when a button is clicked?
-
May 5th, 2013, 10:40 AM
#6
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
 
-
May 5th, 2013, 05:42 PM
#7
Re: How do i paint a rectangle onto a panel when a button is clicked?
Shaggy is correct. I stand corrected
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|