This is a migrated thread and some comments may be shown as answers.

Set Font from FontDialog

7 Answers 174 Views
RichTextBox (obsolete as of Q3 2014 SP1)
This is a migrated thread and some comments may be shown as answers.
Ross
Top achievements
Rank 1
Ross asked on 31 May 2012, 04:09 PM
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

7 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 01 Jun 2012, 06:16 PM
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 >>
0
Ross
Top achievements
Rank 1
answered on 04 Jun 2012, 02:59 PM
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
0
Svett
Telerik team
answered on 06 Jun 2012, 04:13 PM
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 >>
0
kishore
Top achievements
Rank 1
answered on 14 May 2014, 08:25 PM
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
0
George
Telerik team
answered on 19 May 2014, 08:40 AM
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.
 
0
kishore
Top achievements
Rank 1
answered on 19 May 2014, 02:48 PM
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,
0
George
Telerik team
answered on 22 May 2014, 10:44 AM
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.
 
Tags
RichTextBox (obsolete as of Q3 2014 SP1)
Asked by
Ross
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Ross
Top achievements
Rank 1
Svett
Telerik team
kishore
Top achievements
Rank 1
George
Telerik team
Share this question
or