Easiest way to change font and font size
Asked Answered
S

6

78

which is the easiest way to change Font size with C#.

with java it can all be done easily by calling Font constructor with necessary arguments.

JLabel lab  = new JLabel("Font Bold at 24");
lab.setFont(new Font("Serif", Font.BOLD, 24));
Stortz answered 16/4, 2012 at 11:21 Comment(0)
F
127

Maybe something like this:

yourformName.YourLabel.Font = new Font("Arial", 24,FontStyle.Bold);

Or if you are in the same class as the form then simply do this:

YourLabel.Font = new Font("Arial", 24,FontStyle.Bold);

The constructor takes diffrent parameters (so pick your poison). Like this:

Font(Font, FontStyle)   
Font(FontFamily, Single)
Font(String, Single)
Font(FontFamily, Single, FontStyle)
Font(FontFamily, Single, GraphicsUnit)
Font(String, Single, FontStyle)
Font(String, Single, GraphicsUnit)
Font(FontFamily, Single, FontStyle, GraphicsUnit)
Font(String, Single, FontStyle, GraphicsUnit)
Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte)
Font(String, Single, FontStyle, GraphicsUnit, Byte)
Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean)
Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean)

Reference here

Frech answered 16/4, 2012 at 11:25 Comment(1)
Long time no talk - but you didn't dispose the old font - if you'll do that a lot of times - you might run out of GDI handles. Am I right?Tranquilize
A
35

Use this one to change only font size not the name of the font

label1.Font = new System.Drawing.Font(label1.Font.Name, 24F);
Abirritant answered 10/9, 2015 at 8:32 Comment(0)
G
12

Use the Font Class to set the control's font and styles.

Try Font Constructor (String, Single)

Label lab  = new Label();
lab.Text ="Font Bold at 24";
lab.Font = new Font("Arial", 20);

or

lab.Font = new Font(FontFamily.GenericSansSerif,
            12.0F, FontStyle.Bold);

To get installed fonts refer this - .NET System.Drawing.Font - Get Available Sizes and Styles

Gilleod answered 16/4, 2012 at 11:33 Comment(0)
W
7

This should do it (bold as well);

label1.Font = new Font("Serif", 24,FontStyle.Bold);
Westsouthwest answered 16/4, 2012 at 11:30 Comment(0)
A
4

You can also create a varible and then assign it for a text. It is cool because you can assign it two or more texts.

To assign a variable do that

public partial class Sayfa1 : Form

   Font Normal = new Font("Segoe UI", 9, FontStyle.Bold);

    public Sayfa1()

This varible is not assigned to any text yet.To do it write the name of the text(Look proporties -> (Name)) then write ".Font" then call the name of your font variable.

lupusToolStripMenuItem.Font = Normal;

Now you have a text assigned to a Normal font. I hope I could be helpful.

Ayeshaayin answered 15/9, 2017 at 20:17 Comment(0)
U
0

you can change that using label property in property panel. This screen shot is example that

Uke answered 23/11, 2020 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.