2

In my project, I want to use a richtextbox which is featured justified text align and render HTML tags. I examined and tried all examples in the web. But I did not find like in my mind. I tired AdvRichTextBox but it is not support HTML. I want to write some new feature to it. I tried to write a method like below:

private void htmlRTB(AdvRichTextBox rtb)
{
    foreach (var line in rtb.Lines)
    {
        int boldStart = rtb.Find("<b>");
        int boldEnd = rtb.Find("</b>");
        rtb.Select(boldStart, (boldEnd - boldStart));
        rtb.SelectionFont = new Font("Arial", 9, FontStyle.Bold);
        line.Replace("<b>", "").Replace("</b>", "");

        int italicStart = rtb.Find("<i>");
        int italicEnd = rtb.Find("</i>");
        rtb.Select(italicStart, (italicEnd - italicStart));
        rtb.SelectionFont = new Font("Arial", 9, FontStyle.Italic);
        line.Replace("<i>", "").Replace("</i>", "");
    }
}

But it was screwed up. When the HTML tags were used in first time, they are shown bold or italic. But replace method do not work. And when the HTML tags were used in second or more time they are not shown bold or italic and replace method do not work. This is the screenshot image: enter image description here Please could you help me how do i solve this problem? Kind regards.

2
  • Order issue, perhaps? Try doing the replace on rtb instead of line? Commented Nov 14, 2012 at 22:32
  • 2
    No, please, not order issue. I really want to manage this. But cause of my short knowledge about C# do not let the managing the project. So i want to get some help here. If you can, I would glad it. Commented Nov 16, 2012 at 10:57

1 Answer 1

1

Looks like you're just replacing the tags in the line you pull out instead of the RTB itself. You need to run the replace on the RTB text. Shouldn't be too hard since you already have the int locations for the tags, can just use that.

Also, your for loop won't work the way you think it will, since all that text is considered one line... That's why it only bolded/highlighted the first ones. When I get home from work Ill try to write an example out for ya.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.