Solved

How to determine size of Label?

Posted on 2002-06-26
13
206 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

929 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

15 Experts available now in Live!

Get 1:1 Help Now