Solved

How to determine size of Label?

Posted on 2002-06-26
13
205 Views
Last Modified: 2010-05-02
I have to place Labels and TextBoxes on a form in run time. They are different sizes, depending on the text that they will contain. I would like to center them but this depends on their size. Since it’s a proportional font I can’ just count the characters. How can I know what size the text will be?


0
Comment
Question by:eeevbeee
13 Comments
 
LVL 5

Expert Comment

by:rpai
ID: 7110766
len(Text1.Text) should give you the length of the text.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7110777
You can use this to determine the width / height of the text to set the label accordingly.

    With Label1
        .Caption = Text1.Text
        .Width = Me.TextWidth(Text1.Text)
        .Height = Me.TextHeight(Text1.Text)
    End With

This will set the height and width to match the text in the textbox (or you can use a variable or whatever). You may need to allow some extra height/width to ensure that it displays perfectly but in principle this is the way to go.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7110779
Me.TextWidth(Label1.Caption)

Anthony
0
 
LVL 6

Expert Comment

by:marconovaro
ID: 7110781

You have to use the DrawText API: from MSDN

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_0odw.asp

DrawText
The DrawText function draws formatted text in the specified rectangle. It formats the text according to the specified method (expanding tabs, justifying characters, breaking lines, and so forth).

with the parameter uFormat equal to:

DT_CALCRECT

From MSDN:
Determines the width and height of the rectangle. If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by the lpRect parameter and extends the base of the rectangle to bound the last line of text. If the largest word is wider than the rectangle, the width is expanded. If the text is less than the width of the rectangle, the width is reduced. If there is only one line of text, DrawText modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text but does not draw the text.

Hope this helps you. Let me know if you need additional information
Marco.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7110782
Tim,

Did not mean to duplicate your answer, in any case your answer is more complete.

Anthony
0
 

Author Comment

by:eeevbeee
ID: 7110827
acperkins
whatis me.textwidth?
is this a function? where do i find it?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Expert Comment

by:deighton
ID: 7110863
me.textwidth("fred")

is just a shorthand way of referring to your current form

you can use any property of the form this way, e.g me.width, Unload Me etc..

TextWidth is a propery of a form
0
 
LVL 1

Expert Comment

by:M.E.Pierce
ID: 7111006
when you load your controls,

set visible = false
autosize = true
caption = "The Caption"

read the width of the control,
then set it's position as required to center it.

for the text boxes, if you are planning to size them
to the text you set when you load them, use a label
as a temporary container for the text and add 325*
to the width to get the width of the text box
with the same text in it.

*don't quote me on this number, I'm don't have access to
VB right now, but you get the idea.


0
 
LVL 3

Expert Comment

by:PNJ
ID: 7111216
Why don't you just do:

   lblData.Width = Me.Width
   lblData.AutoSize = False ' At Design time if you like
   lblData.Alignment = 2 ' Center (ditto)
   lblData = "This is centred on the form"

Then the label will be the width of the form and therefore the text centred in it.
0
 

Author Comment

by:eeevbeee
ID: 7111532
I don't have a property .TextWidth.

I am using vb.net, could this be the problem? would this property be missing from .net?
0
 
LVL 1

Expert Comment

by:eeevans
ID: 7111548
PNJ,
He is probably putting the textboxes next to the labels.

eeevbeee, it would probably help if you would explain exactly how you are trying to lay out your controls.

-  One way would be to have rows of label and textbox pairs starting some distance from the top of the form and which are centered together in the form with some certain distance between them.

-  Another would be to do the same except that all of the labels would align on left edges and then the text boxes would align on left edges some certain distance from the right edge of the widest label.

Obviously you can choose any combination of these align on right/left edges or whatever.  If you would specify it for us it would help us give you a more complete solution.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7111663
>>I am using vb.net, could this be the problem? would this property be missing from .net? <<
Since this is a VB.NET question, I suggest you post this question in the .NET topic area (http://www.experts-exchange.com/dot_net/)

Anthony
0
 
LVL 2

Accepted Solution

by:
sun365 earned 25 total points
ID: 7113310
Hide a Label with AutoSize = True and use it's width property to set the width property on the Label.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now