vb.net 如何在同一窗口中打开窗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26686279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:25:38  来源:igfitidea点击:

How to open windows forms in the same window

vb.netmdi

提问by Wakka02

Assuming I want to open Form B from Form A, I use the following code in form A:

假设我想从表单 A 打开表单 B,我在表单 A 中使用以下代码:

FormB.Show()

Me.Close()

This results in form A's window closing and Form B's window opening. This is very disruptive, so is it possible to open form B in form A's window? I've read something about an MdiContainer, so I did the following already:

这将导致窗体 A 的窗口关闭和窗体 B 的窗口打开。这是非常具有破坏性的,那么是否可以在表单A的窗口中打开表单B?我已经阅读了有关 MdiContainer 的一些内容,因此我已经执行了以下操作:

-Created a new Windows Form in Visual Studio, calling it MainForm and setting its isMdiContainer property to True. -Used the following code when opening Windows Forms:

- 在 Visual Studio 中创建了一个新的 Windows 窗体,将其命名为 MainForm 并将其 isMdiContainer 属性设置为 True。- 打开 Windows 窗体时使用以下代码:

FormB.MdiParent = MainForm
FormB.Show()

Me.Close()

But this results in FormB not showing up at all.

但这导致 FormB 根本不显示。

How do I do this?

我该怎么做呢?

--EDIT--

- 编辑 -

Based on the below replies, it seems the general consensus is to exclude Me.Close(). I've done that, but it seems that the problem isn't Me.Close(), but FormB.MdiParent = MainForm. For some reason, whenever I include that line in FormB's Load function, FormB doesn't even show up at all.

根据以下回复,似乎普遍的共识是排除 Me.Close()。我已经这样做了,但问题似乎不是Me.Close(),而是FormB.MdiParent = MainForm。出于某种原因,每当我在 FormB 的 Load 函数中包含该行时,FormB 甚至根本不显示。

回答by Creator

So you have 2 forms , lets say "MainForm and Form1"

所以你有 2 个表单,让我们说“MainForm 和 Form1”

In youre mainForm you add the code "Me.IsMdiContainer = True" in the form load event.
To set the MdiContainer to the form.

在您的 mainForm 中,您在表单加载事件中添加代码“Me.IsMdiContainer = True”。
将 MdiContainer 设置为窗体。

Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.IsMdiContainer = True
End Sub

And a button to open the second form.

还有一个按钮来打开第二个表单。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Form1.Show()
End Sub

Then in the form1 loadevent you add the code "Me.MdiParent = MainForm"

然后在 form1 loadevent 中添加代码“Me.MdiParent = MainForm”

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.MdiParent = MainForm
End Sub

Then it works "and dont call me.close then you close the form."

然后它起作用“不要打电话给我。关闭然后关闭表单。”

回答by Qasim-nawaz

I Think You Want to show Both Forms at a Time. then for that Don't Use the .Close()Method for Form A. Well there's another thing IF you want, when Form B is Called Form A can be Hide using its .Hide()Method. also you can do the same from the child Form using the main forms object to show main form and Hide the Child Form.

我认为您想一次显示两种形式。那么对于.Close()表单 A ,不要使用该方法。好吧,如果您想要,还有另一件事,当表单 B 被称为表单 A 时,可以使用其.Hide()方法隐藏。您也可以使用主表单对象从子表单执行相同的操作以显示主表单并隐藏子表单。

回答by Idle_Mind

From FormA, do something like:

在 FormA 中,执行以下操作:

FormB frmB = new FormB()
frmB.MdiParent = Me
frmB.Show()

Don't close the current form with Me.Close()!

不要关闭当前表单Me.Close()

回答by Amr Habib

You can use panel objectas containerto your new form.

您可以使用面板objectcontainer您的新form