Jay
Jay

Reputation: 143

Enlarge form controls when the maximize button is clicked

I have designed a WinForm in vb.NET with VS2017. I would now like to add a feature whereby the form has two sizes - the normal size it initially loads as and also a larger size that is triggered when the user clicks the maximize button on the form. If possible, I don't want the form to resize if a user drags its sides etc, I only want it to work if they click the maximize button.

The form itself is laid out as shown, it is made up of a Panel that will always be anchored to the top, and a TabControl that will enlarge if the window is maximized. The TabControl is made up of 6 TabPages, all of which have GroupBoxes and Panels on them, and each GroupBox/Panel has a couple of Labels and TextBoxes. When it enlarges, I would like the size of the Labels and Textboxes to grow to the same percentage as the TabControl has been enlarged - basically everything in the TabControl grows by the same percentage

Form Layout

A possible solution I am thinking of is capturing the maximized and 'normal sized' form states (Normal Size being triggered at load time and if a user clicks to unmaximize the maximized form), and then looping through each control on each tab page and setting the style of each control to suit the desired larger layout.

I have managed to capture the maximised and minimised events as follows:

Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize

    If Me.WindowState = FormWindowState.Maximized Then

        Console.WriteLine("I have been maximised")

    End If

    If Me.WindowState = FormWindowState.Normal Then
        Console.WriteLine("I have been minimised")
    End If

End Sub

I tried setting the Anchor Style of every control except the TabControl to "Top,Bottom,Right,Left", and then in the above subroutine, setting the TabControl AncorStyle to "Top,Bottom,Right,Left", but I had two problems with this:

1) The following code didn't work correctly to set the AnchorStyle to "Top,Bottom,Right,Left"

TabControl1.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right

The following was also unsuccessful:

TabControl1.Anchor = AnchorStyles.Top And AnchorStyles.Bottom And AnchorStyles.Left And AnchorStyles.Right

2) When I manually set the AnchorStyle of the TabControl to "Top,Bottom,Left,Right" in the Designer, and clicked Maximize when I ran the form, the form spreads out in an undesirable way, with the GroupBoxes on the TabPage overlapping, the text not changing its size in the Labels etc:

enter image description here

Any help on this would be greatly appreciated

Upvotes: 0

Views: 3816

Answers (1)

danish
danish

Reputation: 5610

TableLayoutPanel. That's what you need. Take a look at it. It is basically a grid like setup where each cell can only have one control. You can set sizing for rows and columns to percentage or absolute to get desired layout.

This will work with resizing as well.

Dock the controls to fill in all the cells and they will grow and shrink accordingly.

Upvotes: 1

Related Questions