?
Solved

How to determine size of Label?

Posted on 2002-06-26
13
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 100 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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

743 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