none
Trying to manipulate Bold, Italic and Underline Text RRS feed

  • Question

  • Hey!

    Im trying to make a program that can feel if a word in richtextbox is bold for example.
    If a word is bold for example "Test" then it put <b> around the word.. I cant get it to work.. When im writing test test then it replaces both of them.. Some ideas how i only can change the bold one?
    Ask if you dont understand :)

                //var output = "";
                //for (MallTextRichtxt.SelectionStart = 0; MallTextRichtxt.SelectionStart < MallTextRichtxt.TextLength; MallTextRichtxt.SelectionStart++)
                //{
                //    MallTextRichtxt.SelectionLength = 1;
    
                //    var s = MallTextRichtxt.SelectionFont.Style;
    
                //    if ((s & FontStyle.Bold) != 0)
                //    {
                //        output += MallTextRichtxt.SelectedText;
    
                //    }
                //    else
                //    {
    
                //    }
    
                //}
    
    
    
                //string[] lines = output.Split(new[] { " " }, StringSplitOptions.None);
                //string Test = MallTextRichtxt.Text;
                //string test2 = "";
                //if (output.Contains(" "))
                //{
                //    for (int i = 0; i < lines.Count(); i++)
                //    {
                //        if (lines[i] != "")
                //        {
                //            //Test.Replace(lines[i].ToString(), "<b>" + lines[i].ToString() + "</b>");
                //            test2 = Test.Replace(lines[i], "<b>" + lines[i] + "</b>");
    
                //        }
                //        else
                //        {
    
                //        }
    
    
                //    }
                //    MessageBox.Show(test2);
                //}
                //else
                //{
    
    
                //    try
                //    {
                //        test2 = Test.Replace(output, "<b>" + output + "</b>");
                //        MessageBox.Show(test2);
                //    }
                //    catch
                //    {
                //        MessageBox.Show("Det gick inte att köra replace");
                //    }
    
    
                //}

    Best regards

    Sunday, February 23, 2020 8:02 PM

Answers

  • /* A quick and dirty way but 100,000% the only "right" way to do the job. What you really want and need to do is learn about the Rich Text Format (RTF) itself. http://www.biblioscape.com/rtf15_spec.htm So let's say you want to replace all the RTF BOLD codes with HTML BOLD tags. */ this.richTextBox1.Rtf = this.richTextBox1.Rtf.Replace(@"\b ", @"<b>").Replace(@"\b0", @"</b>");

    /*

    Alternatively, if you want to wrap the bolded text in HTML tags but *leave it bolded* in RTF

    */

    this.richTextBox1.Rtf = this.richTextBox1.Rtf.Replace(@"\b ", @"<b>\b ").Replace(@"\b0", @"\b0</b>");





    Monday, February 24, 2020 1:28 AM

All replies

  • /* A quick and dirty way but 100,000% the only "right" way to do the job. What you really want and need to do is learn about the Rich Text Format (RTF) itself. http://www.biblioscape.com/rtf15_spec.htm So let's say you want to replace all the RTF BOLD codes with HTML BOLD tags. */ this.richTextBox1.Rtf = this.richTextBox1.Rtf.Replace(@"\b ", @"<b>").Replace(@"\b0", @"</b>");

    /*

    Alternatively, if you want to wrap the bolded text in HTML tags but *leave it bolded* in RTF

    */

    this.richTextBox1.Rtf = this.richTextBox1.Rtf.Replace(@"\b ", @"<b>\b ").Replace(@"\b0", @"\b0</b>");





    Monday, February 24, 2020 1:28 AM
  • Hi Carlo,

    Thank you for posting here.

    As Andrew said, as long as you can understand the rtf format text , the problem is not complicated.

    For example, I have a string Test in format rich in my richtextbox. When I use richtextbox.Rtf, I get a string like this:

    {\rtf1\ansi\ansicpg936\deff0\nouicompat\deflang1033\deflangfe2052{\fonttbl{\f0\fswiss\fcharset0 Calibri;}{\f1\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}
    {\*\generator Riched20 10.0.18362}\viewkind4\uc1 
    \pard\b\f0\fs18 Test\b0\f1\lang2052\par
    }

    Note the "\ b" and "\ b0" in the last line, which are replaced in Andrew's method. If the text is italic, they will be "\ i" and "\ i0".

    So, Andrew's method should be good for your requirement.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 24, 2020 2:51 AM
  • Thanks alot! It works! 
    Thursday, February 27, 2020 12:33 PM
  • /* A quick and dirty way but 100,000% the only "right" way to do the job. What you really want and need to do is learn about the Rich Text Format (RTF) itself. http://www.biblioscape.com/rtf15_spec.htm So let's say you want to replace all the RTF BOLD codes with HTML BOLD tags. */ this.richTextBox1.Rtf = this.richTextBox1.Rtf.Replace(@"\b ", @"<b>").Replace(@"\b0", @"</b>");

    /*

    Alternatively, if you want to wrap the bolded text in HTML tags but *leave it bolded* in RTF

    */

    this.richTextBox1.Rtf = this.richTextBox1.Rtf.Replace(@"\b ", @"<b>\b ").Replace(@"\b0", @"\b0</b>");





    Got a problem now.. When a word is Bold and another word is underlined. Then it makes both words Bold.. I want to make so if a word is Bold then it gets Bold tags and if a word is underlined then it gets Underlined tags.. The problem comes when the word is the first word in richtextbox..
    This is what im using..:
                MallTextRichtxt.Rtf = MallTextRichtxt.Rtf.Replace(@"\b ", @"<b>").Replace(@"\b0", @"</b>");
                MallTextRichtxt.Rtf = MallTextRichtxt.Rtf.Replace(@"\i ", @"<i>").Replace(@"\i0", @"</i>");
                MallTextRichtxt.Rtf = MallTextRichtxt.Rtf.Replace(@"\ul ", @"<u>").Replace(@"\ulnone", @"</u>");


    Thursday, February 27, 2020 12:53 PM
  • Hi, Carlo.  Please post the content of the RTF field alongside the C# code.
    Thursday, February 27, 2020 2:26 PM
  • Code:
                    this.MallTextRichtxt.Rtf = MallTextRichtxt.Rtf.Replace(@"\b ", @"<b>").Replace(@"\b0", @"</b>");
                    MallTextRichtxt.Rtf = MallTextRichtxt.Rtf.Replace(@"\i ", @"<i>").Replace(@"\i0", @"</i>");
                    MallTextRichtxt.Rtf = MallTextRichtxt.Rtf.Replace(@"\ul ", @"<u>").Replace(@"\ulnone", @"</u>");
    
    
    
                //this.MallTextRichtxt.Rtf = this.MallTextRichtxt.Rtf.Replace(@"\b ", @"<b> \b").Replace(@"\b0", @"</b>");
                //this.MallTextRichtxt.Rtf = this.MallTextRichtxt.Rtf.Replace(@"\i ", @"<i> \i").Replace(@"\i0", @"</i>");
                //this.MallTextRichtxt.Rtf = this.MallTextRichtxt.Rtf.Replace(@"\ul ", @"<u> \ul").Replace(@"\ulnone", @"</u>");
    
                if (MallTextRichtxt.Text.Contains("\n"))
                {
                    string[] lines = MallTextRichtxt.Text.Split(new[] { "\n" }, StringSplitOptions.None);
    
                    for (int i = 0; i < lines.Count(); i++)
                    {
    
                        färdigMejlMall += lines[i].ToString() + "<br>";
                    }
                }
                else
                {
    
                    färdigMejlMall = MallTextRichtxt.Text;
                }



    Thursday, February 27, 2020 2:37 PM
  • Uhh.  Carlo.  BEFORE you do any replacements:

    Clipboard.SetText(MallTextRichtxt.Rtf)

    Then paste that out before copying the C#.  Why on earth would you think I cared about a screenshot at runtime?

    Thursday, February 27, 2020 2:41 PM
  • i get this:
    {\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1053{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    {\*\generator Riched20 10.0.18362}\viewkind4\uc1 
    \pard\b\f0\fs17 test\b0  \ul test\ulnone\par
    }
    And looks like this after:
    
    Thursday, February 27, 2020 2:47 PM
  • Thank you! So several points:

    {\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1053{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    {\*\generator Riched20 10.0.18362}\viewkind4\uc1 
    \pard\b\f0\fs17 test\b0  \ul test\ulnone\par
    }

    Note the presence and location of whitespace (ie the substring ' '). It looks like the RichTextBox Control itself is a little spotty about how and when and where it inserts whitespace following an RTF format tag. You identified that this new problem occurs at the beginning of the text, but you missed that your replacements target '\b ' while in this case there is no space following the \b - instead the opening \b tag precedes some others, then there's a space after all that. ie: '\pard\b\f0\fs17 '

    It may help if you learn to look at RTF formatting with the same critical eye that you look at any other code.

    {
        \rtf1
        \ansi
        \ansicpg1252
        \deff0
        \nouicompat
        \deflang1053
        {
            \fonttbl
            {
                \f0
                \fnil
                \fcharset0 Microsoft Sans Serif;
            }
        }
        {
            \*
            \generator Riched20 10.0.18362
        }
        \viewkind4
        \uc1 
        \pard
        \b
        \f0
        \fs17 test
        \b0  
        \ul test
        \ulnone
        \par
    }
    

    So that's the problem you're having. I'd suggest using RegEx but this RTF format is so much like XML or HTML with open/close/child tags that I'm doubtful if RTF markup can be treated as "regular." As time permits I'll see if I can drum up a simplistic RTF parser for you today.

    Thursday, February 27, 2020 3:09 PM
  •         // I'm just hard-setting your specific RTF into a string for simplicity.
            string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1053{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}{\*\generator Riched20 10.0.18362}\viewkind4\uc1 \pard\b\f0\fs17 test\b0  \ul test\ulnone\par}";
            private void Form1_Load(object sender, EventArgs e)
            {
                string rptrn = @"\\b(\\|\{|\}|\s)";
                string newRTF = System.Text.RegularExpressions.Regex.Replace(rtf, rptrn, @"<b>");
                rptrn = @"\\b0(\\|\{|\}|\s)";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"</b>");
                rptrn = @"\\ul(\\|\{|\}|\s)";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"<u>");
                rptrn = @"\\ulnone(\\|\{|\}|\s)";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"</u>");
                Clipboard.SetText(newRTF);
            }

    It looks like Regular Expressions work well.  Having to do them one after another like this is a real kludge, and RegEx isn't great performance anyway.  I usually prefer custom parsing over RegEx unless code distribution is an issue - most chumps know RegEx and can't grok parsers.

    The logic that makes this regex work is as follows:

    * Any RTF Tag Declaration begins with a \

    * Any RTF Tag Declaration may end with a \ (which starts a new declaration) or a { (which encloses attributes or children) or a } (for the same reason) or a plain single whitespace, which terminates an RTF control-sequence.

    There will probably be additional cases where this doesn't work well.  If you're just doing simple formatting to HTML tags then you probably won't actually hit any of those.

    Thursday, February 27, 2020 5:48 PM

  • Almost working now! Why does it add "f0" and "par" after u>.
    Thanks alot for all help!
    Thursday, February 27, 2020 7:08 PM
  • I guess it's replacing the / that doubles as a control-code terminator for the previous code and a control-code starter for the next code in a chain.
    Thursday, February 27, 2020 7:09 PM

  • Almost working now! Why does it add "f0" and "par" after u>.
    Thanks alot for all help!

            // I'm just hard-setting your specific RTF into a string for simplicity.
            string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1053{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}{\*\generator Riched20 10.0.18362}\viewkind4\uc1 \pard\b\f0\fs17 test\b0  \ul test\ulnone\par}";
            private void Form1_Load(object sender, EventArgs e)
            {
                string rptrn = @"\\b(?=(\\|\{|\}|\s))";
                string newRTF = System.Text.RegularExpressions.Regex.Replace(rtf, rptrn, @"<b>");
                rptrn = @"\\b0(?=(\\|\{|\}|\s))";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"</b>");
                rptrn = @"\\ul(?=(\\|\{|\}|\s))";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"<u>");
                rptrn = @"\\ulnone(?=(\\|\{|\}|\s))";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"</u>");
                Clipboard.SetText(newRTF);
            }

    Corrected via positive lookahead assertion.
    Friday, February 28, 2020 6:13 AM
  • Im doing it this way when i get that problem..
                string rptrn = @"\\b(\\|\{|\}|\s)";
                string newRTF = System.Text.RegularExpressions.Regex.Replace(MallTextRichtxt.Rtf, rptrn, @"<b>");
                rptrn = @"\\b0(\\|\{|\}|\s)";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"</b>");
                rptrn = @"\\ul(\\|\{|\}|\s)";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"<u>");
                rptrn = @"\\ulnone(\\|\{|\}|\s)";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"</u>");
                MallTextRichtxt.Rtf = newRTF;

    Am i missing something..?
    Friday, February 28, 2020 6:51 AM
  • Im doing it this way when i get that problem..
                string rptrn = @"\\b(\\|\{|\}|\s)";
                string newRTF = System.Text.RegularExpressions.Regex.Replace(MallTextRichtxt.Rtf, rptrn, @"<b>");
                rptrn = @"\\b0(\\|\{|\}|\s)";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"</b>");
                rptrn = @"\\ul(\\|\{|\}|\s)";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"<u>");
                rptrn = @"\\ulnone(\\|\{|\}|\s)";
                newRTF = System.Text.RegularExpressions.Regex.Replace(newRTF, rptrn, @"</u>");
                MallTextRichtxt.Rtf = newRTF;

    Am i missing something..?

    Use the code from my most recent post.  Note that where parentheses start there is now a ?= followed by a new parenthesed group.

    To clarify the regex patterns: 

    \b and \u actually mean something in regex.  It parses.  So the \ has to be escaped.  Hence \\b and \\u - this is the text to find in the Regex.Replace that follows. 

    The code from my first post with regex shows how to use a GROUPING mechanism, such as by enclosing in round brackets aka parentheses.  The code you quoted (and I'm quoting again above) is that code.

    In the post with code that I made after that, the pattern  is now \b(?=(\|{|}|\s)) - in this example I'm leaving out the escapes for clarity.  The ?= means to check the next character but don't consume it.  Unfortunately this means the whitespace is no longer replaced in cases where your RTF code is '\b ' - but it does mean the \ is no longer replaced.

    I suppose to fix the whitespace issue we'd want to do something like @"\\b\s*(?=(\\|\{|\}))" but I haven't tested it this time.

    The fact that I've already explained about the trailing \ getting consumed and you're still confused about what f0 is and why it's showing up tells me that you did not review any amount of the RTF specification as I suggested, nor did you study the actual RTF coming out of your textbox before or after replacement.  I don't have much GAF for helping folks who refuse to do any learning in favor of wanting everything done for them.


    Friday, February 28, 2020 7:47 AM
  • Okey.. Sorry to hear that you feel so.. Im really trying to learn.. My last question is Why the text in start gets bigger.. Im not gonna ask more questions about this after that
    Friday, February 28, 2020 9:13 AM
  • Okey.. Sorry to hear that you feel so.. Im really trying to learn.. My last question is Why the text in start gets bigger.. Im not gonna ask more questions about this after that

    You shouldn't stop asking questions, but you really need to read up on the Rich Text Format Specification itself and you should really study the text that comes out of your Richtextbox.Rtf property in order to understand what you're working with

    I'm not mad.  Again, I'm saying:  You need to understand what you're working WITH - think of the RTF-encoded string like a piece of wood you're trying to cut into a table leg.  Asking on MSDN where f0 comes from when it's part of the RTF encoding scheme is a bit like asking on a carpentry forum why there's all this yellow dust flying around when you use a belt sander.  In this case the first regex I gave you is taking too much material off - the f0 was always there, it just had a / in front of it so the Richtextbox was treating it as an encoding instruction, then the regex replace took away the / so the Richtextbox was treating it like plaintext and it appeared in the output instead of being consumed by the formatting engine.

    As far as the question about "the text in start getting bigger," I just don't get the question.  Can you clarify it?


    Friday, February 28, 2020 4:57 PM
  • Im going to think about that when im working with this. I will look att it with a bigger perspective. About the last question i ment the picture above in this thread. the first <b> tag is getting bigger than the other words.. Its still working for me but its just annoying :)
    Saturday, February 29, 2020 10:37 PM
  • Likely its position in the encoding string.  Ideally you'd want to move it out past the space that terminates an RTF-encoding block.  Also possibly because the formatting effect of /f0 is lost when the / is removed.  I'll go ahead and just drum up a real parsing engine.  At least it's nice to know RegEx was insufficient like I said in the first place :-P
    Saturday, February 29, 2020 10:56 PM
  • See if you can break this one.
            /*
                Designed with considerations taken from:  http://www.biblioscape.com/rtf15_spec.htm
            */
    
            struct rtfTagStruct
            {
                int startOffset;
                string text;
    
                public rtfTagStruct(int offset, string tag)
                {
                    this.startOffset = offset;
                    this.text = tag;
                }
    
                public static List<rtfTagStruct> GetRTFTags(string rtf)
                {
                    List<rtfTagStruct> r = new List<rtfTagStruct>();
    
                    int tagoffset = -1;
                    string tagtxt = string.Empty;
                    // We're going to ignore everything in { or } except for 
                    // the possibility of replacement-terms occurring inside them.
                    for (int i = 0; i < rtf.Length; i++)
                    {
                        if (tagoffset < 0)
                        {
                            // When not processing an open tag, don't care about anything but a \
    
                            if (rtf[i] == (char)0x5c) // The backslash begins an RTF control code
                            {
                                tagoffset = i;
                                tagtxt += rtf[i].ToString();
                            }
                        }
                        else
                        {
                            // When processing an open tag, we must consider \ (opening a new tag) and { (opening a group) and } (closing a group)
                            // and also a plain whitespace character as tag terminators.  In the case of whitespace we don't want to consume it 
                            // unless we're doing a replace operation.
    
                            if (
                                (rtf[i] == (char)0x7b) || // {
                                (rtf[i] == (char)0x7d) // }
                               )
                            {
                                r.Add(new rtfTagStruct(tagoffset,tagtxt));
                                // Reset tag offset and text to nothing
                                tagoffset = -1;
                                tagtxt = string.Empty;
                            }
                            else if (rtf[i] == (char)0x5c) // Backslash
                            {
                                r.Add(new rtfTagStruct(tagoffset, tagtxt));
                                // Reset tag offset to the new/opening tag's offset and reset text to the opening \ char
                                tagoffset = i;
                                tagtxt = rtf[i].ToString();
                            }
                            else if (rtf[i] == (char)0x20) // Whitespace
                            {
                                tagtxt += rtf[i].ToString();
                                r.Add(new rtfTagStruct(tagoffset, tagtxt));
                                // Reset tag offset and text to nothing
                                tagoffset = -1;
                                tagtxt = string.Empty;
                            }
                            else
                            {
                                // Anything that isn't a terminating character gets added to the tag text
                                tagtxt += rtf[i].ToString();
                            }
                        }
                    }
    
                    return r;
                }
    
                public static string ReplaceAllTags(string inRTFString, string findRTFTag, string replaceWithPlainText)
                {
                    // This is a special case because the OP wants to replace RTF control codes with HTML formatting 
                    // instructions instead.  The HTML has to be treated as plaintext, and to do that we should really 
                    // be removing the RTF Control Code from any chain of codes it might be embedded in.
                    // If you were replacing an RTF Control Code with another RTF Control Code you would never want this
                    // behavior.
    
                    string r = string.Empty;
                    int 
                        input_cursor = 0, 
                        copy_length = 0
                    ;
    
                    List<rtfTagStruct> tags = GetRTFTags(inRTFString);
                    foreach (rtfTagStruct tag in tags)
                    {
                        // Test if the tag name additionally trimmed of leading/trailing whitespace
                        // matches the rtf control code specified in findRTFTag
                        if (tag.text.Trim().Equals(findRTFTag))
                        {
                            copy_length = tag.startOffset - input_cursor;
                            // Copy the original string from our last cursor position up to our current 
                            // start offset.
                            r += inRTFString.Substring(input_cursor, copy_length);
                            // Jump the cursor into the RTF input over what we just copied and also 
                            // over the tag we're examining
                            input_cursor += copy_length + tag.text.Length;
    
                            if (tag.text.Substring(tag.text.Length - 1) != @" ")
                            {
                                // If the tag text isn't terminated by a whitespace character then 
                                // it's part of an encoding-instruction chain and must be moved
    
                                // Grab a new cursor for seeking whitespace into the input RTF
                                int seek_cursor = input_cursor;
                                do
                                {
                                    // We aren't doing anything but finding either the end of the string or else 
                                    // the first whitespace following a chain of RTF encodes
                                    seek_cursor++;
                                } while ((seek_cursor < inRTFString.Length) && (inRTFString[seek_cursor] != ' '));
    
                                // When we hit the end of string before whitespace, we'll just add on the 
                                // replacement text, but we'll do it later - there's no need to if/account 
                                // for the eventuality apart from making sure we don't get an 
                                // IndexOutOfBoundsException while testing for whitespace.
    
                                // Instead just test if we hit a space or not
                                if ((seek_cursor < inRTFString.Length) && inRTFString[seek_cursor] == ' ')
                                {
                                    // When we hit whitespace, copy the intervening codechain
    
                                    // Advance the seek_cursor by 1 to make sure we include the terminating 
                                    // whitespace
                                    seek_cursor++;
    
                                    copy_length = seek_cursor - input_cursor;
                                    r += inRTFString.Substring(input_cursor, copy_length);
                                    // Advance the real cursor up to where we just copied, which includes the 
                                    // whitespace terminating the control code chain
                                    input_cursor = seek_cursor;
    
                                    // Don't add the replacement value to the return value here.  We'll add on 
                                    // the replacement value regardless of all eventualities, but later.
    
                                }
    
                                // There's no need for any else here.  If we didn't locate a space then we'll 
                                // blindly add on the replacement text anyway, just later.
                            }
    
                            // There's no need for any else here either.  If the original tag wasn't individually 
                            // terminated by whitespace then we've already accounted for it and copied the appropriate 
                            // input substring into the return value, so all eventualities are accounted for.
    
                            // Shoehorn in the replacement value.  The return value is in a state that's ready to receive 
                            // this data addon
                            r += replaceWithPlainText;
                        }
                    }
    
                    // If all tags/replacements are processed and we haven't hit the end of the input string yet, 
                    // make sure it gets opied in
                    if (input_cursor < inRTFString.Length)
                    {
                        copy_length = inRTFString.Length - input_cursor;
                        r += inRTFString.Substring(input_cursor, copy_length);
                    }
    
                    return r;
                }
            }
    
            // I'm just hard-setting your specific RTF into a string for simplicity.
            string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1053{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}{\*\generator Riched20 10.0.18362}\viewkind4\uc1 \pard\b\f0\fs17 test\b0  \ul test\ulnone\par}";
            private void Form1_Load(object sender, EventArgs e)
            {
                // About as kludgy performance-wise as regular expressions would be.  The real problem here is that RTF's data format 
                // is is identical to XML in the first place - open tag, close tag, group tags, parent tag, child tag.  This kind of 
                // thing is mildly retarded in computing, but easy for humans to read and understand.
    
                // Note that here and ONLY here do we take the original RTF, such as pulled from a Richtextbox.
                string newRTF = rtfTagStruct.ReplaceAllTags(rtf, @"\b", @"<b>");
                // Note that after the first time, we're replacing back into the newRTF, never touching the 
                // original RTF again.
                newRTF = rtfTagStruct.ReplaceAllTags(newRTF, @"\b0", @"</b>");
                newRTF = rtfTagStruct.ReplaceAllTags(newRTF, @"\ul", @"<u>");
                newRTF = rtfTagStruct.ReplaceAllTags(newRTF, @"\ulnone", @"</u>");
    
    
                this.richTextBox1.Rtf = newRTF;
            }

    Sunday, March 1, 2020 12:04 AM
  • Okey! Thank you for all the help and the patience!
    Sunday, March 1, 2020 8:43 PM
  • Did this last codebit end up working for you, Carlo Goretti?

    Before you can learn anything new you have to learn that there's stuff you don't know.

    Sunday, March 8, 2020 4:04 PM