在VB.NET中的父窗体上动态安排用户控件 [英] Arrange user controls dynamically on parent form in VB.NET
问题描述
如何通过代码来在我的父窗体中安排控件?
How can I arrange controls in my parent form by code?
到目前为止,我使用此代码调用用户控件;
So far I call the user controls with this code;
Me.ParentForm.Controls.Remove(Me)
controlMain()
每当用户调整父窗体大小或最大化表单时,我都希望用户控件自行排列。目前,我通过设置控件,
I want the user controls arrange itself whenever the user resize the parent form or maximize the form. Currently, I set the controls by,
Public Sub controlMain()
Dim usrctl As New _ctlMain
_Main.Controls.Add(usrctl)
usrctl.Location = New Point(_Main.Width / 2 - usrctl.Width / 2, _Main.Height / 2 -usrctl.Height / 2)
End Sub
在模块上。 _Main 是我的父窗体, _ctlMain 是被调用的控件。我不打算在设计过程中将用户控件放在父窗体上,因为在调用活动控件中的特定函数后,我有其他用户控件可以调用。
我尝试了 usercontrol的autosize 属性,但我想它不适用于我的应用程序。 Usercontrol没有dock和anchor属性。
which is on a module. _Main is my parent form while _ctlMain is the control being called. I do not intend to put the user control on the parent form during design because I have other user controls to call after a specific function in an active control is called.
I have tried the autosize property of usercontrol but I guess it doesn't work on my application. Usercontrol doesn't have the dock and anchor properties.
推荐答案
使用其中一个自动布局控件,如 FlowLayoutPanel 或者 TableLayoutPanel 。
Use one of the automatic layout controls, like a FlowLayoutPanel or a TableLayoutPanel.
将您的用户控件添加到表单本身,将它们添加到使用 DockStyle.Fill
。
Instead of adding your user controls to the form itself, add them to either a FlowLayoutPanel or TableLayoutPanel control that has been placed on top of the form using DockStyle.Fill
.
在我看来,FlowLayoutPanel就是您想要的。因此,控件的布局完全自动处理,并且它们按照从左到右或从上到下的顺序进行定位,具体取决于 FlowDirection
属性。
It sounds to me like a FlowLayoutPanel is what you want. With that, the layout of the controls is handled entirely automatically, and they are positioned either in left-to-right or top-to-bottom order, depending on the value of the FlowDirection
property.
选择TableLayoutPanel的唯一原因是您需要更精确地控制控件的确切位置。它可以像HTML表一样工作,每个控件都有自己的单元。
The only reason to choose a TableLayoutPanel is if you need to have more precise control over the exact positioning of the controls. It works just like an HTML table, with each control getting its own "cell".
您也可以设置 Dock $ c $如果您想确保自动调整尺寸,请单击
填充
各个用户控件的属性。例如,您可以设置每个控件来填充它放置在TableLayoutPanel中的整个单元。
You can also set the Dock
and/or Fill
properties of the individual user controls if you'd like to ensure that their sizes are automatically adjusted. For example, you can set each control to fill the entire cell in which it is placed in a TableLayoutPanel.
这篇关于在VB.NET中的父窗体上动态安排用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!