|
-
Aug 14th, 2017, 09:32 AM
#1
Thread Starter
Addicted Member
Graphics.DrawRectangle Method
Have this :
Code:
Public Sub DrawRectangleInt(ByVal e As PaintEventArgs)
' Create pen.
Dim blackPen As New Pen(Color.Black, 3)
' Create location and size of rectangle.
Dim x As Integer = 0
Dim y As Integer = 0
Dim width As Integer = 200
Dim height As Integer = 200
' Draw rectangle to screen.
e.Graphics.DrawRectangle(blackPen, x, y, width, height)
End Sub
Try to call it with:
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
DrawRectangleInt(ByVal e As PaintEventArgs)
End Sub
Mouse over DrawRectangleInt, get error : Expression expected. How can I call it ?.
Thank you
-
Aug 14th, 2017, 09:46 AM
#2
Re: Graphics.DrawRectangle Method
That method call in the second code snippet doesn't make sense. The bit inside the parentheses is a parameter declaration so it doesn't belong there. Maybe what you were trying to do is this:
vb.net Code:
DrawRectangleInt(DirectCast(e, PaintEventArgs))
That is valid syntax at least, so it would compile. It's still not going to work though. If you're going to pass a PaintEventArgs object to that method then you need to actually have a PaintEventArgs object to pass, which you don't. You would need to call that method from the Paint event event handler, where the 'e' parameter actually is type PaintEventArgs. If you want to force a Paint event to be raised when you click Button2 then call the Refresh method of the form in the Click event handler of Button2, then call DrawRectangleInt from the Paint event handler.
-
Aug 14th, 2017, 09:54 AM
#3
Thread Starter
Addicted Member
Re: Graphics.DrawRectangle Method
Get error Unable to cast object of type . In this case what could it be a PaintEventArgs object to pass ?.Thank you.
-
Aug 14th, 2017, 10:29 AM
#4
Re: Graphics.DrawRectangle Method
Pretty much as JMC said. The only place you get a valid PaintEventArgs is in the Paint event handler. You won't have any good means to get that in the button click, so you don't really have any alternative.
You also don't really WANT any alternative, though you may think you do. You may be thinking that drawing as a result of a button click would be a good thing. It would not, at least not the way you're thinking about it. All controls are windows, including the form itself, and all windows are transient. At any time, a different window, or something else, can be dragged over the top of the form. When the form becomes visible again, it has to be able to paint itself on the screen. If you bring up a different window over the current one, you may think that the current one is still there, just in the background, but that's not really the case. The monitor has no depth to it. Every window has some area on the screen that it paints itself into. If another form is dragged over the current one, all that is really happening is that the new form is drawn into the screen area that the other one used to be in. When that form is dragged away, the form in the background becomes visible, which means that it has to be able to draw itself into the screen area that it has. Anything else would be terribly inefficient.
So, every form just draws into an area on the screen, and it does this in response to being invalidated. What you see is a Paint event handler. Any drawing you do on the form has to happen in that handler, or else whatever you draw will simply go away as soon as the form is invalidated, because the form will redraw itself after being invalidated, and it won't know to draw anything extra unless it is in the Paint event handler. Therefore, do all your drawing in the Paint event handler and nowhere else, and you'll have your PaintEventArgs object.
My usual boring signature: Nothing
 
-
Aug 14th, 2017, 11:00 AM
#5
Re: Graphics.DrawRectangle Method
 Originally Posted by ebellounisoft
Get error Unable to cast object of type . In this case what could it be a PaintEventArgs object to pass ?.Thank you.
Did you bother to read my post or did you just copy and paste the code? I specifically said that that code would not work and why. I specifically told you what to do to get a PaintEventArgs object to pass.
-
Aug 14th, 2017, 11:03 AM
#6
Thread Starter
Addicted Member
Re: Graphics.DrawRectangle Method
You're write. My mistake opps !!. Read it all and working according to your recommendation. Thank you
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|