-
Dec 11th, 2017, 08:10 PM
#1
Thread Starter
Member
[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.
-
Dec 11th, 2017, 08:28 PM
#2
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:
Private Const DEFAULT_HEIGHT As Integer = 300
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ClientSize.Height = DEFAULT_HEIGHT Then
Height += 100
Else
Height = DEFAULT_HEIGHT
End If
End Sub
or:
vb.net Code:
Private defaultHeight As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
defaultHeight = Height
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Height = defaultHeight Then
Height += 100
Else
Height = defaultHeight
End If
End Sub
Notice that, in both cases, it's the Height property being set. The difference is whether you test ClientSize.Height or Height.
-
Dec 11th, 2017, 08:41 PM
#3
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|