Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

You're not initializing G or P.

 

Try:

 

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 won't draw the rectangle around the mouse.  to make the rectangle around the mouse, you'd want
'g.drawrectangle(p,e.x - 20,e.y - 20,40,40)
End Sub

Posted

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)

  • Leaders
Posted

The CreateGraphics method, um, creates a graphics object for you. You do not have to use the New keyword.

'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()

[sIGPIC]e[/sIGPIC]
Posted (edited)

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.

Edited by dcahrakos

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...