C#- FontDialog Control-Trickcode

FontDialog control,ShowDialog ,FontDialog control,FontDialog,The FontDialog control (System.Windows.Forms.FontDialog) is a handy control for selecting different kinds of font and font-related properties.
Share it:

The FontDialog Control

The FontDialog control (System.Windows.Forms.FontDialog) is a handy control for selecting different kinds of font and font-related properties.

The FontDialog Control

The FontDialog Control in C#


Using the FontDialog, you can change the font type, style, size, color, add effects, and specify character sets for the font. You can also preview the actual font. The following are some of the useful properties of the FontDialog control.

PropertiesDescription
ColorThe selected color of the user.
FontThe resulting font constructed using the font dialog.
MaxSizeThe maximum size the dialog can provide.
MinSizeThe minimum size of the dialog can provide.

Note that the FontDialog hides the option to choose a color by default. To allow the user to choose a color, set the value of the ShowColor property to true. You can then access the selected color via the Color property. The ShowApply property allows you to show the Apply button on the FontDialog. You can then handle the Apply event to apply the changes to the target font while not closing the dialog itself.

C#- FontDialog Control


Let’s look at a simple example of using the FontDialog control. Create a form similar to the form shown below.

ShowColor property


We will be changing the font of the text box depending on the constructed font using the FontDialog control. Drag a FontDialog control to the form. Since the FontDialog control is not a visual control, it will be located at the bottom portion of the Designer. Double click the button and use the code below for the event handler.


private void button1_Click(object sender, EventArgs e)
{
    DialogResult result = fontDialog1.ShowDialog();

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

We first called the ShowDialog static method of the FontDialog control to show the actual dialog to the user. The user can now pick the different font properties and click OK. We check if the user presses the OK button by testing the value returned by the ShowDialog method. If the user presses OK then we change the font of the textbox to the one specified by the user.

FontDialog control

Run the program and type any text inside the text box. Click the button to open up the Font Dialog and then choose the desired font. Click OK and the font of the text box will change.

Next Lesson: 
C# Color Dialog Box
Share it:

Windows Forms

Post A Comment:

0 comments: