-
Sep 1st, 2006, 07:09 AM
#1
Thread Starter
Lively Member
Resize a Label's height to fit text
Using VB.NET 2005, Compact Framework 2 for Windows Mobile 5 PDA
I have a Label on a form that at runtime will have its Text property set to a string from a database. This string is variable in length upto 255 characters. The width of the Label is fixed, but is there a way after the text has been added to resize the Labels height, so it fits the text.
Thanks
-
Sep 1st, 2006, 07:37 AM
#2
Re: Resize a Label's height to fit text
If you want the new text to nicely fit in the label, you'll have to calculate the area needed to hold the text (based on the text length, font size and in your particular case, the fixed width of the label. In other words, you already have the area's width which is the label's width, you just need to calculate the height)
VB Code:
Dim w As Integer = Label1.Width
Dim h As Integer = CalculateHeight(strText, Label1.Font.Size) 'How you calculate this
'is up to you but it's just like how you calculate the print area in a PrintPage event
Dim sz As New Size(w, h)
Label1.Size = sz
Label1.Text = strText
-
Sep 1st, 2006, 08:10 AM
#3
Re: Resize a Label's height to fit text
VB Code:
Private Function CalculateHeight() As Single
Dim g As Graphics = Label1.CreateGraphics
Return g.MeasureString(Label1.Text, Label1.Font).Width
End Function
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Sep 11th, 2006, 08:59 AM
#4
Thread Starter
Lively Member
Re: Resize a Label's height to fit text
Thanks.
I am trying to do this though in the CF, and I don;t seem to have the CeateGraphics function. Is there an alternative for the CF?
Thanks
-
Sep 11th, 2006, 09:05 AM
#5
Re: Resize a Label's height to fit text
try using Me.CreateGraphics and use the form's CreateGraphics method instead of the labels (Since the labels CreateGraphics method is not available in the CF)
-
Sep 11th, 2006, 09:13 AM
#6
Thread Starter
Lively Member
Re: Resize a Label's height to fit text
I don't understand. If it is the label I want o calculate the height of, how will the CreateGraphics of the form work? And what would I set the text property to?
If possible could you give an example?
Thanks
-
Sep 11th, 2006, 10:07 AM
#7
Re: Resize a Label's height to fit text
Because you need a valid graphics object to call the method MeasureString... The MeasureString method accepts a string, and a font, to determine how tall the string will be, which you can use to size your label.
make sure you pass the labels text, and the labels font when you call it.
-
Dec 7th, 2019, 04:02 PM
#8
New Member
Re: Resize a Label's height to fit text
Label1.AutoSize = False
Label1.Width=SomeWidth
Label1.Caption = "Some text that will appear as multiline text"
Label1.AutoSize = True
Damn, did I hunt high and low for this one.
Outlook VBA 7.1.1091
-
Dec 8th, 2019, 11:38 AM
#9
Re: Resize a Label's height to fit text
Hi James,
I'd try fitting the text in this way:
Set your Label's text height to Pixels and to your preferred size as x pixels.
In the following code, make txtHght about (say) half your text height x.
Code:
Dim txtHght as Int32 = (½ text height)
Dim txtSize as Int32 = Label1.Height
Dim flg as Boonean = Frue
While flg
Using gr As Graphics = ctrl.CreateGraphics
txtSize = CInt(gr.MeasureString(ctrl.Text, ctrl.Font).Height)
End Using
If txtSize > Label1.Height Then
Label1.Height += txtHght
Else
flg = False
End If
End While
Poppa.
Oh !
I've just noticed this was first posted Sep 1st, 2006 !!
I guess that mrjjacobs, being a new member, didn't notice that.
(I wondered how come there were 8,450 Views)
Pop.
Last edited by Poppa Mintin; Dec 8th, 2019 at 06:40 PM.
Reason: Oh !
Along with the sunshine there has to be a little rain sometime.
-
Dec 10th, 2019, 05:10 AM
#10
Member
Re: Resize a Label's height to fit text
label.AutoSize = False
label.Width = CInt("pass your text here")
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|