Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I format a richTextBox?

How can I format a richTextBox?

Scheduled Pinned Locked Moved C#
questionhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AngryC
    wrote on last edited by
    #1

    Hello, Can you please tell me how can I format a richTextBox using bbcode! How can I search in the whole richTextBox content and put every occurance of text between [ b ] & [ /b ] in bold. Bla Bla Bla Bla Bla Bla [ b ]IN BOLD1[ /b ] Bla Bla Bla Bla Bla Bla [ b ]IN BOLD2[ /b ] Bla [ i ]ITALIC[ /i ] Your help would be greatly appreciated.

    M 1 Reply Last reply
    0
    • A AngryC

      Hello, Can you please tell me how can I format a richTextBox using bbcode! How can I search in the whole richTextBox content and put every occurance of text between [ b ] & [ /b ] in bold. Bla Bla Bla Bla Bla Bla [ b ]IN BOLD1[ /b ] Bla Bla Bla Bla Bla Bla [ b ]IN BOLD2[ /b ] Bla [ i ]ITALIC[ /i ] Your help would be greatly appreciated.

      M Offline
      M Offline
      mikanu
      wrote on last edited by
      #2

      Well, if I understand correctly what you're trying to do should be as easy as parsing the text in the richTextBox control and looking for the <b> and <i> tags. The easiest (not the most efficient) way to do this is to do a two-pass parse of the text. First for the BOLD tags and second for the ITALIC text. Whenever you run into an open BOLD tag, increment boldCount variable and store the position. Then remove the tag from the text. Whenever you run into a close BOLD tag, decrement boldCount, and remove the colsing tag from the text. When boldCount = 0, select the text starting at the recorded position and then set it's bold property to TRUE. Keep going until the end of the text. Repeat for the ITALIC tags. Here is an example that does bold tags. Modifying this code to do italic text is trivial so it wasn't included here.. Hope this helps and don't forget to visit digitalGetto

          private void ParseBoldText(RichTextBox tb)
          {
              int boldCount = 0;
              int lastBoldStartPosition = 0;
              int currentPosition = 0;
      
              while (currentPosition < tb.Text.Length - 3)
              {
                  if (tb.Text.Substring(currentPosition, 3) == "<b>")
                  {
                      if(boldCount == 0)
                          lastBoldStartPosition = currentPosition;
                      boldCount++;
                  }
                  if (tb.Text.Substring(currentPosition, 4) == "</b>")
                  {
                      boldCount--;
                      if (boldCount == 0)
                      {
                          tb.Select(lastBoldStartPosition + 3, currentPosition - lastBoldStartPosition - 3);
                          tb.SelectionFont = new Font(tb.SelectionFont.FontFamily, tb.SelectionFont.Size, tb.SelectionFont.Style | FontStyle.Bold);
                      }
                  }
                  currentPosition += 1;
              }
                                     
              //clean the <B> tags, carefulf to use the RTF property instead of TEXT property !!!!
              tb.Rtf = tb.Rtf.Replace("<b>", "");
              tb.Rtf = tb.Rtf.Replace("</b>", "");
          }
      

      ---- www.digitalGetto.com -- modified at 18:07 Tuesday 20th June, 2006

      A 2 Replies Last reply
      0
      • M mikanu

        Well, if I understand correctly what you're trying to do should be as easy as parsing the text in the richTextBox control and looking for the <b> and <i> tags. The easiest (not the most efficient) way to do this is to do a two-pass parse of the text. First for the BOLD tags and second for the ITALIC text. Whenever you run into an open BOLD tag, increment boldCount variable and store the position. Then remove the tag from the text. Whenever you run into a close BOLD tag, decrement boldCount, and remove the colsing tag from the text. When boldCount = 0, select the text starting at the recorded position and then set it's bold property to TRUE. Keep going until the end of the text. Repeat for the ITALIC tags. Here is an example that does bold tags. Modifying this code to do italic text is trivial so it wasn't included here.. Hope this helps and don't forget to visit digitalGetto

            private void ParseBoldText(RichTextBox tb)
            {
                int boldCount = 0;
                int lastBoldStartPosition = 0;
                int currentPosition = 0;
        
                while (currentPosition < tb.Text.Length - 3)
                {
                    if (tb.Text.Substring(currentPosition, 3) == "<b>")
                    {
                        if(boldCount == 0)
                            lastBoldStartPosition = currentPosition;
                        boldCount++;
                    }
                    if (tb.Text.Substring(currentPosition, 4) == "</b>")
                    {
                        boldCount--;
                        if (boldCount == 0)
                        {
                            tb.Select(lastBoldStartPosition + 3, currentPosition - lastBoldStartPosition - 3);
                            tb.SelectionFont = new Font(tb.SelectionFont.FontFamily, tb.SelectionFont.Size, tb.SelectionFont.Style | FontStyle.Bold);
                        }
                    }
                    currentPosition += 1;
                }
                                       
                //clean the <B> tags, carefulf to use the RTF property instead of TEXT property !!!!
                tb.Rtf = tb.Rtf.Replace("<b>", "");
                tb.Rtf = tb.Rtf.Replace("</b>", "");
            }
        

        ---- www.digitalGetto.com -- modified at 18:07 Tuesday 20th June, 2006

        A Offline
        A Offline
        AngryC
        wrote on last edited by
        #3

        Thanks but this is not working: tb.Rtf = tb.Rtf.Replace("[b]", ""); tb.Rtf = tb.Rtf.Replace("[/b]", ""); I still have all the tags shown. -- modified at 19:28 Tuesday 20th June, 2006

        1 Reply Last reply
        0
        • M mikanu

          Well, if I understand correctly what you're trying to do should be as easy as parsing the text in the richTextBox control and looking for the <b> and <i> tags. The easiest (not the most efficient) way to do this is to do a two-pass parse of the text. First for the BOLD tags and second for the ITALIC text. Whenever you run into an open BOLD tag, increment boldCount variable and store the position. Then remove the tag from the text. Whenever you run into a close BOLD tag, decrement boldCount, and remove the colsing tag from the text. When boldCount = 0, select the text starting at the recorded position and then set it's bold property to TRUE. Keep going until the end of the text. Repeat for the ITALIC tags. Here is an example that does bold tags. Modifying this code to do italic text is trivial so it wasn't included here.. Hope this helps and don't forget to visit digitalGetto

              private void ParseBoldText(RichTextBox tb)
              {
                  int boldCount = 0;
                  int lastBoldStartPosition = 0;
                  int currentPosition = 0;
          
                  while (currentPosition < tb.Text.Length - 3)
                  {
                      if (tb.Text.Substring(currentPosition, 3) == "<b>")
                      {
                          if(boldCount == 0)
                              lastBoldStartPosition = currentPosition;
                          boldCount++;
                      }
                      if (tb.Text.Substring(currentPosition, 4) == "</b>")
                      {
                          boldCount--;
                          if (boldCount == 0)
                          {
                              tb.Select(lastBoldStartPosition + 3, currentPosition - lastBoldStartPosition - 3);
                              tb.SelectionFont = new Font(tb.SelectionFont.FontFamily, tb.SelectionFont.Size, tb.SelectionFont.Style | FontStyle.Bold);
                          }
                      }
                      currentPosition += 1;
                  }
                                         
                  //clean the <B> tags, carefulf to use the RTF property instead of TEXT property !!!!
                  tb.Rtf = tb.Rtf.Replace("<b>", "");
                  tb.Rtf = tb.Rtf.Replace("</b>", "");
              }
          

          ---- www.digitalGetto.com -- modified at 18:07 Tuesday 20th June, 2006

          A Offline
          A Offline
          AngryC
          wrote on last edited by
          #4

          Also you said: the easiest (not the most efficient) Can you give me an idea about the most efficient way?

          M 1 Reply Last reply
          0
          • A AngryC

            Also you said: the easiest (not the most efficient) Can you give me an idea about the most efficient way?

            M Offline
            M Offline
            mikanu
            wrote on last edited by
            #5

            Well, when I said the method was not the most efficient I meant that for large texts it will most likely be rather slow. Depending on your needs you may or may not want to put in the extra effort to look for/implement more efficient algorithms. The performance hit of the method shown is due to the fact that it reads each character in the text. That is time consuming. A first optimization is to combine looking for BOLD tags with looking for ITALIC tags. That is, to use the same loop to do both. That way, you cut the time approximately in half. Another option you may consider is using unsafe code (pointers) to loop through the text. Yet another possibility is to use regular expressions to perform the search and replace. As far as the other problem goes (the code not replacing the tags), that is rather strange. I've tested the code before posting it here and it worked. Can you give me more details as to how it fails?! ---- www.digitalGetto.com

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups