子帧中通过引用传递的帧的访问控制 [英] Access controls of a frame passed by reference in a sub
问题描述
我试图将UserForm的许多MSForms.Frames传递给应该用来处理它们包含的文本框的子项:
I am trying to pass many MSForms.Frames of a UserForm to a sub that is supposed to do stuff with the textboxes they contain:
initFrame(frame1)
但是,一旦通过,我将无法再访问该框架的.Controls属性(在调试过程中添加了一块手表,剩下的只是项).
However, once passed, I cannot access the .Controls property of that frame anymore (added a watch during debug, all that's left are Items).
我尝试了许多不同的子声明,但是它们要么不编译,要么丢失属性...
I have tried many different sub declarations, but they either don't compile or lose the properties...
Private Sub initFrame(ByRef currFrame As MSForms.Frame)
Private Sub initFrame(ByRef currFrame As MSForms.Object)
Private Sub initFrame(ByRef currFrame As Frame)
Private Sub initFrame(ByRef currFrame As Object)
在所有情况下,当我到达
In all case, I get a runtime error when it gets to
For Each ctl In currFrame.Controls
我是否需要做一些特殊的操作才能访问控件?喜欢铸造吗?
Do I have to do something special to access the controls ? Like casting ?
这是完整的代码(忽略.Name分配):
Here is the whole code (ignore the .Name assignements):
Private Sub initTabs()
initFrame (frPrDetails)
initFrame (frPcDetails)
initFrame (frScDetails)
End Sub
Private Sub initFrame(ByRef currFrame As Frame)
Dim ctl As Control
With currFrame
For Each ctl In .Controls
If TypeName(ctl) = "TextBox" Then
If ctl.TabIndex <> 57 Then
Select Case (ctl.TabIndex) Mod 7
Case 1
'ctl.Name = "tb" & ctl.tag & "MP"
Case 2
'ctl.Name = "tb" & ctl.tag & "HW"
Case 3
'ctl.Name = "tb" & ctl.tag & "SW"
Case 4
'ctl.Name = "tb" & ctl.tag & "IC"
Case 5
'ctl.Name = "tb" & ctl.tag & "ES"
Case 6
'ctl.Name = "tb" & ctl.tag & "CONT"
Case 0
'ctl.Name = "tb" & ctl.tag & "ST"
End Select
Else
'ctl.Name = "tbPrTotal"
End If
ctl.Text = ctl.Name
End If
Next ctl
End With
End Sub
推荐答案
原因很简单,但很微妙.
The reason is simple but very subtle.
首先,默认情况下,参数是通过引用传递的,因此:
First of all, by default, parameters are passed by reference, so :
Private Sub initFrame(currFrame As MSForms.Frame) ' ByRef is optional
但是,如果参数是表达式,则会创建一个新的对象/变量.
因此,不管您相信与否,(frame1)
和 frame1
都不一样.
But, if the parameter is an expression, then a new object/variable is created.
So believe it or not, (frame1)
and frame1
are not the same thing.
回到问题,调用函数时,请使用:
Back to the issue, when you call the function, either use:
Call initFrame(frPrDetails) ' note the lack of space before the opening bracket
或者简单地
initFrame frPrDetails
但不是 initFrame(frScDetails)
,它等效于调用initFrame((frPrDetails))
这篇关于子帧中通过引用传递的帧的访问控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!