Answered by:
All the text isn't displayed at a label created with code

Question
-
I am having a problem because when the length of the text is > 10 only 10 letters are displayed. I use this code to create a label with the text HarryPotter but when I debug it only HarryPotte is displayed
Dim label As New Label label.Name = ("dadad") label.Text = "HarryPotter" label.Location = New Point(50, 50) Dim fnt As Font fnt = label.Font label.Font = New Font(fnt.Name, 12, FontStyle.Bold) Me.Controls.Add(label)
Albania
Saturday, July 12, 2014 7:30 PM
Answers
-
Hi,
Try adding the line like in the code below to set the Label`s AutoSize to True. That should fix the problem i believe. :)
Dim label As New Label label.AutoSize = True 'Add this line label.Name = ("dadad") label.Text = "HarryPotter" label.Location = New Point(50, 50) label.Font = New Font(label.Font.Name, 12, FontStyle.Bold) Me.Controls.Add(label)
EDIT : @ Tom, You beat me this time. 8)
If you say it can`t be done then i`ll try it
- Proposed as answer by Mr. Monkeyboy Saturday, July 12, 2014 8:04 PM
- Edited by IronRazerz Saturday, July 12, 2014 8:08 PM
- Marked as answer by Franklin ChenMicrosoft employee Monday, July 21, 2014 10:02 AM
Saturday, July 12, 2014 8:03 PM -
You could cut your code down a couple of lines also.
I would recommend that you not try to use the same name as a class name for a control as the code can become confusing at some point. Dim label As New Label maybe use Dim labelx As New Label or something else.
Dim label As New Label label.Name = ("dadad") label.Text = "HarryPotter" label.Location = New Point(50, 50) label.Font = New Font(label.Font.Name, 12, FontStyle.Bold) label.AutoSize = True Me.Controls.Add(label)
La vida loca
- Edited by Mr. Monkeyboy Saturday, July 12, 2014 8:09 PM
- Proposed as answer by IronRazerz Saturday, July 12, 2014 8:10 PM
- Marked as answer by Franklin ChenMicrosoft employee Monday, July 21, 2014 10:02 AM
Saturday, July 12, 2014 8:07 PM
All replies
-
Try adding this:
label.AutoSize = True
If you don't want auto size then there are other ways.
- Proposed as answer by Mr. Monkeyboy Saturday, July 12, 2014 8:04 PM
Saturday, July 12, 2014 8:02 PM -
Hi,
Try adding the line like in the code below to set the Label`s AutoSize to True. That should fix the problem i believe. :)
Dim label As New Label label.AutoSize = True 'Add this line label.Name = ("dadad") label.Text = "HarryPotter" label.Location = New Point(50, 50) label.Font = New Font(label.Font.Name, 12, FontStyle.Bold) Me.Controls.Add(label)
EDIT : @ Tom, You beat me this time. 8)
If you say it can`t be done then i`ll try it
- Proposed as answer by Mr. Monkeyboy Saturday, July 12, 2014 8:04 PM
- Edited by IronRazerz Saturday, July 12, 2014 8:08 PM
- Marked as answer by Franklin ChenMicrosoft employee Monday, July 21, 2014 10:02 AM
Saturday, July 12, 2014 8:03 PM -
You could cut your code down a couple of lines also.
I would recommend that you not try to use the same name as a class name for a control as the code can become confusing at some point. Dim label As New Label maybe use Dim labelx As New Label or something else.
Dim label As New Label label.Name = ("dadad") label.Text = "HarryPotter" label.Location = New Point(50, 50) label.Font = New Font(label.Font.Name, 12, FontStyle.Bold) label.AutoSize = True Me.Controls.Add(label)
La vida loca
- Edited by Mr. Monkeyboy Saturday, July 12, 2014 8:09 PM
- Proposed as answer by IronRazerz Saturday, July 12, 2014 8:10 PM
- Marked as answer by Franklin ChenMicrosoft employee Monday, July 21, 2014 10:02 AM
Saturday, July 12, 2014 8:07 PM -
Hi,
Try adding the line like in the code below to set the Label`s AutoSize to True. That should fix the problem i believe. :)
Dim label As New Label label.AutoSize = True 'Add this line label.Name = ("dadad") label.Text = "HarryPotter" label.Location = New Point(50, 50) label.Font = New Font(label.Font.Name, 12, FontStyle.Bold) Me.Controls.Add(label)
EDIT : @ Tom, You beat me this time. 8)
If you say it can`t be done then i`ll try it
I figured you would go for this one:
Dim g As Graphics = Graphics.FromHwnd(IntPtr.Zero) label.Width = 1.09 * g.MeasureString(label.Text, label.Font).Width
Saturday, July 12, 2014 8:36 PM -
Or there is this fancy one:
Saturday, July 12, 2014 8:52 PM -
I figured you would go for this one:
Dim g As Graphics = Graphics.FromHwnd(IntPtr.Zero) label.Width = 1.09 * g.MeasureString(label.Text, label.Font).Width
You know i am more of a Using kind of guy. I like to use it and then loose it. 8)
Using g As Graphics = Graphics.FromHwnd(label.Handle) label.Width = CInt(1.09 * g.MeasureString(label.Text, label.Font).Width) End Using
If you say it can`t be done then i`ll try it
Saturday, July 12, 2014 9:13 PM