SAQ
SAQ

Reputation: 55

How do I take font input from the user in c# WinForms

Presuming this is windows default font styling dialog, is there any library to enable/call it for user to select the font for the textbox formatting?

enter image description here

Upvotes: 0

Views: 156

Answers (2)

Jack Whipnert
Jack Whipnert

Reputation: 295

Updated: Assuming you have a FontDialog object (drag and drop from the toolbox) and a textbox, you can add the following code to whatever event you choose. Checking the result of ShowDailog allows to skip the assignment if the user clicks the Cancel button on the dialog.

 if(fontDialog1.ShowDialog() == DialogResult.OK)
 {
     textBox1.Font = fontDialog1.Font;
 };

Upvotes: 0

Rufus L
Rufus L

Reputation: 37020

You can use the FontDialog class to display the font dialog to the user.

The FontDialog.ShowDialog method returns a DialogResult enumeration that you can then check to see if the user pressed "OK", and if they did, then you can set the Font property of your TextBox to the Font property of the dialog:

The code below assumes you have a TextBox named textBox1, and a Button named button1. Clicking the button allows the user to change the Font of textBox1:

private void button1_Click(object sender, EventArgs e)
{
    var fontDialog = new FontDialog();

    // Show the dialog and check the result. 
    // If the user pressed 'Ok', then change the textbox font
    if (fontDialog.ShowDialog() == DialogResult.OK)
    {
        textBox1.Font = fontDialog.Font;
    }
}

Upvotes: 3

Related Questions