Resize problem!

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
So I am playing around with the resize event. My form is 500 wide, my button.left is at 100. If I use:
VB.NET:
Dim int As Double
int = button1.Left / Me.Width ' this = 0.2
'resize event
'Button1.Left = (int * Me.Width)
TextBox1.Text = (int * Me.Width).ToString
The number in the texbox is correct - proportion wise, but it won't set the button1.Left to this number. I'm sure this is because int is a Double where .Left is an integer? How do you fix this?
 
Last edited:
You should be integer division (\) instead of standard division (/). That way the result of the operation will be an Integer and you can declare 'int' as type Integer so you can assign it to the Left property.

Alternatively, you could use CInt to convert the Double to an Integer.
 
Neither of these are working, both send the button.left to 0 on form load. I have tried several conversions, just no luck so far. I even tried to Shadow the Left property to type double. :(
 
Here is what I have tried:
VB.NET:
' class declaration
Dim int As Integer 
' form load tell int what number to hold
int = btn1.Left \ Me.Width
' in the form resize event...
btn1.Left = int * Me.Width
' this sets the btn1.left to 0 and it will not move during resize
also tried...
VB.NET:
Dim int As Double
'... form load
int = btn1.Left / Me.Width
'...resize
btn1.Left = Cint(int * Me.Width)
' again btn1.Left is at 0, and will not move
 
I'm guessing that it might have something with dividing the button's left property by the window width always coming up with a decimal less than 1. Converting to an integer will give you 0.
 
Ok, but I am confused. My math says btn1.Left(100) / Me.Width(500) = 0.2 .
Then during the resize I say btn1.Left = (int)0.2 * the new Me.Width(i.e. 400) = 80 -> this is where btn1.Left should be. I'll keep at it...
 
As demausdauth says, dividing the Button's Left property by the form's Width property is always going to produce a result less than 1 so integer division will produce the result 0. If you read my first post I also said that you can use CInt to convert a Double to an Integer. With that in mind, divide the Button's Left property by form's Width property to get a Double. Resize the form and then multiply that Double by the form's Width property to get another Double. Use CInt to convert that Double to an Integer and assign that value to the Button's Left property.
VB.NET:
Dim proportion As Double = btn1.Left / Me.Width

Me.Width *= 2

btn1.Left = CInt(proportion * Me.Width)
Depending on your form layout you might also be able to just put the Button in a TableLayoutPanel and have it move the Button automatically.
 
Back
Top