对一个Frame内控件的遍历

1.
Dim objControl As Control

For Each objControl In Me.Controls
If objControl.Container.Name = "Frame1" Then
Debug.Print objControl.Name
End If
Next


2.
Public Sub OperateFrame(ByRef objFrame As VB.Frame, ByVal intOperation As Integer)
'\\***********************************************************************
'
\\函 数 名:OperateFrameTextBox
'
\\输 入: ByRef objFrame(VB.Frame) -
'
\\ : ByVal intOperation(Integer)
'
\\ 1: 表示清空TextBox,
'
\\ 2: 表示Enable所有TextBox
'
\\输 出:无
'
\\功能描述:对一个Frame内的TextBox或者ComboBox进行操作,如清空TextBox,Enable或者Disable等
'
\\全局变量:
'
\\使用例子:Call OperateFrame(Me.Frame2, 3)
'
\\日 期:2004-09-22
'
\\修 改 人:
'
\\日 期:
'
\\版 本:V1.0.0
'
*************************************************************************
Dim objControl As Control
Dim TopForm As VB.Form

On Error GoTo ErrHandle

Set TopForm = GetTopContainer(objFrame)

For Each objControl In TopForm.Controls
Select Case intOperation
Case 1
'\\ 1 表示清空frame内所有TextBox
If (objControl.Container.Name = objFrame.Name) And (TypeName(objControl) = "TextBox"Then
objControl.Text 
= ""
End If
Case 2
'\\ 2 表示Enable所有frame内的TextBox
If (objControl.Container.Name = objFrame.Name) And (TypeName(objControl) = "TextBox"Then
objControl.Enabled 
= True
End If
Case 3
'\\ 2 表示Enable所有frame内的TextBox
If (objControl.Container.Name = objFrame.Name) And (TypeName(objControl) = "TextBox"Then
objControl.Enabled 
= False
End If

Case Else

End Select
Next

ErrHandle:
If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical, "提示"
Err.Clear
End If

End Sub

Public Function GetTopContainer(ByRef objCtl As Control) As Object
'\\***********************************************************************
'
\\函 数 名:GetTopContainer
'
\\输 入:ByRef objCtl(Object)
'
\\输 出:(Object)
'
\\功能描述:获得某控件的顶层容器
'
\\使用例子:Set TopForm = GetTopContainer(objFrame)
'
\\全局变量:
'
\\日 期:2004-09-22
'
\\修 改 人:
'
\\日 期:
'
\\版 本:V1.0.0
'
*************************************************************************
Dim objContainer As Object
Dim objMe As Object
Dim i As Integer
= 1

On Error GoTo ErrHandle

'\\一层层向上获得Container,直到最高一层(为Form对象)
'
\\这时候由于不存在Container对象,会诱发错误438(对象不支持该属性或方法)
Set objMe = objCtl
Do Until 1 = 2
If i > 2 Then
Set objMe = objContainer
End If
Set objContainer = objMe.Container
= i + 1
If i > 100 Then
'\\当循环超过100次,应该是存在错误了
MsgBox "异常情况!!", vbCritical, "提示"
Exit Function
End If
DoEvents
Loop

ErrHandle:
If Err.Number <> 0 Then
If Err.Number = 438 Then
Set GetTopContainer = objMe
Else
MsgBox Err.Description, vbCritical, "提示"
End If
End If

End Function


原文地址:https://www.cnblogs.com/sekihin/p/705270.html
Creative Commons License 本作品采用 知识共享署名-非商业性使用 2.5 中国大陆许可协议进行许可。