且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何卸载用户窗体而不关闭其他所有内容?

更新时间:2023-12-06 16:33:04

使用对象而不是使用全局/默认实例。

  Sub DoSomething()
Dim parentForm As frmDatastation1
Set parentForm = New frmDatastation1
parentForm.Show vbModal
End Sub
/ pre>

没有必要真的卸载任何东西 - 对象引用ence parentForm 将在执行退出 DoSomething 时死机,对象超出范围。

  Sub ShowChildForm()
Dim childForm As frmDatastation2
Set childForm = New frmDatastation2
childForm.Show vbModal
End Sub

为使您的两个对象彼此交流,您需要传递对象引用。说孩子表单需要一个对父母的引用,它可以有一个这样的属性:

 私人父窗体作为frmDatastation1 

公共属性获取父()作为frmDatastation1
设置Parent = parentForm
结束属性

公共属性集父(ByVal值作为frmDatastation1)
设置parentForm = value
End Property

然后父表单可以这样做:

  Sub ShowChildForm()

Dim childForm As frmDatastation2
Set childForm = New frmDatastation2
设置childForm.Parent = Me
childForm.Show vbModal

textbox1.Text = childForm.textbox12.Text

End Sub

...虽然子表单可以访问其父表单,如下所示:

  Sub DoSomething()
Me.Caption = Parent.textbox1.Text
End Sub

如果你想要能够交流确定已关闭但尚未卸载/丢弃的表单的属性,不要关闭它 - 只是隐藏它(您将需要在 QueryClose 以防止在用户X-out的窗体中丢弃该对象。


I have two userforms (frmDatastation1 and frmDatastation2). When the user clicks on cmdbutton on frmDatastation1, the first form becomes hidden (frmDatastation.hide) and the second appears (frmDatastation.show). That works fine. When the user is done with frmDatastation2, I'd like to go back to frmDatastation1 and unload frmdatastation2 (to erase all it's content to use it again later from frmDatastation1). But when I unload frmDatastation2, everything closes (even frmDatastation1).

Here's my code in 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)

Use objects instead of using the global/default instance.

Sub DoSomething()
    Dim parentForm As frmDatastation1
    Set parentForm = New frmDatastation1
    parentForm.Show vbModal
End Sub

There's no need to really Unload anything then - the object reference parentForm will die when execution exits DoSomething and the object goes out of scope.

Sub ShowChildForm()
    Dim childForm As frmDatastation2
    Set childForm = New frmDatastation2
    childForm.Show vbModal
End Sub

To make your two objects "talk" to each other, you need to pass object references around. Say the child form needs a reference to the parent, it could have a property like this:

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

And then the parent form could do this:

Sub ShowChildForm()

    Dim childForm As frmDatastation2
    Set childForm = New frmDatastation2
    Set childForm.Parent = Me
    childForm.Show vbModal

    textbox1.Text = childForm.textbox12.Text

End Sub

...While the child form could access its parent form like this:

Sub DoSomething()
    Me.Caption = Parent.textbox1.Text
End Sub

If you want to be able to access the properties of a form that's closed but not yet unloaded/discarded, don't close it - just hide it instead (you'll need to implement the behavior in QueryClose to prevent discarding the object when the user "X-out"'s of the form).