Click here to Skip to main content
14,383,503 members
Rate this:
Please Sign up or sign in to vote.
See more:
how do i change label size in vb.net to new size (available in a textbox)

eg : if textbox contains number =50 i want the label width =50

What I have tried:

Label6.Size = New System.Drawing.Size(Val(TextBox1.Text), 90)
Posted
Updated 5-Nov-19 15:10pm
Comments
Ralf Meier 8-Apr-18 14:48pm
   
Basicly Wendelius gave you an alternative Solution.
Independant from that should your code work too if Textbox1 contains a value which could be converted to a number by Val.
When do you assign the new Size-Value to the Label ? Which Event from which Control causes this ...?
Rate this:
Please Sign up or sign in to vote.

Solution 1

Instead of modifying the Size property try to set the Width property of the label control. In other words:
Label6.Width = Val(TextBox1.Text)

Also ensure that Label.AutoSize Property (System.Windows.Forms)[^] is not set
   
v2
Comments
Maciej Los 7-Apr-18 9:16am
   
5ed!
Wendelius 10-Apr-18 0:13am
   
Thanks!
Rate this:
Please Sign up or sign in to vote.

Solution 2

Make sure to set AutoSize property of the label to False; Otherwise, you cannot set any custom size.

label6.AutoSize = False
label6.Width = CInt(TextBox1.Text)
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 4

Just change the minimum size of your label to whatever you desire.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

This worked for me
labelSesion.Width = labelSesion.Text.Length * 6
   
Comments
Dave Kreskowiak 27-Oct-19 14:11pm
   
There's a ton of problems with this, namely font size, varying character width, whitespace content of the string, ...

There is far batter ways of doing this without taking uneducated guesses at string length.

And it doesn't even answer the OP's question.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100