Saving and setting Rich Text editor font name and size

Posted by: mike on 12 September 2021, 1:31 pm EST

  • Posted 12 September 2021, 1:31 pm EST

    Hi. I have the rich text toolbar and editor on a form (linked together). I am wanting to save the last selected font name and size when the form is closed, and then set it when the form it opened.

    I save off Toolbar.FontFamily.Source and Toolbar.FontSize. FontFamilySource does not contain the selected font name, and I cannot locate any property that has it.

    When I set it FontFamily = new FontFamily(font name) and FontSize = fontsize, the values are not set on the toolbar.

    I have tried the toolbar and the editor and nothing is working. Could you please help? Thanks.
  • Marked as Answer

    Replied 13 September 2021, 6:27 pm EST

    Hi Mike,

    You can save FontSize and FontFamily at the time of window closing. For that you need to find C1FontFamilyTool and C1FontSizeTool from C1RichTextBoxToolbar's template in Closing event handler of Window and then you can save SelectedValue in XML :

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    if(rtbToolbar.IsLoaded)
    {
    C1FontFamilyTool fontFamily = (C1FontFamilyTool)rtbToolbar.Template.FindName("13_T", rtbToolbar);
    C1FontSizeTool fontSize = (C1FontSizeTool)rtbToolbar.Template.FindName("15_T", rtbToolbar);

    using (XmlWriter writer = XmlWriter.Create(fileName))
    {
    ToolbarSetting setting = new ToolbarSetting();
    if(fontSize.SelectedValue!=null & fontFamily.SelectedValue!=null)
    {
    setting.SavedFontSize = fontSize.SelectedValue.ToString();
    setting.SavedFontFamily = fontFamily.SelectedValue.ToString();
    System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(ToolbarSetting));
    xml.Serialize(writer, setting);
    }
    }
    }
    }


    You can load previously save FontSize and FontFamily. For that you need to handle Loaded event of C1RichTextBoxToolbar and then you can set SelectedValue of C1FontFamilyTool and C1FontSizeTool( which can get from toolbar's template) :

    private void ToolBar_Loaded(object sender, RoutedEventArgs e)
    {
    C1RichTextBoxToolbar toolbar = sender as C1RichTextBoxToolbar;
    C1FontFamilyTool fontFamily = (C1FontFamilyTool)toolbar.Template.FindName("13_T", toolbar);
    C1FontSizeTool fontSize = (C1FontSizeTool)toolbar.Template.FindName("15_T", toolbar);
    try
    {
    using (StreamReader reader = File.OpenText(fileName))
    {
    ToolbarSetting setting = new ToolbarSetting();
    System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(ToolbarSetting));
    setting = (ToolbarSetting)xml.Deserialize(reader);
    fontSize.SelectedValue = Double.Parse(setting.SavedFontSize);
    fontFamily.SelectedValue = new FontFamily(setting.SavedFontFamily);
    }
    }
    catch (Exception ex)
    {
    fontSize.SelectedIndex = 0;
    fontFamily.SelectedIndex = 0;
    }
    }


    Please refer the attached sample for the same : RichTextBoxToolbar.zip

    Best Regards,

    Nitin
  • Replied 28 April 2022, 1:48 pm EST

    Hi. I am still having issues with the toolbar font family and size defaulting. Here is the code that is being called (based on Nitin's earlier answer):

          private void Toolbar_Loaded(object sender, RoutedEventArgs args)
    {
    C1RichTextBoxToolbar tb = sender as C1RichTextBoxToolbar;
    C1FontFamilyTool fontname = tb.Template.FindName("13_T", tb) as C1FontFamilyTool;
    C1FontSizeTool fontsize = tb.Template.FindName("15_T", tb) as C1FontSizeTool;
    fontname.SelectedValue = new FontFamily("Courier New");
    fontsize.SelectedValue = 16.0;
    }


    The toolbar shows Courier New and size 16. When I start typing in the editor, the font family immediately changes to Segoe UI and the size is 8.25. How come?

    Secondly, the list of fonts shows only 7 fonts. Microsoft Word on my computer shows many more than this (100+). What do I need to do to show all fonts in this dropdown? Are these only fix-width fonts? If so, there is a setting to display all?

    Thanks.
  • Replied 28 April 2022, 10:11 pm EST

    Hi Mike,

    Thanks for reaching out to us again with your query.

    1. You were very close to the solution. Try to set FontFamilyTool and FontSizeTool's SelectedValue when it is loaded i.e., you need to handle Loaded event for both tools and then set SelectedValue accordingly.

    2. You can add all SystemFontFamilies to the C1FontFamilyTool.

    Refer below code snippet for both requirements:

    private void ToolBar_Loaded(object sender, RoutedEventArgs e)
    {
    C1RichTextBoxToolbar toolbar = sender as C1RichTextBoxToolbar;
    C1FontFamilyTool fontFamily = (C1FontFamilyTool)toolbar.Template.FindName("13_T", toolbar);
    C1FontSizeTool fontSize = (C1FontSizeTool)toolbar.Template.FindName("15_T", toolbar);

    //Load all system font families in toolbar
    toolbar.FontFamilies.Clear();
    foreach (var font in Fonts.SystemFontFamilies)
    toolbar.FontFamilies.Add(font);

    fontFamily.Loaded += FontFamily_Loaded;
    fontSize.Loaded += FontSize_Loaded;
    }

    private void FontFamily_Loaded(object sender, RoutedEventArgs e)
    {
    //set default font family
    (sender as C1FontFamilyTool).SelectedValue = new FontFamily("Courier New");
    }
    private void FontSize_Loaded(object sender, RoutedEventArgs e)
    {
    //set default font size
    (sender as C1FontSizeTool).SelectedValue = (double)16;
    }


    Please refer the attached sample for the same: RichTextBoxToolbarDefaultFontFamily_FontSize.zip

    Best Regards,
    Nitin.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels