如何在vb.net中调整表单大小时调整控件大小?

时间:2015-08-24 09:42:47

标签: vb.net

  

我正在使用带有UI Control的vb.net应用程序。如果我调整表单大小,UI控件对齐方式会有不正确的变化。请指导我解决这个问题。

Dim CWidth As Integer = Me.Width ' Current Width
Dim CHeight As Integer = Me.Height ' Current Height
Dim IWidth As Integer = Me.Width ' Initial Width
Dim IHeight As Integer = Me.Height ' Initial Height

Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    IWidth = Me.Width
    IHeight = Me.Height

End Sub

Private Sub Form4_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    Dim RW As Double = (Me.Width - CWidth ) / CWidth ' Ratio change of width
    Dim RH As Double = (Me.Height - CHeight ) / CHeight ' Ratio change of height

    For Each Ctrl As Control In Controls
        Ctrl.Width += CInt(Ctrl.Width * RW)
        Ctrl.Height += CInt(Ctrl.Height * RH)
        Ctrl.Left += CInt(Ctrl.Left * RW)
        Ctrl.Top += CInt(Ctrl.Top * RH)
    Next

    CWidth = Me.Width
    CHeight = Me.Height

End Sub
  

请参考我的上述代码并指导我。

1 个答案:

答案 0 :(得分:2)

您是否尝试过使用TableLayoutPanel控件?您可以为每个单元格添加一个按钮,然后将TableLayoutPanel锚定到Top,Bottom,Left,Right。

这将为您处理按钮的大小调整 - 无需任何编码?

这是否可以解决您的问题?Image showing two buttons docked in cells