944,052 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 11109
  • VB.NET RSS
Aug 28th, 2007
0

Determining Width of Text in a Control

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JaedenRuiner is offline Offline
17 posts
since Aug 2007
Aug 28th, 2007
0

Re: Determining Width of Text in a Control

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.
Reputation Points: 25
Solved Threads: 18
Practically a Master Poster
binoj_daniel is offline Offline
645 posts
since Dec 2006
Aug 29th, 2007
0

Re: Determining Width of Text in a Control

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.
VB.NET Syntax (Toggle Plain Text)
  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.
VB.NET Syntax (Toggle Plain Text)
  1. s = g.MeasureString("This is a label", Label1.Font, 50)
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Data retrieving using ms-access
Next Thread in VB.NET Forum Timeline: Program running in the background





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC