Jiew Meng
Jiew Meng

Reputation: 88189

Setting up font of TextBox from code behind

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

// example
txtEditor.FontFamily = "Consolas";

Upvotes: 39

Views: 99075

Answers (6)

Strong84
Strong84

Reputation: 1969

One simple way to do it globally, programmatically:

public MainWindow()
{
    this.FontFamily = new FontFamily("Segoe UI");
}

Upvotes: 3

Furqan Ahmed
Furqan Ahmed

Reputation: 201

Use the following syntax:

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

Where lblCounting is just any label.

Upvotes: 20

Amir Nazari
Amir Nazari

Reputation: 113

System.Drawing.Font = new Font("Arial", 8, FontStyle.Bold);

Upvotes: 8

Rahul Soni
Rahul Soni

Reputation: 4968

Use txtEditor.Font.Name = "Consolas";

Upvotes: 2

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59101

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");
    }
}

Upvotes: 3

Gishu
Gishu

Reputation: 136603

txtEditor.FontFamily = new FontFamily("Consolas"); // the Media namespace

Upvotes: 61

Related Questions