clylv
asked on
Height of text
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
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
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
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
ASKER
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 = "lllllllllllllllllllllllll llllllllll llllllll"
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
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 = "lllllllllllllllllllllllll
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
Glad to help ;-)
CJ
CJ
ASKER
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
It looked like it worked fine but in reality the label is 10 as high as the form?
/Claes
ASKER
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
It looked like it worked fine but in reality the label is 10 as high as the form?
/Claes
CJ