Export (0) Print
Expand All

FontDialog.Apply Event

Occurs when the user clicks the Apply button in the font dialog box.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public event EventHandler Apply
/** @event */
public void add_Apply (EventHandler value)

/** @event */
public void remove_Apply (EventHandler value)

JScript supports the use of events, but not the declaration of new ones.

Every time the Apply button is clicked, another Apply event is raised.

For more information about handling events, see Consuming Events.

The following code example demonstrates using the MinSize, MaxSize, ShowEffects and FontMustExist members and handling the Apply event. To run this example paste the following code in a form containing a FontDialog named FontDialog1 and a Button named Button1.

private void Button1_Click(System.Object sender, System.EventArgs e)
{
    // Set FontMustExist to true, which causes message box error
    // if the user enters a font that does not exist. 
    FontDialog1.FontMustExist = true;
    
    // Associate the method handling the Apply event with the event.
    FontDialog1.Apply += new System.EventHandler(FontDialog1_Apply);

    // Set a minimum and maximum size to be
    // shown in the FontDialog.
    FontDialog1.MaxSize = 32;
    FontDialog1.MinSize = 18;

    // Show the Apply button in the dialog.
    FontDialog1.ShowApply = true;

    // Do not show effects such as Underline
    // and Bold.
    FontDialog1.ShowEffects = false;
    
    // Save the existing font.
    System.Drawing.Font oldFont = this.Font;

    //Show the dialog, and get the result
    DialogResult result = FontDialog1.ShowDialog();
 
    // If the OK button in the Font dialog box is clicked, 
    // set all the controls' fonts to the chosen font by calling
    // the FontDialog1_Apply method.
    if (result == DialogResult.OK)
    {
        FontDialog1_Apply(this.Button1, new System.EventArgs());
    }
        // If Cancel is clicked, set the font back to
        // the original font.
    else if (result == DialogResult.Cancel)
    {
        this.Font = oldFont;
        foreach ( Control containedControl in this.Controls)
        {
            containedControl.Font = oldFont;
        }
    }
}

// Handle the Apply event by setting all controls' fonts to 
// the chosen font. 
private void FontDialog1_Apply(object sender, System.EventArgs e)
{

    this.Font = FontDialog1.Font;
    foreach ( Control containedControl in this.Controls )
    {
        containedControl.Font = FontDialog1.Font;
    }
}

private void button1_Click(Object sender, System.EventArgs e)
{
    // Set FontMustExist to true, which causes message box error
    // if the user enters a font that does not exist. 
    fontDialog1.set_FontMustExist(true);
    // Associate the method handling the Apply event with the event.
    fontDialog1.add_Apply(new System.EventHandler(fontDialog1_Apply));
    // Set a minimum and maximum size to be
    // shown in the FontDialog.
    fontDialog1.set_MaxSize(32);
    fontDialog1.set_MinSize(18);
    // Show the Apply button in the dialog.
    fontDialog1.set_ShowApply(true);
    // Do not show effects such as Underline
    // and Bold.
    fontDialog1.set_ShowEffects(false);
    // Save the existing font.
    System.Drawing.Font oldFont = this.get_Font();
    //Show the dialog, and get the result
    DialogResult result = fontDialog1.ShowDialog();
    // If the OK button in the Font dialog box is clicked, 
    // set all the controls' fonts to the chosen font by calling
    // the fontDialog1_Apply method.
    if (result.Equals(get_DialogResult().OK)) {
        fontDialog1_Apply(this.button1, new System.EventArgs());
    }
    // If Cancel is clicked, set the font back to
    // the original font.
    else {
        if (result.Equals(get_DialogResult().Cancel)) {
            this.set_Font(oldFont);
            for (int iCtr = 0; iCtr < this.get_Controls().get_Count();
                iCtr++) {
                Control containedControl = this.get_Controls().
                    get_Item(iCtr);
                containedControl.set_Font(oldFont);
            }
        }
    }
} //button1_Click

// Handle the Apply event by setting all controls' fonts to 
// the chosen font. 
private void fontDialog1_Apply(Object sender, System.EventArgs e)
{
    this.set_Font(fontDialog1.get_Font());
    for (int iCtr = 0; iCtr < this.get_Controls().get_Count(); 
        iCtr++) {
        Control containedControl = this.get_Controls().get_Item(iCtr);
        containedControl.set_Font(fontDialog1.get_Font());
    }
} //fontDialog1_Apply

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft