我试图通过使用CallByName和一个遍历对象的循环来将一些图像的可见性设置为false。
以下是代码
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它在"CallByName **theobj_str**...“上抛出错误”类型不匹配“。
CallByName接受一个对象作为它的第一个参数。我需要以某种方式将字符串"theobj_str“转换为对象。我该怎么做呢?
如果我这样称呼它,那么CallByName运行良好:CallByName Images2, "Visible", vbLet,False
谢谢
发布于 2018-03-13 17:49:38
如果不需要使用CallByName,可以遍历controls集合并检查类型。如果类型与您想要隐藏的控件相匹配,那么您可以这样设置它的visible属性。
代码将如下所示:
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这样做将允许您在窗体中添加更多图像控件,而不必记住在for循环中更改计数。
希望这能有所帮助。
https://stackoverflow.com/questions/49232859
复制相似问题