[RESOLVED] calling mousedown event from a subroutine
Hi,
I have a "typical" test program for drawing a "rubberband" or "rubber box" onto a picture box control. I would like to separate out the drawing of the rubberband into a stand-alone subroutine.
My picture box control (pic1) would still remain on the main-form, and then I would like to call the subroutine, e.g. as:
Call DrawRubberBox(pic1).
In the subroutine:
Public sub DrawRubberBox(p1 as PictureBox)
...
end sub
It is frustrating :mad: that p1_MouseDown(...) is NOT recognized, even though I have declared it as:
Set p1= new PictureBox
The MS help indicates that the MouseDown event applies to objects too (e.g. PictureBox)
Please help
Re: calling mousedown event from a subroutine
As I see it, you are sending the reference to your PictureBox (pic1) to DrawRubberBox and then ignoring this and creating a new PictureBox called p1.
You should be able to send the reference in the call and just use it as p1 inside the routine, without using the Set.... line. - HOWEVER - The picturebox events (mousedown etc) will still only take place within the picturebox code panel. It is not clear from your query what you are actually trying to achieve and yet not be able to use the picturebox events in the normal way.
Re: calling mousedown event from a subroutine
The key word in my original posting is: stand-alone subroutine.
When I call DrawRubberBand, with its argument referring to the picture box, I would like to have ALL of the code in the subroutine (and NOT in the main, which would "normally" contain the MouseDown and MouseMove events).
In some of the programming languages I could do this with pointers and/or aliases referring to the PictureBox
Re: calling mousedown event from a subroutine
http://www.vbforums.com/attachment.p...id=47243&stc=1
I'm confused as to what you are trying to do. Are you trying to create a new picturebox on the form or somehow create one in memory?
Re: calling mousedown event from a subroutine
BTW if it's the latter please see this.
Re: calling mousedown event from a subroutine
not sure exactly what you are wanting to do. but if you are trying to get the code out of your form for reasons of having many controls do the same procedures and your trying to save coding, referencing the control in a class module may be a solution for you.
here is a sample..so start a new project to test it.
this would be the forms code.
Code:
Option Explicit
Private MyPicBox As Class1
Private Sub Form_Load()
Set MyPicBox = New Class1
Set MyPicBox.Ctrl = Picture1
End Sub
this would be the code in the the class module "Class1"
Code:
Option Explicit
Public WithEvents Ctrl As PictureBox
Private Sub Ctrl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Ctrl.Parent.Caption = "mouse is down"
End Sub
Private Sub Ctrl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Ctrl.Parent.Caption = "mouse is up"
End Sub
Private Sub Ctrl_Paint()
Ctrl.Parent.Caption = "Painting"
End Sub
this manipulation allows you to assign many objects to use the same code. the way i have it setup is just for pictureboxes, but with some extra code additions you could have textboxes and such using the same class as well. just as long as they share a common event.
Re: calling mousedown event from a subroutine
oops, i forgot to add the cleanup..
in form1
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Set MyPicBox = Nothing
End Sub
in class1
Code:
Private Sub Class_Terminate()
Set Ctrl = Nothing
End Sub
Re: calling mousedown event from a subroutine
Dear Billy Conner,
That's EXACTLY what I was looking for. Thanks.
P.S. [1] I know I'm supposed to change the Thread to [RESOLVED], but unfortunately I don't know how to do it. But consider it resolved.
P.S. [2] Same about "Rating" your reply. But consider it "EXCELLENT".
Re: calling mousedown event from a subroutine
You can show it's resolved by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button, and to rate someone click the "Rate" link at the bottom of the post under his name.