如何卸载用户窗体而不关闭其他所有内容? [英] How do I unload a userform without closing everything else?

查看:165
本文介绍了如何卸载用户窗体而不关闭其他所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个用户表单( frmDatastation1 frmDatastation2 )。当用户单击 frmDatastation1 上的cmdbutton时,第一个窗体将被隐藏( frmDatastation.hide ),第二个窗体显示( frmDatastation.show )。那工作正常当用户完成 frmDatastation2 时,我想回到 frmDatastation1 并卸载 frmdatastation2 (要清除所有内容,请稍后再从 frmDatastation1 )重新使用。但是当我卸载 frmDatastation2 时,一切都会关闭(甚至 frmDatastation1 )。



这是我在 frmDatastation2 中的代码:

  frmDatastation1.TextBox1 .Text =这是为了重置我的文本框不确定它需要
我隐藏的frmDatastation2
卸载frmDatastation1
frmDatastation1.Show vbModeless'能够继续下一步
frmDatastation1.TextBox1.SetFocus
卸载frmDatastation2',当这行被执行时,它关闭所有的东西(甚至frmDatastation1)


解决方案

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

  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).

这篇关于如何卸载用户窗体而不关闭其他所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆