VB.NET - 按钮调整大小

时间:2015-09-25 10:39:42

标签: vb.net winforms

我正在编写一个全屏“kiosk-esque”应用程序,我希望按钮能够相应地适应。

目前,当表单打开时,我将按钮固定并垂直拉伸,但我希望它们也能水平调整大小?

有一种简单的方法吗? In editor view In application view

2 个答案:

答案 0 :(得分:1)

对于WinForms,请使用包含3列的TableLayoutPanel,宽度设置为33.33%。在每个面板中放置一个按钮,将其Dock属性设置为Fill,并使用Anchors拉伸整个TableLayoutPanel

对于WPF,您将使用Grid控件。

答案 1 :(得分:0)

使用过的代码来执行此操作。只需将此代码添加到表单中。

Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
    Button1.Width = (Me.Width - 100) / 3 - 10 'That "10" in the last comes from 15*2/3 .So if the space2 is 15 this number must be 10.
    Button2.Width = (Me.Width - 100) / 3 - 10 'That "Me.Width - 100" comes from 2*50 .If you want space1 50,this number must be 100.
    Button3.Width = (Me.Width - 100) / 3 - 10 'Resize the last.
    Me.Button1.Location = New Point(50, Button1.Location.Y) 'Start from 50 in X Coordinate.
    Me.Button2.Location = New Point(Button1.Width + 65, Button2.Location.Y) 'That 65 means "50 + 15".(50 is space1,15 is space2.)
    Me.Button3.Location = New Point(Button1.Width + Button2.Width + 80, Button3.Location.Y) 'As you expected "50 + 15 + 15 = 80" 
    'This works for only 3 buttons(horizontally).Get the logic and improve your code for your button count.
End Sub

Here is the image

您可以在此处看到我在我的代码中使用的space1space2。如果您的表单为SizeChanged,那么3个按钮会按比例调整大小。