vb.net 调整窗口大小时,我的表单元素如何调整大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14116848/
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-09 16:32:55  来源:igfitidea点击:

How can my form elements resize when the window is resized?

vb.netwinformsvisual-studio

提问by Voldemort

I have this form, with a tab control and a listbox inside:

我有这个表单,里面有一个选项卡控件和一个列表框:

enter image description here

在此处输入图片说明

When I resize the window's height, I get something like this:

当我调整窗口的高度时,我得到如下信息:

enter image description here

在此处输入图片说明

However, I actually wanted the tab control and the listbox to resize along, having the following result:

但是,我实际上希望选项卡控件和列表框一起调整大小,结果如下:

enter image description here

在此处输入图片说明

I believe I could achieve this effect by just responding to some kind of onResize()method in the form, and do my own calculations to manually update the size of the tab control and the listbox accordingly.

我相信我可以通过响应onResize()表单中的某种方法来实现这种效果,并进行我自己的计算以相应地手动更新选项卡控件和列表框的大小。

However, I've seen many applications achieve this effect, so I suspect there is actually a better way to do so - perhaps a builtin feature.

但是,我已经看到许多应用程序实现了这种效果,所以我怀疑实际上有更好的方法 - 也许是内置功能。

Do you know a better way to achieve this effect?

你知道实现这种效果的更好方法吗?

回答by Cdeez

Here is a good tutorial that explains how to resize winform controls on resizing the container form control using the dock and anchor properties:

这是一个很好的教程,它解释了如何使用停靠栏和锚点属性调整容器窗体控件大小时调整 winform 控件的大小:

http://www.techrepublic.com/article/manage-winform-controls-using-the-anchor-and-dock-properties/6165908

http://www.techrepublic.com/article/manage-winform-controls-using-the-anchor-and-dock-properties/6165908

Setting the four values of the anchor property(top,right,bottom,left), you can make your control's edges to stay stationary with respect to the Form control even when it is re-sized.

设置锚属性的四个值(顶部、右侧、底部、左侧),您可以使您的控件的边缘相对于 Form 控件保持静止,即使它被重新调整大小。

回答by xpda

You can use the anchor property of the tab control -- just anchor all four sides.

您可以使用选项卡控件的锚属性——只需锚定所有四个边。

回答by gani

This may help:

这可能有帮助:

Private Sub frmMain_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged 'tab container sizechanged event
        tabMain.Dock = DockStyle.None 'set dock to none to allow resize
        tabMain.Size = New Size(New Point(3, 3)) ' set size anything you want
        tabMain.Dock = DockStyle.Fill 'set dock to fill to fit to container
    End Sub

回答by Dean

To get the desired result place the control how you want it to be in the standard size form. then use the Anchor property to Top, Bottom, Left, Right. this allow you to specify that you always want the control to be relatively positioned in the form based off of the smallest size.

要获得所需的结果,请按照您希望的方式将控件置于标准尺寸形式中。然后使用 Anchor 属性到 Top、Bottom、Left、Right。这允许您指定始终希望控件在基于最小尺寸的表单中相对定位。

I.E: Set the anchor property to Top, Bottom, Left, Right this will ensure that the Top, Bottom, Left, Right edge of the control will always be the same distance from each edge regardless of the parents size.

IE:将锚点属性设置为 Top、Bottom、Left、Right,这将确保控件的 Top、Bottom、Left、Right 边缘与每个边缘的距离始终相同,无论父级大小如何。

If you want the control placed a specific distance from each edge of the control surface use Anchor Top, Bottom, Left, Right

如果您希望控件与控制表面的每个边缘保持特定距离,请使用 Anchor Top、Bottom、Left、Right

if you want the control top and left edge locked 8 pixels from the top and left edge of the parent, set the Anchor Property to Top, Left the control will never resize it will always stay relatively positioned at 8,8.

如果您希望控件的顶部和左侧边缘距离父级的顶部和左侧边缘锁定 8 个像素,请将 Anchor 属性设置为 Top,Left 控件将永远不会调整大小,它将始终保持相对位置在 8,8。

回答by Ankur Singh

You Can use Dock property of Of Both Tab and listbox to be fill

您可以使用 Of Both Tab 和 listbox 的 Dock 属性来填充

回答by Richard Dong

You use the Dockand Anchorproperties to control how a component control is resized when its parent/container is resized.

您可以使用DockAnchor属性来控制在调整其父/容器大小时如何调整组件控件的大小。

To make a control fill its container, simply

要使控件填充其容器,只需

theControl.Dock = System.Windows.Forms.DockStyle.Fill

To retain some margins, set the Anchorproperty

要保留一些边距,请设置Anchor属性

theControl.Anchor = CType((System.Windows.Forms.AnchorStyles.Top _
    Or System.Windows.Forms.AnchorStyles.Bottom _
    Or System.Windows.Forms.AnchorStyles.Left _
    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)

You can also set these properties in the Property tool window.

您还可以在“属性”工具窗口中设置这些属性。

回答by kevin

Click on tab 1or tab 2Then on the side in the Property Tablook for the Dockproperty. Then select the one in the middle or type full.

单击tab 1tab 2然后在Property Tab查找Dock属性的一侧。然后选择中间的那个或者输入full。

Or

或者

You can look for Anchorand select the bottom tab, the top tag, the right tag, and the left tag.

你可以寻找Anchor和选择bottom tabtop tag,在right tagleft tag

Hope This Helped!

希望这有帮助!

回答by tejasgupta

You may use the following code in the form load event...

您可以在表单加载事件中使用以下代码...

TabControl1.Dock = DockStyle.Fill
ListBox1.Dock = DockStyle.Fill

This will Resize the controls when the form is also resized.

当窗体也调整大小时,这将调整控件的大小。

You may also use tableLayout Panel or anchor property of the controls to achive the same result.

您也可以使用控件的 tableLayout 面板或锚点属性来实现相同的结果。

Hope this helps!

希望这可以帮助!

回答by Aashutosh Taikar

I struggled a lot with the anchorand dockas I had many controls from other libraries and some of them did not have the anchor and dock properties, but this technique helped me. It dynamically sets the size of all the controls in the WinForm by referencing it with the widthand Heightproperty of the WinForm using the Resize event of that form. In the below code sample the form name is Main.

我在锚点停靠点方面遇到了很多困难,因为我有许多来自其他库的控件,其中一些没有锚点和停靠点属性,但这种技术对我有所帮助。它通过使用该窗体的 Resize 事件引用 WinForm 的宽度高度属性来动态设置 WinForm 中所有控件的大小。在下面的代码示例中,表单名称是 Main。

Dim CuRWidth As Integer = Me.Width
Dim CuRHeight As Integer = Me.Height

    Private Sub Main_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
            Dim RatioHeight As Double = (Me.Height - CuRHeight) / CuRHeight
            Dim RatioWidth As Double = (Me.Width - CuRWidth) / CuRWidth

            For Each ctrl As Control In Controls
                ctrl.Width += ctrl.Width * RatioWidth
                ctrl.Left += ctrl.Left * RatioWidth
                ctrl.Top += ctrl.Top * RatioHeight
                ctrl.Height += ctrl.Height * RatioHeight
            Next

            CuRHeight = Me.Height
            CuRWidth = Me.Width
End Sub

Source: https://www.youtube.com/watch?v=QVi1ve5qHXA

来源:https: //www.youtube.com/watch?v=QVi1ve5qHXA