Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing forms and controls

Status
Not open for further replies.

radarm

Programmer
Aug 14, 2003
5
US
I have a very long sub that pass forms "frm as form" to draw and move pictures to and from the form.
I would like to use the same code in the sub to draw to a control or picture box.
Any ideas how to do this. I get a type mismatch if I just try to pas the control.
 
Show your code where you "try to pas the control", ie your Sub and how you call it.

Have fun.

---- Andy
 
Hi Andy, thanks for the reply.
This is how I use to call it:

Sub DrawTrack(ByVal TrkJ&, ByVal C&, frm As Form)
frm.PSet (Lgn&, Ltn&), Tclrs&
end sub

This is what I have tried:

Sub DrawTrack(ByVal TrkJ&, ByVal C&, frmm As Form, Optional Ctrl As Control, Optional IsControl%)
If IsControl% = False Then
Dim frm As Form
Else
Dim frm As Control
End If
Set frm = frmm
frm.PSet (Lgn&, Ltn&), Tclrs&
end sub
 
>Sub DrawTrack(ByVal TrkJ&, ByVal C&, frm As Form)
>frm.PSet (Lgn&, Ltn&), Tclrs&
>end sub

Try;
Sub DrawTrack(ByVal TrkJ&, ByVal C&, Targ As Object)
Targ.PSet (Lgn&, Ltn&), Tclrs&
end sub

PictureBoxes, Forms and Printers have a subset of common Properties so passing any of them into Targ will generally be successful. However you have to be careful inside the sub to make sure all the code will work with all the Objects you intend to pass to it, eg. running Targ.Caption = "blah" inside the sub would cause an error unless a Form had been passed. You have to be more careful than usual because intellisense in the IDE is less useful on variables Dimmed as Object.



 
First I would decide on what standard I would use: [tt]
ByVal TrkJ&, ByVal C&, Optional IsControl%[/tt]
Or[tt]
frmm As Form, Optional Ctrl As Control[/tt]
and stick with it.
Personally I would avoid outdated (in my opinion) notification of Something& or ABC% and stick with strXYZ As String, lngKLM As Long (unless I miss-interpret your &’s and %’s)

I see you are trying to pass a couple of variables, a Form, and a Control of some sort. Is that any particular Control like a PictureBox, or any Control?

It would be helpful if you would state: I want to pass 2 variables as Long, one Form, and a PictureBox into my Sub.

This is what I have:[tt]
Dim ABC As Long
Dim XYZ As Long
frmMain is my main form with a PictureBox on it
Call DrawTrack(ABC, XYZ, frmMain)
[/tt]


Have fun.

---- Andy
 
Hi Andy,
The suggestion to use OBJECT instead of FORM or CONTROL worked fine in my app., I have it up and running. The only reason I was passing a variable is becaused I thought I needed it to properly DIM the control or form depending on what I was passing. With the OBJECT it doesn't matter although it does seem to take longer to load the subroutine using the AS OBJECT declaraton.
Thank you for your help
 
If it works for you and that's what you want, then you are done.

But in my opinion:
If you accept any Object passed to your Sub, then you need to detect what it is in the Sub. Is it a Form or a Control? And if control - what control is it?

If you narrow it to just a Control, you still need to detect what it is.

I try to narrow it down as much as I can, so I would declare it as PictureBox (or any other control) if I would know what would be passed into the Sub.

But like I said - if it works the way you want it... :)

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top