article

Changing Object Fonts In Code @ RunTime

Email
Submitted on: 1/1/2015 10:18:00 AM
By: Pooh! (from psc cd)  
Level: Beginner
User Rating: By 10 Users
Compatibility: VB.NET
Views: 1084
 
     This little tutorial shows how to change the font on controls at runtime. Easy to do in the IDE, but, to do it in code at runtime in .NET (compared to VB6) it is a bit different!

 
				

The reason for this tutorial:

Feeling like a newbie again after 5 years with VB, .NET poses some obsticals that I certainly was not prepared for.
Changing a font size was quick and easy in VB6, but in the .NET world, it proved itself to be a tedious task. My example is shown below:

I have a solution with a blank form, and at runtime, all controls are dynamically built from a text file and placed in the window.

The problem is that with .NET, all controls like this default to the form's font settings - And MS made the Font property of labels, etc. READ-ONLY.?

In order to accomplish this, you need to create a new font object with your values, and assign it to your (Control).Font property.

This will allow you to change the Font Family, the size in points, and the Bold / Italic etc. properties as well.

As an example, we are defining label "Label1" the Font Family "Arial", 8.25 Points in size, and BOLD.
Label1.Font = New Font("Arial", 8.25, FontStyle.Bold, GraphicsUnit.Point, 0, False)


You could also express it as:
Dim MyFont as New Font("Arial", 8.25, FontStyle.Bold, GraphicsUnit.Point, 0, False)

Label1.Font = MyFont

Label2.Font = MyFont


I hope this helps someone, as I was scratchin' hair out of my head over this. I knew it could be done.

Funny thing is, it actually was pretty simple! Heh.

John.


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.