Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Height of text

Posted on 2002-06-27
8
Medium Priority
?
856 Views
Last Modified: 2006-11-17
Since microsoft desided to remove the autosize property for the labels in vb.net (its still there but only on a horizontal sence of the word), I am trying to determin the required height of a text (with the current font, fontsize etc). I have tried everything (.PreferredHeight and so on) but have faild ,looking for ideas. What I am trying to do is to create a label that will autosize vertical (down) when a text is added to it that cannot fit into its widht (wordwrap).

PLZ PLZ PLZ ideas anything that might pop into your heads, give it to me... I will post the solution if I solve it my self.

Sincerly
Claes
0
Comment
Question by:clylv
[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
  • 4
  • 4
8 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 7113052
So you want the text to always be on one line?

CJ
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7113084
Anyway, no matter how you can retrieve the height of the font by using code similar to the following:

System.Drawing.Font x = new Font("Verdana", 3);
x.GetHeight()

You can use these functions to determine how big your font needs to be.

CJ
0
 

Author Comment

by:clylv
ID: 7113154
Thanx CJ S,
That goes a way but what I need to know is how many lines in a multiline label I need to make room for.

EX

Label1.wordwrap = true
Label1.width = 50
label1.Text = "lllllllllllllllllllllllllllllllllllllllllll"

Now the text will require more space thatn the 50 pixels that has been assigned to it.
The text will be displayd something like this:
lllllllllll and the rest will be hidden.
What I want to be able to do is to calculate the number of needed rows so that I can adjust the height of the label to accomodate the whole text like this:
lllllllllll
lllllllllll
lllllllllll
lllllllllll
lllllll
The autosize property will not let me do this, so I tried this formula

Dim Label2 as Label
Label2.AutoSize = true
Label2.Text = Label1.Text
dim rows as integer
rows = label1.width / label2.width
dim iHeight as integer
iHeight = rows * font.GetHeight
Label1.Height  = iHeight

And it works sortof,

The problem is that like most wordprocessors the Label changes the spacings in a text when wordwrap adds a new line. And then sometimes you end up with a few char or a word below the Label.... irritating lik hel.... and I can't find a way around it.

/claes
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 22

Accepted Solution

by:
CJ_S earned 600 total points
ID: 7113182
Just add the linespacing

iHeight = rows * (Font.GetHeight + Font.FontFamily.GetLineSpacing(FontStyle.Regular))
0
 

Author Comment

by:clylv
ID: 7113207
AHHHHHHHHHH!! CJ S YOU HAVE NO IDEA HOW HAPPY YOU JUST MADE ME!!!

Been trying to solve this for 3 (three!) weeks know and you just naild it for me my friend! works like a charm, a grade A and all the points are yours!

Once again THANKYOU!!!

/Claes
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7113214
Glad to help ;-)

CJ
0
 

Author Comment

by:clylv
ID: 7113557
mayby I was a bit premature there, or mayby im just stupid, it looked as if it worked like a charm and mayby it will but the linespaceing returns a value of 2355, is this in some strange unit do you know?
It looked like it worked fine but in reality the label is 10 as high as the form?

/Claes
0
 

Author Comment

by:clylv
ID: 7113572
mayby I was a bit premature there, or mayby im just stupid, it looked as if it worked like a charm and mayby it will but the linespaceing returns a value of 2355, is this in some strange unit do you know?
It looked like it worked fine but in reality the label is 10 as high as the form?

/Claes
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

609 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