473,473 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Font Size vb.net

115 New Member
Hello

I am currently developing an application that displays information to users. I have everything working but one tiny thing thats bugging me. I create a font as below
Expand|Select|Wrap|Line Numbers
  1. Dim[ myfont AsNew Font(["Arial", size, System.Drawing.FontStyle.Regular)
  2.  
I use g.measurestring to measure a string and if it exceeds limits I set the size of the font to a smaller font in a loop until the string fits the limits.
My problem is that when i set my size value to a lower value, myfont does not recognise this, Do i have to dispose of the font and then recreate it for the value of size to be recognised.

I thought it would be as easy as setting font1.size = value but i was wrong! any help would be welcomed with open arms :)

cheers
truez
Jan 12 '09 #1
4 13079
vekipeki
229 Recognized Expert New Member
Font class is immutable in .Net, meaning you cannot change its properties once it is instantiated. It also implements IDisposable, meaning that you must dispose it manually to ensure garbage collection as soon as possible.

In other words, whenever you need a font with a different size, you will have to create a new instance and then dispose it after use:

Expand|Select|Wrap|Line Numbers
  1. Using font As New Font("Tahoma", 1.0F, FontStyle.Regular)
  2.     ' do something with font
  3. End Using ' this will dispose the font
  4.  
Jan 12 '09 #2
truezplaya
115 New Member
ok so i would be looking at something like this
Expand|Select|Wrap|Line Numbers
  1. Dim wob As SizeF = g.MeasureString(scheduleResults(ScreenNumber).Item(1).ToString, font1)]while wob.width > me.width
  2. size - = 5 
  3. end while 
  4. 'dispose of my old font create my new font here
  5.  
My only problem with doing this is knowing which font to put in this line of code
Expand|Select|Wrap|Line Numbers
  1. g.DrawString(scheduleResults(Screen).Item(2).ToString, font1, Brushes.Red, Me.Width * 0.7F, y)
  2.  
Jan 12 '09 #3
vekipeki
229 Recognized Expert New Member
Please fix your CODE tags, your source is really hard to read this way.

I am not sure what you are trying to do. If you want to resize a font to fit your form you must call g.MeasureString several times for different font sizes, until the text fits the control:

Expand|Select|Wrap|Line Numbers
  1.         Dim fontSize As Single = 12.0F
  2.         Dim measuredWidth As Single
  3.  
  4.         Do
  5.             Using font As New Font("Tahoma", fontSize, FontStyle.Regular)
  6.                 measuredWidth = g.MeasureString("text", font).Width
  7.             End Using
  8.             If (measuredWidth > Me.Width) Then
  9.                 fontSize = fontSize - 0.1
  10.             End If
  11.         Loop While (measuredWidth > Me.Width)
  12.  
  13.         Using font As New Font("Tahoma", fontSize, FontStyle.Regular)
  14.             ' do something
  15.         End Using
  16.  
Jan 12 '09 #4
truezplaya
115 New Member
:) cheers thats got me back on track
Jan 12 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Dr John Stockton | last post by:
Assuming default set-ups and considering all reasonable browsers, whatever that may mean, what should an author expect that his readers in general will see (with visual browsers) for a page with...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
7
by: Daniel Kaplan | last post by:
I have the item below at the top of my style sheet. And it seems that the font-szie is ignored. I know that my linked style sheet is being read, and used because if I remove the font-family line,...
16
by: maya | last post by:
I have heard so much preaching here about how font sizes should be set as percentages so users can change font-sizes on their browsers... ok, so now at work am working on a site where we need to do...
7
by: carterweb | last post by:
This is how I do it now. 1. Determine the dimensions of the rectangle. 2. Set a my font size to a fixed maximum size. 3. Apply the font my string and measure the string using the graphics...
30
by: Takehiko Abe | last post by:
I have a <pelement with <ttinside: ;;; <p>A paragraph contains <tt>tt element</tt>.</p> I would like to set the font-size of the TT to the same as the containing <p>. This does not seem to...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
17
by: teser3 | last post by:
I can put out a Word Doc from PHP but if I try and manipulate the font size in the PHP part it outputs a blank Word Document. Here is what I have: <?php $fname="report.doc"; $handle = fopen(...
27
by: 1001 Webs | last post by:
I am trying to make my style sheet as compatible as possible and I'm getting a bit confused here. I've read that the best size for font-size would be 76.1%; due to shortcomings in the way both...
6
by: Miro | last post by:
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.