Setting up font of TextBox from code behind
Asked Answered
W

6

39

How do I set the font of a TextBox from a string in the code behind?

// example
txtEditor.FontFamily = "Consolas";
Wound answered 23/10, 2010 at 3:14 Comment(1)
Not a stupid question.Hicks
Y
61
txtEditor.FontFamily = new FontFamily("Consolas"); // the Media namespace
Yorgos answered 23/10, 2010 at 3:32 Comment(1)
If txtEditor is a System.Windows.Forms.TextBox, there is no FontFamily property on that object, but there is a Font property.Min
B
20

Use the following syntax:

lblCounting.Font  = new Font("Times New Roman", 50);

Where lblCounting is just any label.

Bendix answered 8/11, 2012 at 15:19 Comment(1)
Use the markdown and formatting tools to make your code display as code: lblCounting.Font = new Font("Times New Roman", 50);Smell
B
8
System.Drawing.Font = new Font("Arial", 8, FontStyle.Bold);
Belda answered 17/7, 2012 at 18:54 Comment(2)
The question asked how to set the Font but the example implied the OP wanted to set the FontFamily. This answer answers the question if not the example and is the answer I was searching for (though I needed to correct it a bit.)Carlie
To be sure, the OP said nothing about FontFamily in the question, but you are right that it is in the example, but only for the TextBox, rather than setting the Font program-wide.Min
B
3

Copy and paste your example code into the constructor of the form, right after InitializeComponent();

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        txtEditor.FontFamily = new FontFamily("Consolas");
    }
}
Bobsleigh answered 23/10, 2010 at 3:33 Comment(0)
S
3

One simple way to do it globally, programmatically:

public MainWindow()
{
    this.FontFamily = new FontFamily("Segoe UI");
}
Sonia answered 7/10, 2014 at 17:19 Comment(0)
C
2

Use txtEditor.Font.Name = "Consolas";

Charlot answered 23/10, 2010 at 3:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.