Thursday, March 7, 2013

RichTextBox losing formatting : Workaround


Problem-

This is the common problem with RichTextBox, when we set any setting like - bold, Italic or Underline, then RichTextBox loses previous setting/formatting. Means it doesn't preserve previous settings.

Example-

We have Text -   Jitendra Faye

after making bold it should be - Jitendra Faye

But output will be - Jitendra Faye     

means here RichTextBox not maintaining previous setting UnderLine.

In this article I am explaining - How to maintain RichTextBox setting while applying new settings?

I also faced same problem and came with one work around.

Solution-

This problem can be solved by setting new formatting to each character instead of going for whole string.

Here is the code-

To make Text as Bold

 private void btnBold_Click(object sender, EventArgs e)
        {
            int selStart = rtfBox.SelectionStart;
            int selLength = rtfBox.SelectionLength;
            int selEnd = selStart + selLength;

            if (rtfBox.SelectionFont.Bold)
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style & ~FontStyle.Bold);

                }
            }
            else
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style | FontStyle.Bold);
                }
            }

        }


To make Text as Italic

  private void btnItalic_Click(object sender, EventArgs e)
        {
            int selStart = rtfBox.SelectionStart;
            int selLength = rtfBox.SelectionLength;
            int selEnd = selStart + selLength;

            if (rtfBox.SelectionFont.Italic)
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style & ~FontStyle.Italic);
                }
            }
            else
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style | FontStyle.Italic);
                }
            }
        }




To make Text as UnderLine

  private void btnUnderLine_Click(object sender, EventArgs e)
        {
            int selStart = rtfBox.SelectionStart;
            int selLength = rtfBox.SelectionLength;
            int selEnd = selStart + selLength;

            if (rtfBox.SelectionFont.Underline)
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style & ~FontStyle.Underline);

                }
            }
            else
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style | FontStyle.Underline);
                }
            }
        }


In above code you can toggle for Bold, Italic and UnderLine by maintaining previous setting also.


Try this code.


Thanks

2 comments:

  1. Replies
    1. Thanks for the information, but that is third party control.... By above code you can maintain formatting for normal .Net RichTextBox control....

      Delete