如何在 VB.NET 中打开位于 MDI 父级中的子窗体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13533909/
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
How to open child forms positioned within MDI parent in VB.NET?
提问by user961627
How do we arrange child forms in a parent MDI window? I'm able to call and display a child form from a menu on the parent, but the child pops up outside the parent - I want it to actually be inside the parent. I've checked in C# and VB.Net solutions but they all say pretty much the same, i.e. try to access LayoutMDI, such as here:
我们如何在父 MDI 窗口中排列子窗体?我可以从父级的菜单中调用和显示子窗体,但子级弹出父级之外 - 我希望它实际上在父级内。我检查了 C# 和 VB.Net 解决方案,但他们都说几乎相同,即尝试访问 LayoutMDI,例如:
http://msdn.microsoft.com/en-us/library/x9fhk181.aspx
http://msdn.microsoft.com/en-us/library/x9fhk181.aspx
The problem is, where do I access this? When I'm in the code of my MDI parent, Me.LayoutMdi
is not recognized. In which part of the application do I put the Me.LayoutMDI
code?
问题是,我在哪里访问它?当我在我的 MDI 父代码中时,Me.LayoutMdi
无法识别。我将Me.LayoutMDI
代码放在应用程序的哪个部分?
Edit
编辑
The Me.LayoutMDI
code worked in the parent after all. I'd been trying for a while but don't know where I was going wrong.
Me.LayoutMDI
毕竟,该代码在父级中有效。我已经尝试了一段时间,但不知道我哪里出错了。
However, the child continues to pop up out of the parent. Here's an image of how that happens. The broader form in the back is the parent, and the one with the gridview and two buttons is the new child that popped up. I want it to pop up "Docked" within the parent.
但是,子级继续从父级中弹出。这是如何发生的图像。后面更宽的窗体是父窗体,带有 gridview 和两个按钮的窗体是弹出的新子窗体。我希望它在父级中弹出“停靠”。
回答by LarsTech
If your form is "outside" the MDI parent, then you most likely didn't set the MdiParent property:
如果您的表单在 MDI 父级“外部”,那么您很可能没有设置 MdiParent 属性:
Dim f As New Form
f.MdiParent = Me
f.Show()
Me, in this example, is a form that has IsMdiContainer = True
so that it can host child forms.
在这个例子中,我是一个IsMdiContainer = True
可以承载子表单的表单。
For re-arranging the child form layout, you just call the method from your MdiContainer form:
要重新安排子表单布局,您只需从 MdiContainer 表单调用该方法:
Me.LayoutMdi(MdiLayout.Cascade)
The MdiLayout enum also has tiling and arrange icons values.
MdiLayout 枚举还具有平铺和排列图标值。
回答by Jims Alias
Dont set MDI Child property from MDIForm
不要从 MDIForm 设置 MDI Child 属性
In Chileform Load event give the below code
在 Chileform Load 事件中给出以下代码
Dim l As Single = (MDIForm1.ClientSize.Width - Me.Width) / 2
Dim t As Single = ((MDIForm1.ClientSize.Height - Me.Height) / 2) - 30
Me.SetBounds(l, t, Me.Width, Me.Height)
Me.MdiParent = MDIForm1
end
结尾
try this code
试试这个代码
回答by alex Arias
You will want to set the MdiParent
property of your new form to the name of the MDI parent form, as follows:
您需要将MdiParent
新表单的属性设置为MDI 父表单的名称,如下所示:
dim form as new yourform
form.show()
form.MdiParent = nameParent
回答by Allan Empalmado
Try adding a button on mdi parent and add this code' to set your mdi child inside the mdi parent. change the yourchildformname to your MDI Child's form name and see if this works.
尝试在 mdi 父级上添加一个按钮并添加此代码'以在 mdi 父级中设置您的 mdi 子级。将 yourchildformname 更改为您的 MDI Child 的表单名称,看看这是否有效。
Dim NewMDIChild As New yourchildformname()
'Set the Parent Form of the Child window.
NewMDIChild.MdiParent = Me
'Display the new form.
NewMDIChild.Show()
回答by Murugesh KGM
Private Sub FileMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) handles FileMenu.Click
Form1.MdiParent = Me
Form1.Dock = DockStyle.Fill
Form1.Show()
End Sub
回答by Zeddy
Try the code below and.....
试试下面的代码,然后......
1 - change the name of the MENU as in my sample the menuitem was called 'Form7ToolStripMenuItem_Click'
1 - 更改菜单的名称,因为在我的示例中,菜单项被称为“Form7ToolStripMenuItem_Click”
2 - make SURE to paste it into an MDIFORM and not just a basic FORM
2 - 确保将其粘贴到 MDIFORM 而不仅仅是基本的 FORM
Then let me know if the CHILD form still shows OUTSIDE the parent form
然后让我知道 CHILD 表单是否仍然显示在父表单之外
Private Sub Form7ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form7ToolStripMenuItem.Click
Dim NewForm As System.Windows.Forms.Form
NewForm = New System.Windows.Forms.Form
'USE THE NEXT LINE - to add an existing CUSTOM form you already have
'NewForm = Form7
NewForm.Width = 400
NewForm.Height = 250
NewForm.MdiParent = Me
NewForm.Text = "CAPTION"
NewForm.Show()
DockChildForm(NewForm, "left") 'dock left
'DockChildForm(NewForm, "right") 'dock right
'DockChildForm(NewForm, "top") 'dock top
'DockChildForm(NewForm, "bottom") 'doc bottom
'DockChildForm(NewForm, "full") 'fill the client area (maximise the child INSIDE the parent)
'DockChildForm(NewForm, "Anything-Else") 'center the form
End Sub
Private Sub DockChildForm(ByRef Form2Dock As Form, ByVal Position As String)
Dim XYpoint As Point
Select Case Position
Case "left"
Form2Dock.Dock = DockStyle.Left
Case "top"
Form2Dock.Dock = DockStyle.Top
Case "right"
Form2Dock.Dock = DockStyle.Right
Case "bottom"
Form2Dock.Dock = DockStyle.Bottom
Case "full"
Form2Dock.Dock = DockStyle.Fill
Case Else
XYpoint = New Point
XYpoint.X = ((Me.ClientSize.Width - Form2Dock.Width) / 2)
XYpoint.Y = ((Me.ClientSize.Height - Form2Dock.Height) / 2)
Form2Dock.Location = XYpoint
End Select
End Sub
回答by rnizvi
Try Making the Child Form's StartPosition Property set to Center Parent. This you can select from the form Properties.
尝试将子窗体的 StartPosition 属性设置为中心父级。您可以从表单属性中进行选择。
回答by James Perkins
See this page for the solution! https://msdn.microsoft.com/en-us/library/7aw8zc76(v=vs.110).aspx
请参阅此页面以获取解决方案! https://msdn.microsoft.com/en-us/library/7aw8zc76(v=vs.110).aspx
I was able to implement the Child
form inside the parent.
我能够Child
在父级内部实现表单。
In the Example below Form2
should change to the name of your child form.
NewMDIChild.MdiParent=me
is the main form since the control that opens (shows) the child form is the parent or Me
.
在下面的示例中,Form2
应更改为您的子表单的名称。
NewMDIChild.MdiParent=me
是主窗体,因为打开(显示)子窗体的控件是父窗体或Me
.
NewMDIChild.Show()
is your child form since you associated your child form with Dim NewMDIChild As New Form2()
NewMDIChild.Show()
是您的子表单,因为您将子表单与 Dim NewMDIChild As New Form2()
Protected Sub MDIChildNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim NewMDIChild As New Form2()
'Set the Parent Form of the Child window.
NewMDIChild.MdiParent = Me
'Display the new form.
NewMDIChild.Show()
End Sub
Simple and it works.
简单,它的工作原理。