Hey,
I tried to change the size of a label from 2 text boxes, but doesn't work. Here is my code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        y = TextBox2.Text
        x = TextBox3.Text
        Form2.Label1.Size = New Size(y, x)
    End Sub

textbox2 is width and textbox3 is height.
It doesn't change and I don't know why. Can someone help me with this?

Dani AI

Generated

Good catch from about AutoSize. Another common gotcha here is VB’s default form instance. If you call Form2.Label1... you are modifying the hidden default instance, but if you opened a different instance with New Form2() you will not see the change. Work against the same instance you show, and validate the textbox input before resizing.

Example pattern:

' Form2
Public Sub ApplyDimensions(w As Integer, h As Integer)
    Label1.AutoSize = False
    Label1.Size = New Size(w, h)
    Label1.BorderStyle = BorderStyle.FixedSingle   ' optional: helps you see the bounds
End Sub
' Form1
Private _form2 As Form2

Private Sub Button2_Click(...) Handles Button2.Click
    Dim w As Integer, h As Integer
    If Not Integer.TryParse(TextBox2.Text, w) OrElse Not Integer.TryParse(TextBox3.Text, h) Then
        MessageBox.Show("Enter numeric width and height.")
        Return
    End If

    If _form2 Is Nothing OrElse _form2.IsDisposed Then _form2 = New Form2()
    _form2.Show()
    _form2.ApplyDimensions(w, h)
End Sub

For your follow-up about changing the text size (not the label box size), set a new Font based on the current family/style. Clamp the value to something sensible to avoid tiny or huge fonts:

Private Sub ApplyFontSize(szText As String)
    Dim pt As Single
    If Single.TryParse(szText, pt) Then
        pt = Math.Max(6.0F, Math.Min(72.0F, pt))
        Label1.Font = New Font(Label1.Font.FontFamily, pt, Label1.Font.Style, GraphicsUnit.Point)
    End If
End Sub

Notes:

  • If the label is Docked, size changes can be overridden.
  • This thread’s code is Windows Forms. If you actually meant ASP.NET, Label does not have a Size property; you would set CSS width/height and font-size instead.

Recommended Answers

All 7 Replies

Turn "AutoSize" off on the Label1.

Turn "AutoSize" off on the Label1.

I tried and still not working, any other ideas?

Hey,
I tried to change the size of a label from 2 text boxes, but doesn't work. Here is my code:
VB.NET Syntax (Toggle Plain Text)

1.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
2.
y = TextBox2.Text
3.
x = TextBox3.Text
4.
Form2.Label1.Size = New Size(y, x)
5.
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click y = TextBox2.Text x = TextBox3.Text Form2.Label1.Size = New Size(y, x) End Sub
textbox2 is width and textbox3 is height.
It doesn't change and I don't know why. Can someone help me with this?

Forgot to add:

Dim y As Integer
    Dim x As Integer
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        y = TextBox2.Text
        x = TextBox3.Text
        Form2.Label1.Size = New Size(y, x)
    End Sub

Hi,

You should do it like this:

In form1 you need a button to show your form2 like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fr As New Form2
        fr.Show()
    End Sub

In form2 you can do it like this:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim y As Integer
        y = Form1.TextBox1.Text
        Dim x As Integer
        x = Form1.TextBox2.Text

        Label1.Size = New Size(y, x)
    End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
        Dim W As Integer = TextBox1.Text
        Dim H As Integer = TextBox2.Text
        Label1.Size = New Point(W, H)
    End Sub

I use it and it work Turn off Autosize And to test it work just change BackColor
to other color
the font size didn't change as it label size you must code it

Dim fnt As Font
fnt = TextBox1.Font
Label1.Font = New Font(fnt.Name, 12, FontStyle.Bold)

VB.NET Syntax (Toggle Plain Text)

1.
Private Sub Button1_Click(ByVal sender As System.Object, _
2.
ByVal e As System.EventArgs) Handles Button1.Click
3.
Dim W As Integer = TextBox1.Text
4.
Dim H As Integer = TextBox2.Text
5.
Label1.Size = New Point(W, H)
6.
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim W As Integer = TextBox1.Text Dim H As Integer = TextBox2.Text Label1.Size = New Point(W, H) End Sub
I use it and it work Turn off Autosize And to test it work just change BackColor
to other color
the font size didn't change as it label size you must code it
VB.NET Syntax (Toggle Plain Text)

1.
Dim fnt As Font
2.
fnt = TextBox1.Font
3.
Label1.Font = New Font(fnt.Name, 12, FontStyle.Bold)

Well, them how can I change the size of text using those text boxes?

Private Sub Button1_Click(ByVal sender As System.Object, _
Dim TxtSz As Integer = Textbox1.text
Dim fnt As Font
fnt = Label1.Font
Label1.Font = New Font(fnt.Name, TxtSz, FontStyle.Regular)
End Sub

This Work For Me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.