Small Problem...

dcahrakos

Active member
Joined
Jul 18, 2004
Messages
25
well im trying to get it so when a persons mouse goes into a picture box, which I have named as prender, a box is drawn around the mouse, and then it follows the mouse, the code I have is this

Private Sub prender_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles prender.MouseMove
Dim p As Pen
Dim g As Graphics
g.DrawRectangle(p, e.X, e.Y, 40, 40)
End Sub

this all builds, and everything, but when i try it in the app, I get a Object reference not set to an instance of an object error...

can someone help me with this I havent used GDI much, so im not firmiliar that much with it.
 
Youre not initializing G or P.

Try:

Code:
Private Sub prender_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles prender.MouseMove
Dim p As Pen
p = new pen(color.black)
Dim g As Graphics
g = new graphics(prender.creategraphics)
g.DrawRectangle(p, e.X, e.Y, 40, 40)
Also, this wont draw the rectangle around the mouse.  to make the rectangle around the mouse, youd want
g.drawrectangle(p,e.x - 20,e.y - 20,40,40)
End Sub
 
doh....I knew I was forgetting something, im still to use to VB6....oh well, thanks alot :)

edit, it doesnt really work, it gives me an error
System.Drawing.Graphics.Private Sub New(nativeGraphics As System.IntPtr) is not accessible in this context because it is Private.

that happens at g = New Graphics(prender.CreateGraphics)
 
The CreateGraphics method, um, creates a graphics object for you. You do not have to use the New keyword.
[VB]
Wrong: You are passing a graphics object to the graphics constructor
(If you look at intellisense or the object browser, you will see that graphics has no
public constructor).
g = New Graphics(prender.CreateGraphics)
Right
g = prender.CreateGraphics()
[/VB]
 
thanks, that works perfectly, although its not the way I thought it would respond, but I got it to work the way I wanted now. :)

edit: actually, one other quick question while were still on the same subject, say if I wanted to draw the same rectangle when the mouse just stays in one spot, how would I do that, I tried mousehover, but it doesnt have any X,Y coordinates to draw the rectangle at.
 
Last edited by a moderator:
Back
Top