sowjanya attaluri
sowjanya attaluri

Reputation: 911

How to resize controls while resizing form in vb.net?

I am having vb.net application with UI Controls.If I resize the form,the UI controls alignment varies improperly. Please guide me to solve this issue.

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

Please refer my above code and guide me.

Upvotes: 0

Views: 2470

Answers (1)

Mike Bateman
Mike Bateman

Reputation: 380

have you tried using a TableLayoutPanel control? You can add a button to each cell and then anchor the TableLayoutPanel to Top, Bottom, Left, Right.

This will then handle the resizing of the button for you - no need for any coding?

Does this fix your issue?Image showing two buttons docked in cells

Upvotes: 2

Related Questions