Input for WinForms | ComponentOne
Input Controls / FontPicker
In This Topic
    FontPicker
    In This Topic

    FontPicker is a font selection control that lets users pick the desired font from a drop-down list. Users can select out of a list of more than 250 fonts available in the FontPicker. The FontPicker also supports additional visual styles and themes to choose from so that the users can further customize their desktop applications.

     When you click the dropdown button of the FontPicker control, it's interface looks similar to the following image:

    You can browse through the list to see the available fonts.

    Elements

    FontPicker comprises of three parts: textbox or editor, dropdown button and dropdown list.

    Add FontPicker

    You can add the FontPicker control using the C1FontPicker class as depicted in the code snippet below:

    C#
    Copy Code
     InitializeComponent();
    // Initialize the FontPicker control and add the control to the Form
     this.c1FontPicker1 = new C1.Win.Input.Pickers.C1FontPicker();
     this.Controls.Add(this.c1FontPicker1);
     this.c1FontPicker1.Location = new System.Drawing.Point(105, 81);
     this.c1FontPicker1.Name = "c1FontPicker1";
     this.c1FontPicker1.Size = new System.Drawing.Size(200, 50);
    

    You can use FontPicker with controls like RichTextBox, such that the font of the selected text in RichTextBox gets altered as you pick any font in the FontPicker. For this purpose, the C1FontPicker class provides the SelectedFontFamilyChanged event, which fires when the SelectedFontFamily property changes.

    Observe the code snippet below to set this event:

    C#
    Copy Code
          // Fires when the SelectedFontFamily property changes
          private void c1FontPicker1_SelectedFontFamilyChanged(object sender, EventArgs e)
    {
              if (richTextBox1.SelectionFont != null)
                  richTextBox1.SelectionFont = new Font(((C1.Win.Input.Pickers.C1FontPicker)sender).SelectedFontFamily, richTextBox1.SelectionFont.Size, richTextBox1.SelectionFont.Style);
    }
    

    Invalid Font Error

    Invalid font error message appears when user enters a invalid font family name in the C1FontPicker control. This is a custom message that can be set through InvalidFontFamily.DisplayedText property of C1FontPicker class that is displayed when control loses focus. You need to set InvalidFontFamily.AllowedInvalidFontFamily property of the C1FontPicker class to true, in order to display the error message. The snapshot below shows the text displayed for a wrong input.

    display invalid font text message

    The code snippet below illustrates usage of the above-mentioned properties.

    C#
    Copy Code
    c1FontPicker1.InvalidFontFamily.AllowInvalidFontFamily = true;
    c1FontPicker1.InvalidFontFamily.DisplayedText = "Invalid Font";
    

    Customize FontPicker

    You can customize the FontPicker and change the dropdown alignment, dropdown width and the text alignment in FontPicker editor using the DropDownAlign, DropDownWidth and TextAlign properties.

    The code snippet below shows how to set these properties:

    C#
    Copy Code
    this.c1FontPicker1.Location = new System.Drawing.Point(17, 220);
    this.c1FontPicker1.Name = "c1FontPicker1";
    this.c1FontPicker1.Size = new System.Drawing.Size(200, 25);
    this.c1FontPicker1.DropDownAlign = C1.Win.Input.DropDownAlignment.Center;
    this.c1FontPicker1.DropDownWidth = 300;
    this.c1FontPicker1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
    

    Change Item Size

    FontPicker lets you alter the size of the items in the dropdown form of the control. For this purpose, the C1FontPicker class provides the ItemFontSize property.

    The snapshot below depicts a FontPicker control with the ItemFontSize set as 15.

    The code snippet below shows how to set this property:

    C#
    Copy Code
    c1FontPicker1.ItemFontSize = 15;