如何在不closures其他所有的情况下卸载用户窗体?
我有两个frmDatastation1
frmDatastation2
( frmDatastation1
和frmDatastation2
)。 当用户单击frmDatastation1上的frmDatastation1
,第一个窗体变为隐藏( frmDatastation.hide
),第二个frmDatastation.show
显示( frmDatastation.show
)。 这工作正常。 当用户完成frmDatastation2
,我想回到frmDatastation1
并卸载frmdatastation2
(从frmDatastation1
删除所有内容以便再次使用它)。 但是,当我卸载frmDatastation2
,一切都closures(甚至frmDatastation1
)。
这是我在frmDatastation2
的代码:
frmDatastation1.TextBox1.Text = "" 'this is to reset my textbox not sure it's needed Me.Hide 'hiding frmDatastation2 Unload frmDatastation1 frmDatastation1.Show vbModeless ' to be able to continue with the next step frmDatastation1.TextBox1.SetFocus Unload frmDatastation2 'when this line is executed, it closes everything (even frmDatastation1)
使用对象而不是使用全局/默认实例。
Sub DoSomething() Dim parentForm As frmDatastation1 Set parentForm = New frmDatastation1 parentForm.Show vbModal End Sub
没有必要真正Unload
任何东西 – 当执行退出DoSomething
并且对象超出范围时,对象引用parentForm
将会死亡。
Sub ShowChildForm() Dim childForm As frmDatastation2 Set childForm = New frmDatastation2 childForm.Show vbModal End Sub
为了使你的两个对象相互“交谈”,你需要传递对象引用。 说孩子的forms需要一个父母的参考,它可以有这样一个属性:
Private parentForm As frmDatastation1 Public Property Get Parent() As frmDatastation1 Set Parent = parentForm End Property Public Property Set Parent(ByVal value As frmDatastation1) Set parentForm = value End Property
然后父窗体可以这样做:
Sub ShowChildForm() Dim childForm As frmDatastation2 Set childForm = New frmDatastation2 Set childForm.Parent = Me childForm.Show vbModal textbox1.Text = childForm.textbox12.Text End Sub
…虽然子窗体可以像这样访问其父窗体:
Sub DoSomething() Me.Caption = Parent.textbox1.Text End Sub
如果您想要访问已closures但尚未卸载/丢弃的表单的属性,请不要closures它 – 只需将其隐藏起来(您需要在QueryClose
实现该行为以防止在用户“X-out”的forms)。