Set Font from FontDialog

8 posts, 0 answers
  1. Ross
    Ross avatar
    2 posts
    Member since:
    Nov 2010

    Posted 31 May 2012 Link to this post

    Can I set font family and size from a FontDialog() for a selected area of text in a RichTextBox? If so, what properties do I need to look for and set? I'm doing this in C#.

    Thanks,
    Ross
  2. Nikolay
    Admin
    Nikolay avatar
    1803 posts

    Posted 01 Jun 2012 Link to this post

    Hi Ross,

    Thank you for the question.

    You need to follow the API below in order to achieve your requirement:

    private void radButton2_Click(object sender, EventArgs e)
    {
        FontDialog fDialog = new FontDialog();
        DialogResult res = fDialog.ShowDialog();
        if (res == System.Windows.Forms.DialogResult.OK)
        {
            StyleDefinition style = new StyleDefinition();
            style.SetPropertyValue(Span.FontSizeProperty, fDialog.Font.SizeInPoints);
            style.SetPropertyValue(Span.FontFamilyProperty, fDialog.Font.FontFamily.Name);
     
            if (fDialog.Font.Style == FontStyle.Bold)
            {
                style.SetPropertyValue(Span.FontStyleProperty, TextStyle.Bold);
            }
     
            if (fDialog.Font.Style == FontStyle.Italic)
            {
                style.SetPropertyValue(Span.FontStyleProperty, TextStyle.Italic);
            }
     
            if (fDialog.Font.Style == FontStyle.Regular)
            {
                style.SetPropertyValue(Span.FontSizeProperty, TextStyle.Regular);
            }
     
            this.radRichTextBox1.ChangeSpanStyle(style);
        }
    }

    I hope this helps.

    Kind regards,
    Nikolay
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ross
    Ross avatar
    2 posts
    Member since:
    Nov 2010

    Posted 04 Jun 2012 Link to this post

    Hi Nikolai,

    That sort of worked.It works fine the first time I try it but it generates an error the second when I try to change either highlighted text or just have the cursor in the box to change the font after.

    This is the error I get:
    Value of '0' is not valid for 'emSize'. 'emSize' should be greater than 0 and less than or equal to System.Single.MaxValue.
    Parameter name: emSize

    I checked the values from the font dialog and I'm getting a valid size and font name.

    Here is the code:
        private void radButtonElement3_Click(object sender, EventArgs e)
        {
          FontDialog fDialog = new FontDialog();
          try
          {
           
            DialogResult res = fDialog.ShowDialog();
            if (res == System.Windows.Forms.DialogResult.OK)
            {
              Telerik.WinControls.RichTextBox.Model.Styles.StyleDefinition style = new Telerik.WinControls.RichTextBox.Model.Styles.StyleDefinition();
              //StyleDefinition style = new StyleDefinition();
              style.SetPropertyValue(Span.FontSizeProperty, fDialog.Font.SizeInPoints);
              style.SetPropertyValue(Span.FontFamilyProperty, fDialog.Font.FontFamily.Name);

              if (fDialog.Font.Style == FontStyle.Bold)
              {
                style.SetPropertyValue(Span.FontStyleProperty, TextStyle.Bold);
              }

              if (fDialog.Font.Style == FontStyle.Italic)
              {
                style.SetPropertyValue(Span.FontStyleProperty, TextStyle.Italic);
              }

              if (fDialog.Font.Style == FontStyle.Regular)
              {
                style.SetPropertyValue(Span.FontSizeProperty, TextStyle.Regular);
              }

              this.radRichTextBox1.ChangeSpanStyle(style);
            }
          }
          catch(Exception ex)
          {
            string err = ex.Message;
          }

        }

    The error is at this.radRichTextBox1.ChangeSpanStyle(style);

    Thanks,

    Ross
  5. Svett
    Admin
    Svett avatar
    728 posts

    Posted 06 Jun 2012 Link to this post

    Hi Ross,

    You should use the following code snippet based on my colleague's one:

    private void button1_Click(object sender, EventArgs e)
    {
        FontDialog fDialog = new FontDialog();
        DialogResult res = fDialog.ShowDialog();
     
        if (res == System.Windows.Forms.DialogResult.OK)
        {
            Telerik.WinControls.RichTextBox.Model.Styles.StyleDefinition style =
                new Telerik.WinControls.RichTextBox.Model.Styles.StyleDefinition();
            style.SetPropertyValue(Span.FontSizeProperty, fDialog.Font.SizeInPoints);
            style.SetPropertyValue(Span.FontFamilyProperty, fDialog.Font.FontFamily.Name);
     
            TextStyle? textStyle = null;
     
            if (fDialog.Font.Style == FontStyle.Bold)
            {
                textStyle = TextStyle.Bold;
            }
     
            if (fDialog.Font.Style == FontStyle.Italic)
            {
                textStyle = textStyle != null ? textStyle.Value | TextStyle.Italic : TextStyle.Italic;
            }
     
            if (fDialog.Font.Style == FontStyle.Regular)
            {
                textStyle = textStyle != null ? textStyle.Value | TextStyle.Regular : TextStyle.Regular;
            }
     
            style.SetPropertyValue(Span.FontStyleProperty, textStyle.Value);
            this.radRichTextBox1.ChangeSpanStyle(style);
        }
     
    }

    I hope this helps.

    Kind regards,
    Svett
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  6. kishore
    kishore avatar
    4 posts
    Member since:
    May 2014

    Posted 14 May 2014 in reply to Svett Link to this post

    I have a small issue, how to account for bold italic option in the font style. I don't see the "Bold Italic" as a part of font Style or TextStyle
  7. George
    Admin
    George avatar
    500 posts

    Posted 19 May 2014 Link to this post

    Hello Krishore,

    Thank you for replying.

    You need to combine both enum values - Bold and Italic in a single value. Since this case is not taken into consideration in the below example, you can use the following code in order to take advantage of such combined values:
    FontDialog fDialog = new FontDialog();
    DialogResult res = fDialog.ShowDialog();
     
    if (res == System.Windows.Forms.DialogResult.OK)
    {
        Telerik.WinControls.RichTextBox.Model.Styles.StyleDefinition style =
            new Telerik.WinControls.RichTextBox.Model.Styles.StyleDefinition();
        style.SetPropertyValue(Span.FontSizeProperty, fDialog.Font.SizeInPoints);
        style.SetPropertyValue(Span.FontFamilyProperty, fDialog.Font.FontFamily.Name);
     
        TextStyle? textStyle = null;
     
        FontStyle[] fontStyles = (FontStyle[])Enum.GetValues(typeof(FontStyle));
     
        Font dialogFont = fDialog.Font;
        foreach (FontStyle fontStyle in fontStyles)
        {
            if ((dialogFont.Style & fontStyle) == fontStyle)
            {
                TextStyle parsedtextStyle;
                if (Enum.TryParse<TextStyle>(fontStyle.ToString(), out parsedtextStyle))
                {
                    if (!textStyle.HasValue)
                    {
                        textStyle = parsedtextStyle;
                    }
                    else
                    {
                        textStyle |= parsedtextStyle;
                    }
                }
            }
        }
     
        style.SetPropertyValue(Span.FontStyleProperty, textStyle.Value);
        this.FindControl<RadRichTextBox>().ChangeSpanStyle(style);
    }

    Also note that RadRichTextBox's TextStyle does not support all values which FontStyle supports. Specifically, Underline and Strikeout.

    Let me know if you have further questions.

    Regards,
    George
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  8. kishore
    kishore avatar
    4 posts
    Member since:
    May 2014

    Posted 19 May 2014 in reply to George Link to this post

    Thanks George for the response. I do have some issues pertaining to the RadRichTextbox. 

    1) wanted to set the default font on the RadRichTextBox at all time means if I copy paste from an external source or import a document I always wanted to set the default font to something like Arial Style and Font size 8, I did something like below but have lot of issues when I do perform some other operations on the textbox which I will present here.

    For default font I did something like this , please let me know if there is a better way of doing this with the points mentioned above.



    private void radRichTextBox1_Initialized(object sender, EventArgs e)
    {
        Telerik.WinControls.RichTextBox.Model.Styles.StyleDefinition style =
                            new Telerik.WinControls.RichTextBox.Model.Styles.StyleDefinition();
     
        style.SetPropertyValue(Span.FontSizeProperty, Unit.PointToDip(8));
        style.SetPropertyValue(Span.FontFamilyProperty, "Arial");
     
        this.radRichTextBox1.ChangeSpanStyle(style);
     
        this.radRichTextBox1.ChangeParagraphSpacingAfter(0);
        this.radRichTextBox1.ChangeSectionMargin(new Padding(30));
    }

    2) I wanted to insert a horizontal Line when I click a button, I did something like this. It's inserting a horizontal line but messing up the font before and after the inserted horizontal line as seen in the attached image, can you suggest me on this.

    private void radButtonElementInsHorLine_Click(object sender, EventArgs e)
            {
                AddTableAsSeparator(this.radRichTextBox1.Document);
            }
     
            private void AddTableAsSeparator(RadDocument doc1)
            {
                doc1.CaretPosition.MoveToLastPositionInDocument();
     
                Table table = new Table(1, 1);
                table.Borders.All = new Border(1, Telerik.WinControls.RichTextBox.Model.BorderStyle.Single, Color.Transparent);
                table.Borders.Bottom = new Border(1, Telerik.WinControls.RichTextBox.Model.BorderStyle.Single, Color.Black);
                doc1.Sections.First.Blocks.Add(table);
     
                this.MeasureAndArrangeInDefaultSize(doc1);
                this.radRichTextBox1.ChangeParagraphSpacingAfter(0);
                this.radRichTextBox1.ChangeParagraphSpacingBefore(0);
     
            }

    3) I wanted to achieve Find and Replace on button click , I couldn't find an option to do that like a show a pop up box and then do find and replace , I know it can be achieved with Ctrl+F but I want to achieve the same on button click and hook that up with the textbox, can you please suggest me on how to go about this.

    Thanks,
  9. George
    Admin
    George avatar
    500 posts

    Posted 22 May 2014 Link to this post

    Hello Kirshore,

    Thank you for writing back.

    I would like to kindly ask you to submit these questions in new separate threads since mixing different topics in one thread makes finding information in the forum more difficult. This will also allow us to provide you with more adequate and quicker responses.

    Looking forward to hearing from you again.

    Regards,
    George
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WinForms is Visual Studio 2017 Ready