Question Dynamically created label not fully displaying

tmontney

Member
Joined
Nov 2, 2015
Messages
8
Programming Experience
1-3
VB.NET:
Dim noDrivesFoundLBL As New Label
noDrivesFoundLBL.Text = "No optical disc drives found. Please ensure they are connected properly, and restart the application."
noDrivesFoundLBL.Location = New Point(9, 10)
Me.Controls.Add(noDrivesFoundLBL)
Me.Size = New Size(360, 114)

When I run it, all I see is "No optical disc". Not sure why it's being cut off. I added in Location thinking that might help, but it didn't.
 
Last edited:
AutoSize property, for some reason, is set to False. I decided to size the window close to the size of the text. Then I realized, that was far too small for the text. And that led me to realize the size of the label was too small to fit that text.

tl;dr Set AutoSize property to True.

Fixed code:

VB.NET:
            Dim noDrivesFoundLBL As New Label
            noDrivesFoundLBL.Text = "No optical disc drives found. Please ensure they are connected properly, and restart the application."
            noDrivesFoundLBL.AutoSize = True
            MyBase.Controls.Add(noDrivesFoundLBL)
            Me.AutoSize = True
            Me.Size = New Size(Me.Size.Width, 75)
 
AutoSize property, for some reason, is set to False.

The reason is that False is the default value for the Boolean type and AutoSize is a Boolean property. When you add a Label to a form in the designer, the designer itself sets AutoSize to True explicitly. If you create one yourself then you have to do that.
 
Back
Top