Solved

How to determine size of Label?

Posted on 2002-06-26
13
207 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Basic Excel Formatting error 4 102
passing parameter in sql procedure 9 61
Microsoft Access combo box help 2 44
MsgBox 4 59
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

775 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