README.md

December 4, 2022 ยท View on GitHub

Changing Object Fonts In Code @ RunTime

Description

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!

More Info

Submitted On
ByPooh!
LevelBeginner
User Rating4.9 (49 globes from 10 users)
CompatibilityVB.NET
CategoryControls/ Forms/ Dialogs/ Menus
World.Net (C#, VB.net)
Archive File

Source Code



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.