Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

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.

What I have tried:

I learned about the operator "<>" - however I have failed to apply it in this context.
Posted
Updated 11-Dec-17 11:59am
v2
Comments
Ralf Meier 12-Dec-17 5:27am    
What do you want to know ?
I suppose the resizing is not your real question - you want to know how to handle the controls which will be "outside the form" after resizing to "small" - right ?
Which Behaviour do you want to have ?

1 solution

Wow.

1) Don't use Size to find the size of a form. Use the Width and Height properties.

2) Don't create a New Size just to throw it away after a comparison.

3) You're not changing the Width at all, just the Height, so don't set it again with a new Size.

4) You don't need the "Me" on everything. The code is in the form class, so Me is already implied with all of it. Just Get/Set the properties and call the methods without breaking your M and E keys on the keyboard.

5) Hopefully this code is only called when the button is clicked?!

Simplify this into:
VB.NET
If Height = 274 Then
    Height = 458
Else
    Height = 274
End If
 
Share this answer
 
Comments
Ralf Meier 12-Dec-17 5:24am    
@Dave:
For me there is no difference in using Size or Width and/or Height seperately.
If you use one or the other it causes a SizeChange ...
But of cause : your code-snippet is much smaller and better readable than the code from the OP ...
Dave Kreskowiak 12-Dec-17 8:27am    
You're correct. There isn't any difference. It just makes the code cheaper and easier to write and support.

As usual, it does fire a Size_Changed event, but if the code isn't in that event handler, it's not a problem.

Truthfully, I wouldn't write the functionality he's describing like this, but it's simple and works for a beginner.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900