Results 1 to 3 of 3

Thread: [RESOLVED] Resizing form during runtime?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    51

    Resolved [RESOLVED] Resizing form during runtime?

    Hello,

    I have a small issue. I have a button on my form. On Button_Click event, the form must resize itself to a certain height, this is the code I currently have:

    Code:
    If Me.Size = New System.Drawing.Size(478, 274) Then ' This is the initial form size.
        Me.Size = New System.Drawing.Size(478, 458) ' Form size after pressing button.
    Else
        Me.Size = New System.Drawing.Size(478, 274) ' Initial form size.
    End If
    This is sloppy for so many reasons - it's relying on the fact that the form is in fact 478, 458 - the forms' size will alter depending on the theme that the user is using (as I leaned today).
    I cannot simply put "Me.Size = New System.Drawing.Size(478, 458) to increase the height of the form either, as there are components at the bottom of my form which will be cut out (due to differing form sized according to theme).

    What do I do?
    Thanks a lot for your help, I've been struggling with this for a while now.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Resizing form during runtime?

    There are a couple of options that come to mind.

    1. Use the ClientSize property to perform the check. The ClientSize property ignores the form border and title bar and just measures the client area of the form. The size of that client area should be unaffected by themes.

    2. Check the current size at startup (I think that the Load event handler should be appropriate but you may need to use Shown) and consider that the default instead of hard-coding it.

    Either way, don't use the Size property like that. You only care about the vertical direction so only deal with the vertical direction, e.g.
    vb.net Code:
    1. Private Const DEFAULT_HEIGHT As Integer = 300
    2.  
    3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.     If ClientSize.Height = DEFAULT_HEIGHT Then
    5.         Height += 100
    6.     Else
    7.         Height = DEFAULT_HEIGHT
    8.     End If
    9. End Sub
    or:
    vb.net Code:
    1. Private defaultHeight As Integer
    2.  
    3. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.     defaultHeight = Height
    5. End Sub
    6.  
    7. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    8.     If Height = defaultHeight Then
    9.         Height += 100
    10.     Else
    11.         Height = defaultHeight
    12.     End If
    13. End Sub

    Notice that, in both cases, it's the Height property being set. The difference is whether you test ClientSize.Height or Height.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    51

    Re: Resizing form during runtime?

    Wow, clever (to me it is anyway).

    Hopefully I'll be able to think of the same sort of solutions one day, I'll try to implement the same sort of thing with other aspects of my project.

    Thanks for your help!

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