| | |
Determining Width of Text in a Control
Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Aug 2007
Posts: 17
Reputation:
Solved Threads: 0
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
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.
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.
•
•
Join Date: Dec 2002
Posts: 461
Reputation:
Solved Threads: 56
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.
If you want to limit your width then add 50(or whatever) to the measure string.
VB.NET Syntax (Toggle Plain Text)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim g As Graphics = Label1.CreateGraphics Dim s As SizeF s = g.MeasureString("This is a label", Label1.Font) Label1.Width = s.Width Label1.Height = s.Height Label1.Text = "This is a label" End Sub
VB.NET Syntax (Toggle Plain Text)
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.
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
![]() |
Similar Threads
- Determining correct size of JDesktopPane (Java)
- Guide: How Google Works (Search Engine Optimization)
- IE 6 - No response from javascript:openWindow (Web Browsers)
- Red Hat 9 and VMWare 4 networking (*nix Software)
- need help to debugg this code please! (C++)
- can somebody pls. help me out with my HJT log.. (Viruses, Spyware and other Nasties)
- Disappearing Drive in Win XP (Storage)
- Toshiba compatible hard drive... (Storage)
- c++ compiling problem (C++)
Other Threads in the VB.NET Forum
- Previous Thread: Data retrieving using ms-access
- Next Thread: Program running in the background
| Thread Tools | Search this Thread |
"crystal .net .net2005 .net2008 2008 access add application array assignment basic beginner box browser button buttons click code combo convert cpu cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationthesis dissertationtopic dosconsolevb.net editvb.net exists fade filter firewall forms html image images input isnumericfuntioncall listview math mobile module mssqlbackend mysql navigate number opacity open panel picturebox picturebox2 port print printing printpreview record regex reports" reuse right-to-left savedialog serial settings shutdown socket sqldatbase sqlserver storedprocedure string temp temperature textbox timer timespan transparency txttoxmlconverter useraccounts usercontol usercontrol vb vb.net vb.nettoolboxvisualbasic2008sidebar vbnet vista visual visualbasic.net visualstudio.net web winforms wpf wrapingcode xml year





