• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

How to determine size of Label?

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
eeevbeee
Asked:
eeevbeee
1 Solution
 
rpaiCommented:
len(Text1.Text) should give you the length of the text.
0
 
TimCotteeCommented:
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
 
Anthony PerkinsCommented:
Me.TextWidth(Label1.Caption)

Anthony
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
marconovaroCommented:

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
 
Anthony PerkinsCommented:
Tim,

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

Anthony
0
 
eeevbeeeAuthor Commented:
acperkins
whatis me.textwidth?
is this a function? where do i find it?
0
 
deightonCommented:
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
 
M.E.PierceCommented:
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
 
PNJCommented:
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
 
eeevbeeeAuthor Commented:
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
 
eeevansCommented:
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
 
Anthony PerkinsCommented:
>>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
 
sun365Commented:
Hide a Label with AutoSize = True and use it's width property to set the width property on the Label.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now