Saving and setting Rich Text editor font name and size

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

    • Post Options:
    • Link

    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.

  • Posted 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

  • Posted 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.

  • Posted 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.

  • Posted 23 May 2022, 3:04 pm EST

    Got it. Works perfectly. Sorry for the delayed response Nitin. This case may be closed.

  • Posted 30 June 2022, 11:21 am EST

    Hi. I trust you don’t mind me adding another question here. I figure this would be a logic place to ask this and for others to look for the same answer.

    You have given the logic on how to retrieve and set the current font name and size. What about other toolbar items? Is there a document for the various toolbar controls? I’m wanting to set and save the foreground and background colors and the paragraph (margin, padding, line spacing). Thanks, Mike

  • Posted 30 June 2022, 10:32 pm EST

    Hi Mike,

    Thanks for the acknowledgement.

    We are discussing with the development team to save and set value on DropdownButtons like C1FontColorTool, C1TextHighLightTool, C1MarginTool and C1PaddingTool. We will get back to you once we have any update from them. [Internal Tracking Id - C1XAML-29848]

    However, You can save and set linespacing as:

    
            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);
                C1LineSpacingTool lineSpacingTool = (C1LineSpacingTool)toolbar.Template.FindName("47_T", toolbar);
    
    
                toolbar.FontFamilies.Clear();
                foreach (var font in Fonts.SystemFontFamilies)
                    toolbar.FontFamilies.Add(font);
                FetchSetting();
                fontFamily.Loaded += FontFamily_Loaded;
                fontSize.Loaded += FontSize_Loaded;          
                lineSpacingTool.Loaded += LineSpacingTool_Loaded;
            }
           private void LineSpacingTool_Loaded(object sender, RoutedEventArgs e)
            {
                var lineSpacingTool = (C1LineSpacingTool)sender;
                if(setting.SavedLineSpace!=null)
                    lineSpacingTool.SelectedValue = Double.Parse(setting.SavedLineSpace);
            }
    
    

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

    Best Regards,

    Nitin

  • Posted 7 July 2022, 8:33 pm EST

    Hi Mike,

    Apologize for the delay.

    We are working on other toolbar buttons customization.

    Could you please confirm that which version you are using? Is it 4.5.2 or 4.6.2 version.

    If it’s 4.5.2, can you consider upgrading to 4.6.2?

    Regards,

    Nitin

  • Posted 25 July 2022, 2:32 pm EST

    I am sorry for not replying until now. For some reason, I don’t receive emails when my posts are updated. I am on 4.5.2. I can upgrade if needed.

  • Posted 25 July 2022, 2:47 pm EST

    Hi Mike,

    Thanks for the response.

    There is some issue while saving or setting value on DropdownButtons like C1FontColorTool, C1TextHighLightTool, C1MarginTool and C1PaddingTool. We are working on it. And that’s nice to hear you that you can migrate to 4.6.2.

    We will inform you whenever we get any update form development team.

    Best Regards,

    Nitin

  • Posted 26 April 2023, 4:20 pm EST

    Hi,

    The issue is fixed in the 4.6.2 build, and now you can fetch C1FontColorTool, C1TextHighLightTool, C1MarginTool, and C1PaddingTool.

            private void ToolBar_Loaded(object sender, RoutedEventArgs e)
            {
                C1RichTextBoxToolbar toolbar = sender as C1RichTextBoxToolbar;
                //Font Color
                fontColor = (C1FontColorTool)toolbar.Template.FindName("28_T", toolbar);
    
                //Text Highlight
                colorTool = (C1TextHighlightTool)toolbar.Template.FindName("29_T", toolbar);
    
                //Marging
                marginTool = (C1MarginTool)toolbar.Template.FindName("45_T", toolbar);
    
                //Padding
                paddingTool = (C1PaddingTool)toolbar.Template.FindName("46_T", toolbar);
            }

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

    You can download latest C1 from here: https://www.grapecity.com/componentone/download

    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