Results 1 to 9 of 9

Thread: Resize the controls on windows form when the form is maximed or in normal state

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    6

    Question Resize the controls on windows form when the form is maximed or in normal state

    In case of windows desktop vb.net application, All the controls on my form should scale/resize based on the size of the form when the form is maximized. I have a flow layout panel,multiple controls on this panel, a panel with report viewer (of .rdlc report(client not server side)) as one of the many controls on the form.Also, I need to revert back to the regular size of all these controls when the form is in normal state?Should I do in the form_resize event?

    I tried as below:
    Private Sub formMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

    If Me.WindowState = "2" Then
    ResizeFormToScreen(Me)

    tabcntrl.Size = New Size(tabcntrl.Width * 0.4, tabcntrl.Height * 1.5)
    flpcntrl.Size = New Size(flpcntrl.Width + 40, flpcntrl.Height + 100)

    flpcntrl.Refresh()

    ElseIf Me.WindowState = "1" Then
    MessageBox.Show("Form minimized")
    Me.SuspendLayout()
    Else 'Normal state

    Me.ResumeLayout()

    pnlViewer.Size = New Size(981, 596)
    pnlViewer.Refresh()

    tcCntrl.Size = New Size(998, 602)
    flpMain.Size = New Size(flpMain.Width - 40, flpMain.Height - 100)
    EndIf


    Please suggest.I appreciate your help.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Resize the controls on windows form when the form is maximed or in normal state

    Take a look at the .Anchor property of pretty much any control. This resizes the control any time the parent is resized. The way I learned how to use the .Anchor properly was I started a blank project and added just about every control and used differnet anchors like: Left and Right or Left and Top, and see what happened when I maximized my form.

    Edit - Start a new project and add a Datagridview, Button, and Textbox. Then press F7 and add this:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            With Me
                .Text = "Anchor Test"
                .Size = New Size(300, 300)
            End With
            With DataGridView1
                .Location = New Point(13, 13)
                .Size = New Size(259, 208)
                .Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top Or AnchorStyles.Bottom
            End With
            With Button1
                .Location = New Point(12, 227)
                .Size = New Size(75, 23)
                .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left
            End With
            With TextBox1
                .Location = New Point(93, 229)
                .Size = New Size(179, 20)
                .Anchor = AnchorStyles.Bottom Or AnchorStyles.Right Or AnchorStyles.Left
            End With
        End Sub
    End Class
    Last edited by dday9; Aug 30th, 2012 at 11:09 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    6

    Re: Resize the controls on windows form when the form is maximed or in normal state

    Thanks for the reply with example code. I have used Anchor property for controls that I wanted them to stick to a position. I didn't wanted to use Anchor propoerty as that may screw up the alignment of the rest of the controls on my form(I have toolstrip on top of my form,have a flow layout panel on which, another flow layout panel with groupbox on left, flow layout panel with tab control that contained a report viewer with .rdlc report on each tabpage). I used dock.fill when I wanted my reportviewer report to fill the parent control.I missed to include the below code for the function (which I tried) :

    Private Sub ResizeFormToScreen(ByVal FrmName)
    Dim actualfrmW, actualfrmH As integer
    Dim XRatio, YRatio As Long
    actualfrmW = Screen.PrimaryScreen.Bounds.Width
    actualfrmH = Screen.PrimaryScreen.Bounds.Height

    XRatio = me.Width / actualfrmW
    YRatio = me.Height / actualfrmH
    'me.Width is my maiximized form width
    me.Height = actualfrmW.Height * YRatio
    me.Width = actualfrmH.Width * XRatio
    For I = 0 To me.Controls.Count - 1
    FrmName.Controls(I).Left = FrmName.Controls(I).Left * XRatio
    FrmName.Controls(I).Top = FrmName.Controls(I).Top * YRatio
    FrmName.Controls(I).Height = FrmName.Controls(I).Height * YRatio
    FrmName.Controls(I).Width = FrmName.Controls(I).Width * XRatio
    ' FrmName.Controls(I).Font.Size = FrmName.Controls(I).Font.Size * XRatio
    Next I
    End sub
    Private Sub formMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

    If Me.WindowState = "2" Then
    ResizeFormToScreen(Me)

    tabcntrl.Size = New Size(tabcntrl.Width * 0.4, tabcntrl.Height * 1.5)
    flpcntrl.Size = New Size(flpcntrl.Width + 40, flpcntrl.Height + 100)

    flpcntrl.Refresh()

    ElseIf Me.WindowState = "1" Then
    MessageBox.Show("Form minimized")
    Me.SuspendLayout()
    Else 'Normal state

    Me.ResumeLayout()

    pnlViewer.Size = New Size(981, 596)
    pnlViewer.Refresh()

    tcCntrl.Size = New Size(998, 602)
    flpMain.Size = New Size(flpMain.Width - 40, flpMain.Height - 100)
    EndIf


    Can I use "PerformAutoScale()" for this purpose?
    Last edited by Neena20; Aug 30th, 2012 at 11:58 AM.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: Resize the controls on windows form when the form is maximed or in normal state

    Personally the best why I have found to handle non-Anchored, non-Docked controls, I figure out how I want then aligned and then adjust them every time the form is resized.

    For instance, if you have a RichTextBox (RTB1) that you want to extend across the whole form leaving a 10p gap on either side and from the top to bottom, leaving a 10p border on top and a 100p border on bottom, with a button (B1) centered on the page under the RTB, I would do something like the following:
    Code:
        Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
            RTB1.Left = 10 'Anything aligning with the left or top of the form will not change based on size
            RTB1.Width = Me.Width - 26 'I increased this from 20 to 26 since Form width is based on it's place in the overall desktop space thus includes the 3p borders
            RTB1.Top = 10  'Anything aligning with the left or top of the form will not change based on size
            RTB1.Height = Me.Height - 110
            B1.Left = ((RTB1.Width / 2) - (B1.Width / 2))
            B1.Top = RTB1.Top + RTB1.Height + 10 'The 10 is how far below the bottom of the RTB you want the button.
        End Sub
    This is the long way to do things, but once you get it how you want it, it should preserve the layout no matter how the form is resized.

    It is of course easier if you can make liberal use of panels, and Splitter Controls and dock as many controls as possible but this way works when you can't use panels and docking.

    EDIT: made a few small changes and added comments to the code.
    Last edited by Maverickz; Aug 30th, 2012 at 05:52 PM.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Resize the controls on windows form when the form is maximed or in normal state

    I still don't understand why anchoring and/or docking the controls isn't what you need. But if you want to take the difficult way, Maverickz's posted a good solution.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    6

    Question Re: Resize the controls on windows form when the form is maximed or in normal state

    dday9,
    I am re-trying the Dock and anchor properties as I noticed that I can only set one of these 2 properties on the control and not both the properties on the same control.

    If this doesn't work,I may have go use how Maverickz mentioned.

    Once I maximize the form,how do I refer to the maximized form to get the width of the maximized current form.I referred to the actual form as
    actualfrmW = Screen.PrimaryScreen.Bounds.Width

    The " me.width" gives the original form's width (form in normal state)
    I need to save the original normal form width and then capture the maximized form width.

  7. #7
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: Resize the controls on windows form when the form is maximed or in normal state

    It might help if you could post a screen shot of your interface maximized with all the controls where you want them and list specifically all of the controls used. Maybe then we can see how to best organize them into panels that can be properly docked to produce the effect that you want.

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    6

    Question Re: Resize the controls on windows form when the form is maximed or in normal state

    Quote Originally Posted by Maverickz View Post
    It might help if you could post a screen shot of your interface maximized with all the controls where you want them and list specifically all of the controls used. Maybe then we can see how to best organize them into panels that can be properly docked to produce the effect that you want.
    I will put a sample screen as I can't post the actual screen due to security reasons on the network that I work.

    Between,as I maximize the form,Please suggest as to how I can refer to the maximized form to get the width of the maximized current form.I referred to the actual form as
    actualfrmW = Screen.PrimaryScreen.Bounds.Width

    The " me.width" gives the original form's width (form in normal state)

  9. #9
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: Resize the controls on windows form when the form is maximed or in normal state

    Quote Originally Posted by Neena20 View Post
    The " me.width" gives the original form's width (form in normal state)
    It shouldn't.
    If you start a brand new Windows Forms project, add a RichTextBox (named RTB1), and add a button (named B1) to the form (it does not matter where you put them).
    Then paste the code I provided above into the Form code and run the project, everything will be aligned exactly as I described regardless of window state. Maximized, Minimized, and even manually resized, it will all work.

    If for some reason it is still not working for you, maybe use a panel with full dock and put all of the controls in it and then use the panel.width property as your starting point.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width