'在UserControl中添加以下属性 Public Property Get Controls() As Object Set Controls = UserControl.Controls End Property'就可以在窗体上进行遍历 Private Sub Command1_Click() Dim ctl As Control For Each ctl In UserControl11.Controls Debug.Print ctl.Name Next End Sub
多谢楼上的回答,现在我有多个UserControl 我定义了一个公共过程:Private nFormHeight As Integer Private nFormWidth As Integer Private nNumOfControls As Integerpublic Sub InitCtrl(ctl as UserControl) nFormHeight = ctl.height nFormWidth = ctl.Width nNumOfControls = ctl.Controls.count - 1 For Each ctl In ctl.Controls Debug.Print ctl.name Next End Sub然后我在其他地方调用: Call InitCtrl(U0801_01) U080101是其中一个UserControl,这样在处理多个UserControl时,只需要把名字做参数带进过程就行了,不过现在运行提示:U0801_01变量未定义,现在U0801_01并没有被当成UserControl控件,而是一个字符串,该怎么定义这个参数呢
'访问控件集合
Dim ctr As Control
For Each ctr In Controls
Debug.Print ctr.Name
Next
Public Property Get Controls() As Object
Set Controls = UserControl.Controls
End Property'就可以在窗体上进行遍历
Private Sub Command1_Click()
Dim ctl As Control
For Each ctl In UserControl11.Controls
Debug.Print ctl.Name
Next
End Sub
我定义了一个公共过程:Private nFormHeight As Integer
Private nFormWidth As Integer
Private nNumOfControls As Integerpublic Sub InitCtrl(ctl as UserControl)
nFormHeight = ctl.height
nFormWidth = ctl.Width
nNumOfControls = ctl.Controls.count - 1 For Each ctl In ctl.Controls
Debug.Print ctl.name
Next
End Sub然后我在其他地方调用:
Call InitCtrl(U0801_01)
U080101是其中一个UserControl,这样在处理多个UserControl时,只需要把名字做参数带进过程就行了,不过现在运行提示:U0801_01变量未定义,现在U0801_01并没有被当成UserControl控件,而是一个字符串,该怎么定义这个参数呢
外面的叫CtlSpace,这上面包含了多个控件,U0801_01是它当中的一个控件
现在可以在CtlSpace读到U0801_01,把它当成了一个控件,可是我现在想读U0801_01中的控件目前是循环CtlSpace中的控件,当读到U0801_01时,调用 public Sub InitCtrl(ctl as Control) (4楼的问题我把参数改为Control就可以了)4楼的公共过程第三行 nNumOfControls = ctl.Controls.count - 1 出错,说不支持这个属性
没有嵌套时可以正常实现,当嵌套时里层的UserControl被当作一个控件,在它上面的控件就读不出来了
晕啊
在Form1中调用CtlSpace.DoInitCtrl(),其它控件调用InitCtrl(ctl);
而在CtlSpace.DoInitCtrl()中又调用U0801_01.DoInitCtrl(),其它控件调用InitCtrl(ctl);
U0801_01.DoInitCtrl()中对内含控件调用InitCtrl(ctl)。
而我现在的窗体是不能改变大小的,只是需要通过改变机器分辨率,来自动适应分辨率的