如何在单独的用户窗体中操作控件?
How to manipulate Controls in a separate UserForm?
我有两种形式:一种是创造性地命名为 UserForm1
,我所有的魔法都在这里发生,另一种是 SettingsForm
,用户可以在其中调整一些设置。 UserForm1
可以打开 SettingsForm
并在以后初始化时加载任何已保存的设置。
我 运行 遇到的问题是将当前打开的 UserForm1
更新为保存在 SettingsForm
中的新选择的设置。其中一个设置是一组选项按钮中的默认选择。
我尝试修改我在别处使用的循环来处理选项组,方法是将 Me.Controls
更改为 [Forms]![exportForm].Controls
,但这会引发错误。我以前从未引用过其他形式的控件,所以我不太确定我在做什么(阅读:我完全无能为力)。 (defBGU
是前面代码中定义的字符串)
Dim opt As Control
For Each opt In [Forms]![exportForm].Controls
If TypeName(opt) = "OptionButton" Then
If opt.Name = defBGU Then
opt.Value = True
End If
End If
Next
用户窗体是 VBA 中的 class,您可以(并且应该)使用变量来访问它。
通常,您在代码中编写类似
的内容
UserForm1.Show
这将创建一个所谓的 默认实例 的形式。但你也可以
Dim frm as UserForm1
set frm = new UserForm1
frm.show
您可以使用此变量并访问它的所有成员(如果您在表单代码中,Me
只不过是对实例本身的引用)。
因此您的 UserForm1 中的部分代码可能类似于
Dim frmSettings As SettingsForm ' Declare a variable for the setting form
Private Sub CommandButton1_Click()
' Create a new instance of the setting form
If frmSettings Is Nothing Then Set frmSettings = New SettingsForm
' Do some manipulations and show it
With frmSettings
.Caption = "Greetings from " & Me.Name
.Label1.Caption = "I was set by " & Me.Name
.TextBox1.Text = "Me too..."
.Show
End With
End Sub
我有两种形式:一种是创造性地命名为 UserForm1
,我所有的魔法都在这里发生,另一种是 SettingsForm
,用户可以在其中调整一些设置。 UserForm1
可以打开 SettingsForm
并在以后初始化时加载任何已保存的设置。
我 运行 遇到的问题是将当前打开的 UserForm1
更新为保存在 SettingsForm
中的新选择的设置。其中一个设置是一组选项按钮中的默认选择。
我尝试修改我在别处使用的循环来处理选项组,方法是将 Me.Controls
更改为 [Forms]![exportForm].Controls
,但这会引发错误。我以前从未引用过其他形式的控件,所以我不太确定我在做什么(阅读:我完全无能为力)。 (defBGU
是前面代码中定义的字符串)
Dim opt As Control
For Each opt In [Forms]![exportForm].Controls
If TypeName(opt) = "OptionButton" Then
If opt.Name = defBGU Then
opt.Value = True
End If
End If
Next
用户窗体是 VBA 中的 class,您可以(并且应该)使用变量来访问它。 通常,您在代码中编写类似
的内容UserForm1.Show
这将创建一个所谓的 默认实例 的形式。但你也可以
Dim frm as UserForm1
set frm = new UserForm1
frm.show
您可以使用此变量并访问它的所有成员(如果您在表单代码中,Me
只不过是对实例本身的引用)。
因此您的 UserForm1 中的部分代码可能类似于
Dim frmSettings As SettingsForm ' Declare a variable for the setting form
Private Sub CommandButton1_Click()
' Create a new instance of the setting form
If frmSettings Is Nothing Then Set frmSettings = New SettingsForm
' Do some manipulations and show it
With frmSettings
.Caption = "Greetings from " & Me.Name
.Label1.Caption = "I was set by " & Me.Name
.TextBox1.Text = "Me too..."
.Show
End With
End Sub