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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing control to sub/function 1

Status
Not open for further replies.

bateman23

Programmer
Mar 2, 2001
145
0
0
DE
Hi to all,

and merry Christmas to the whole community.
Anyway... back to VB:
I've got a problem passing a control as an argument to a sub.
My code looks like that:
'1) The header of the sub placed in a module
Public Sub readdir(pattern As String, tempvz As String, ByRef cntr As Control)

'2) and the call of the function
readdir pattern, (tempvz), frmeditor.listproject

...,but the variable cntr is always empty. - I think it should contain frmeditor.listproject. Where is my mistake?

Thanks a lot
bateman23 aka Daniel

---------------------------------------
Visit me @:
 
Hi!

First of all, you didn't say what is the type of your frmeditor.listproject. Is that a ListBox or what?
Second, you don't have to specify ByRef explicitely, since omitting this directive tells VB to use ByRef by default. It is not wrong to specify it, it is just unnecessery.
Third, try this:

Public Sub ReadDir(ByVal Pattern As String, ByVal Tempvz As String, Cntr As Object)

The ByVal directive is suggested to be used when you don't need to modify the argument in the procedure. Routines which have strings passed to as ByVal are a bit quicker. So if you don't need to modify the Pattern and Tempvz in the procedure, pass them as ByVal (if directive is omitted, VB passes it as ByRef by default!)

Also use As Object, when you need to pass the control to subroutine.

Aleksander
 
Mhhhh...
1st - Yes it's a ListBox
2nd - Using as Object also doesn't work. The variable is still empty.

Also also tried dim as control and passing frmeditor.listproject.name. But this just passes the name of the selected entry and not the name of the control. - Very confusing...

Any other ideas?
All comments and thoughts are greatly appreciated
Daniel

---------------------------------------
Visit me @:
 
The code below works, and you can also pass 'lst' as Control or as ListBox. When you hold the mouse pointer over lst in the Sub it says 'lst = ""', but it's not empty. I think it's trying to show you whatever Text the object may have, because there's no textual representation of an Object for the debugger to use.

Option Explicit

Private Sub FillList(lst As Object)
lst.AddItem "Hello"
lst.AddItem "World"

End Sub

Private Sub Command1_Click()
FillList List1

End Sub

Private Sub Command2_Click()
FillList List2

End Sub

HTH.

~Mike

Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Yuhuuu... thanks a lot ;-)
The code actually worked from the beginning. It was just the confusing behavior of the debugger, which made me stumble. - as you said. - a big star for you.

Daniel ---------------------------------------
Visit me @:
 
Oh come on!

Paste this in your project and click the button! It works 100 %!

Code:
Option Explicit

Public Sub AddLine(ListBox As Object, ByVal Str As String)
  ListBox.AddItem Str
End Sub

Private Sub Command1_Click()
  AddLine List1, "Test"
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top