Determining Width of Text in a Control

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 17
Reputation: JaedenRuiner is an unknown quantity at this point 
Solved Threads: 0
JaedenRuiner JaedenRuiner is offline Offline
Newbie Poster

Determining Width of Text in a Control

 
0
  #1
Aug 28th, 2007
Is there a way with the VB interface to either:

GetTextMetrics (ie: take Label, and I want to get the Width of the text that would be outputted by that control.)

Determine if the Text is "larger" than the control.

Basically, I have a form, with multiple "display" labels, that are affected by different colors for the parameters of my application's color coding. HOwever, the text that is displayed in those labes varies based upon the values of a combo box. I populate the combo-box real time, and then when I select Each one in the box, it changes the Text of the Label, and updates the foreground and background colors based upon that current entries settings.

HOwever, sometimes, the text is larger than my initial label size (82x20). Now I can set the AutoSize property, but that does something I don't like: It doesn't obey Form Anchors.

I have 5 labels, each one could change. All the form Anchors are set, so when i resize one label, I can expand the form based upon the width change, and all the other controls follow suit. however, if the other 4 labels are AutoSize=True, they won't resize unless they need to.
WHat I want:

I want an event, (or a precheck function) that tells me the width of a Label when I've Set it's Text propery (or before I set it), which allows me to resize the form to expand the Label dimensions appropriate to fit the text.
Thanks
Jaeden "Sifo Dyas" al'Raec Ruiner
Last edited by JaedenRuiner; Aug 28th, 2007 at 2:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 645
Reputation: binoj_daniel is an unknown quantity at this point 
Solved Threads: 17
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: Determining Width of Text in a Control

 
0
  #2
Aug 28th, 2007
First you can find the length of the text inside the label control using the len function. And then calculate the width of the label needed.
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Determining Width of Text in a Control

 
0
  #3
Aug 29th, 2007
You can use the following to find the width of the string going into the label control. I used the return values to set the size of the label.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Dim g As Graphics = Label1.CreateGraphics
  3. Dim s As SizeF
  4. s = g.MeasureString("This is a label", Label1.Font)
  5. Label1.Width = s.Width
  6. Label1.Height = s.Height
  7. Label1.Text = "This is a label"
  8. End Sub
If you want to limit your width then add 50(or whatever) to the measure string.
  1. s = g.MeasureString("This is a label", Label1.Font, 50)
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC