padjee
padjee

Reputation: 265

callbyname vb6 using string as argument

I am trying to set some images's visibility to false by using CallByName and a loop through the objects.

here is the code

Private Sub command1Click
dim theobj_str as string
dim ctr as integer
for ctr = 1 to 3
   theobj_str = "Images" & ctr
   CallByName theobj_str, "Visible", vbLet,False
end for
END SUB

It throws an error "TYPE MISMATCH" on "CallByName **theobj_str**..."

The CallByName takes an object as its first argument. I need to somehow convert the string "theobj_str" into an object. How can I do this ?

The CallByName works fine if I call it like : CallByName Images2, "Visible", vbLet,False

Thanks

Upvotes: 0

Views: 644

Answers (1)

lardymonkey
lardymonkey

Reputation: 768

If you don't need to use CallByName you could loop through the controls collection and check the type. If the type matches the control you want to hide then you can set it's visible property that way.

The code would look like this:

Private Sub Command_Click()

    SetControlVisibility "Image", False

End Sub

Private Sub SetControlVisibility(ByVal controlType As String, ByVal visibleValue As Boolean)
Dim ctrl As Control

    For Each ctrl In Me.Controls
        If TypeName(ctrl) = controlType Then
            ctrl.Visible = visibleValue
        End If
    Next

End Sub

Doing it this way will allow you to add more image controls to your form without having to remember to change your counts in the for loop.

Hope that helps.

Upvotes: 1

Related Questions