473,323 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Label Font Size

Is there a way to / or a mathematical formula to see if a font size is 'too
big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize that
font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
Label2.Font = New Font(Label2.Font.Name, _
Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point, the
font is simply too big for the label.
I know I can add some math for a maximum font size compared to the height
and width of the label, but im wondering if there is another simpler way of
a mathematical formula to do this in one shot.

Thanks,

miro

Oct 24 '08 #1
6 8763
On Oct 24, 6:57*am, "Miro" <m...@beero.comwrote:
Is there a way to / or a mathematical formula to see if a font size is 'too
big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize that
font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
* * * * Label2.Font = New Font(Label2.Font.Name, _
* * * *Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point, the
font is simply too big for the label.
I know I can add some math for a maximum font size compared to the height
and width of the label, but im wondering if there is another simpler way of
a mathematical formula to do this in one shot.

Thanks,

miro
I'm not sure but you may want to enable Label's "AutoSize" property to
True because if you keep it disabled, the label borders will remain
fixed regardless of label's font. By the way, based on your
description, it's clueless that why you're using the code while
resisizng form.

Thanks,

Onur Güzel
Oct 24 '08 #2
"Miro" <mi**@beero.coma écrit dans le message de news:
ei**************@TK2MSFTNGP04.phx.gbl...
Is there a way to / or a mathematical formula to see if a font size is
'too big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize that
font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
Label2.Font = New Font(Label2.Font.Name, _
Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point,
the font is simply too big for the label.
I know I can add some math for a maximum font size compared to the height
and width of the label, but im wondering if there is another simpler way
of a mathematical formula to do this in one shot.

Thanks,
You want to be looking at the font's height to size ratio (which is fixed,
but varies from font to font)
In pseudocode you need to do this
Ratio = font.height/font.size
New font size= label.height/Ratio

If you need to adjust the width to the content (or the content to the width)
use TextRenderer.MeasureText with the font from the label.

Good luck

Oct 24 '08 #3
Yeah I have started looking at finding the ratio,

Another thought I had is to do some benchmarks in some if statements - that
might be easier if i cannot find the ratio easily.

Thanks for your help...
Ill be playing with this this weekend.

Miro

"Clive Lumb" <clumb2@gratuit_en_anglais.fr.invalidwrote in message
news:uL**************@TK2MSFTNGP02.phx.gbl...
"Miro" <mi**@beero.coma écrit dans le message de news:
ei**************@TK2MSFTNGP04.phx.gbl...
>Is there a way to / or a mathematical formula to see if a font size is
'too big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize that
font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
Label2.Font = New Font(Label2.Font.Name, _
Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point,
the font is simply too big for the label.
I know I can add some math for a maximum font size compared to the height
and width of the label, but im wondering if there is another simpler way
of a mathematical formula to do this in one shot.

Thanks,
You want to be looking at the font's height to size ratio (which is fixed,
but varies from font to font)
In pseudocode you need to do this
Ratio = font.height/font.size
New font size= label.height/Ratio

If you need to adjust the width to the content (or the content to the
width) use TextRenderer.MeasureText with the font from the label.

Good luck
Oct 24 '08 #4
Autosize sizes the labels borders to the font size

I am trying to size the fontsize to the labels borders.

I dont see how it is clueluess...as I am trying to enlarge and shrink a
label on the screen depending on the realistate.

"kimiraikkonen" <ki*************@gmail.comwrote in message
news:6d**********************************@m32g2000 hsf.googlegroups.com...
On Oct 24, 6:57 am, "Miro" <m...@beero.comwrote:
Is there a way to / or a mathematical formula to see if a font size is
'too
big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize that
font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
Label2.Font = New Font(Label2.Font.Name, _
Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point,
the
font is simply too big for the label.
I know I can add some math for a maximum font size compared to the height
and width of the label, but im wondering if there is another simpler way
of
a mathematical formula to do this in one shot.

Thanks,

miro
I'm not sure but you may want to enable Label's "AutoSize" property to
True because if you keep it disabled, the label borders will remain
fixed regardless of label's font. By the way, based on your
description, it's clueless that why you're using the code while
resisizng form.

Thanks,

Onur Güzel

Oct 24 '08 #5
I have found a very simple solution to my problem - just posting it so if
anyone else wants to try this, they can.

It's based off of what Clive Lumb said.

I temporarily put 3 textboxes on the screen. On resize i just put the form
width and height and fontsize there so i can watch the size setting.
Basically I found ( with my window layout ) font size 232 was teh biggest
font size i wanted.

Then I took the font size on an inbetween window size - and divided it by
the form height and got a 66% font size to height ratio
then I took the font size and divided it by the width and got a 22% font
size to width ratio.

In the end...I take the height and multiply it by 66%
take the height and multiply it by the 21%
take the minimum of the two

works like a charm.

Thanks Clive - you got me on the right track.

the textbox's will be removed but I left them there so you get the idea.
===
TextBox1.Text = Me.Size.Width.ToString
TextBox2.Text = Me.Size.Height.ToString

Dim CalcFontSizeA As Integer = Math.Min(232,
CType(Math.Round(Me.Label2.Height * 0.66, 0), Integer))
Dim CalcFontSizeB As Integer = Math.Min(232,
CType(Math.Round(Me.Label2.Width * 0.21, 0), Integer))

Dim FinalFontSize As Integer = Math.Min(CalcFontSizeA,
CalcFontSizeB)
Label2.Font = New Font(Label2.Font.Name, FinalFontSize,
Label2.Font.Style) )

TextBox3.Text = Label2.Font.Size.ToString
===

Thank you,

Cheers'

Miro

"Miro" <mi**@beero.comwrote in message
news:ud**************@TK2MSFTNGP02.phx.gbl...
Yeah I have started looking at finding the ratio,

Another thought I had is to do some benchmarks in some if statements -
that might be easier if i cannot find the ratio easily.

Thanks for your help...
Ill be playing with this this weekend.

Miro

"Clive Lumb" <clumb2@gratuit_en_anglais.fr.invalidwrote in message
news:uL**************@TK2MSFTNGP02.phx.gbl...
>"Miro" <mi**@beero.coma écrit dans le message de news:
ei**************@TK2MSFTNGP04.phx.gbl...
>>Is there a way to / or a mathematical formula to see if a font size is
'too big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize
that font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
Label2.Font = New Font(Label2.Font.Name, _
Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point,
the font is simply too big for the label.
I know I can add some math for a maximum font size compared to the
height and width of the label, but im wondering if there is another
simpler way of a mathematical formula to do this in one shot.

Thanks,
You want to be looking at the font's height to size ratio (which is
fixed, but varies from font to font)
In pseudocode you need to do this
Ratio = font.height/font.size
New font size= label.height/Ratio

If you need to adjust the width to the content (or the content to the
width) use TextRenderer.MeasureText with the font from the label.

Good luck
Oct 25 '08 #6
You need to look towards System.Drawing.Graphics.MeasureString.

This method should give you the length of the string as it will be
drawn on the form. It accepts a Font argument so as to better
pinpoint the exact size. I've worked with it before and its not
perfect, but pretty decent.

Oct 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stuart Norris | last post by:
Dear Readers, I am attempting to draw box around some text using unicode on multiline label. The label is forty characters wide and 12 lines deep. I have been trying to draw a box around text...
1
by: Venki | last post by:
I have a textbox to enter an email address followed by a telephone textbox. The email has a regularexpressionvalidator and a requiredfieldvalidator. The ReqField works fine, but if I put in an...
2
by: Peter Rilling | last post by:
Below is some code that I do not know why it does not work. Okay, as you can see it is simple HTML with two ASP:Label controls. Each label has some code that basically gets a string that will be...
3
by: Neil Wallace | last post by:
Hi, This is an odd one. I've been struggling to get "double click" to work well for my controls. The same event handler works perfectly for buttons, but not for labels. Can anyone tell me...
7
by: Joe | last post by:
I have a label control that will be filled with text at runtime. The length of the text will vary, but the label must stay the same size. Is there a way to set the font size of the text such that...
1
by: | last post by:
I have a label control that I've embedded in a datalist template. I will be binding data to that label. I want to run a string formatting function on the database text before it is injected into...
6
by: andrew.ames | last post by:
Hi I have a pretty basic windows application created in Visual Studio 2005 and VB.NET. I set my Form's font to Arial 8.25pt, so when i added a label and a button they automatically have a...
11
by: Peter Larsen [] | last post by:
Hi, I have two questions related to the Label control. How do i prevent the label from expand the canvas into 2 lines (word wrap) ?? At the moment i set AutoSize to false (to prevent the word...
5
by: Mike | last post by:
I am trying to set the font size on a Label control. Seems like a simple item but I can't find a way to do it. I can set other properties for a Label like Borderstyle, BackColor, Fontstyle=BOLD...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.