Saturn
Saturn

Reputation: 18149

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

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.

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?

Upvotes: 10

Views: 70121

Answers (9)

tejasgupta
tejasgupta

Reputation: 109

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.

Hope this helps!

Upvotes: 0

Aashutosh Taikar
Aashutosh Taikar

Reputation: 155

I struggled a lot with the anchor and dock as 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 width and Height property of the WinForm using the Resize event of that form. In the below code sample the form name is 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

Upvotes: -1

gani
gani

Reputation: 21

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

Upvotes: 2

kevin
kevin

Reputation: 1

Click on tab 1 or tab 2 Then on the side in the Property Tab look for the Dock property. Then select the one in the middle or type full.

Or

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

Hope This Helped!

Upvotes: 0

Dean
Dean

Reputation: 11

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.

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.

If you want the control placed a specific distance from each edge of the control surface use 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.

Upvotes: 1

Richard Dong
Richard Dong

Reputation: 704

You use the Dock and Anchor properties to control how a component control is resized when its parent/container is resized.

To make a control fill its container, simply

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

To retain some margins, set the Anchor property

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.

Upvotes: 0

Ankur Singh
Ankur Singh

Reputation: 81

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

Upvotes: 1

Shiridish
Shiridish

Reputation: 4962

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

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.

Upvotes: 13

xpda
xpda

Reputation: 15813

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

Upvotes: 8

Related Questions