dcahrakos Posted September 18, 2005 Posted September 18, 2005 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. Quote
thenerd Posted September 18, 2005 Posted September 18, 2005 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 Quote
dcahrakos Posted September 18, 2005 Author Posted September 18, 2005 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) Quote
Leaders snarfblam Posted September 18, 2005 Leaders Posted September 18, 2005 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() Quote [sIGPIC]e[/sIGPIC]
dcahrakos Posted September 18, 2005 Author Posted September 18, 2005 (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 September 18, 2005 by dcahrakos Quote
Machaira Posted September 19, 2005 Posted September 19, 2005 The rectangle should stay there until you move the mouse. Quote Here's what I'm up to.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.