1. #1
    x BlueRobot's Avatar
    Join Date
    May 2013
    Location
    Minkowski Space
    Posts
    1,878

    How to Make Selected Text Bold [.NET]

    Evening everyone,

    I'm attempted to make a simple parser which takes a string in BBCode format, removes the tags and then makes the text bold using the RichTextBox functionality. The text is made bold, however, a non-bold string is produced first and then the bold string.

    Code:
    private void parseButton_Click(object sender, EventArgs e)
            {
                foreach(String line in richTextBox1.Lines)
                {
                    String[] subStrings = line.Split('-');
    
                    foreach(String subString in subStrings)
                    {
                        
                        if ((subString.StartsWith("['b]") && subString.EndsWith("['/b]")))
                        {
                            String noStartTag = "";
                            String noEndTag = "";
    
                            noStartTag = subString.Replace("['b]", "");
                            noEndTag = noStartTag.Replace("[/'b]", "");
                            richTextBox2.SelectedText = noEndTag;
                            richTextBox2.SelectionFont = new Font(richTextBox2.Font, FontStyle.Bold);
    
                        }
                    }
    I believe the issue may lie with the fact that the Text property of the RichTextBox control is being populated first, and then a new string is being altered?
    Machines Can Think

    We don't make mistakes; we just have happy accidents.


    • Ad Bot

      advertising
      Beep.

        
       

  2. #2
    AceInfinity's Avatar
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,725

    Re: How to Make Selected Text Bold [.NET]

    Why does the code split each line by '-'? I'm curious. Keep in mind if this gets expanded for further functionality, it wouldn't parse such bbcode as [no-parse] for instance like we have here. Aside from that, I did something like this a while back and the key is to change the font properties by creating a pair-map. I'm not sure how far you want to go with this, and I haven't pulled up an IDE yet, but is this only for bold tags so far?

    Don't iterate over the lines either... What if I did this:
    Code:
    [b]line 1
    line 2[/b]
    Last edited by AceInfinity; 08-15-2017 at 10:16 PM.
    Automation Programmer
    Microsoft MVP [2012 - 2018]

  3. #3
    AceInfinity's Avatar
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,725

    Re: How to Make Selected Text Bold [.NET]

    You can try something like this just for what you're trying to do:
    Code:
    string data = "[b]testing[b]12\r\n34[/b][/b] hello! [b]abcd[/b]";
    richTextBox1.Text = data;
    
    int n1 = 0;
    int n2 = richTextBox1.Text.Length;
    
    while ((n1 = richTextBox1.Text.IndexOf("[b]", n1)) != -1
        && (n2 = richTextBox1.Text.IndexOf("[/b]", n1)) != -1)
    {
        richTextBox1.Select(n1 + 3, n2 - n1 - 3);
        using (var boldFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold))
            richTextBox1.SelectionFont = boldFont;
    
        n1 = n2;
    }
    
    richTextBox1.Rtf = richTextBox1.Rtf.Replace("[b]", string.Empty);
    richTextBox1.Rtf = richTextBox1.Rtf.Replace("[/b]", string.Empty);
    Pretty minimal error checking.
    x BlueRobot and jcgriff2 say thanks for this.
    Automation Programmer
    Microsoft MVP [2012 - 2018]

  4. #4
    x BlueRobot's Avatar
    Join Date
    May 2013
    Location
    Minkowski Space
    Posts
    1,878

    Re: How to Make Selected Text Bold [.NET]

    Thanks Ace, that works perfectly!

    I've decided to start this project, as a form of motivation to learn the .NET framework.
    Machines Can Think

    We don't make mistakes; we just have happy accidents.

  5. #5
    AceInfinity's Avatar
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,725

    Re: How to Make Selected Text Bold [.NET]

    Implement the other 2 fundamental tags as well then for italics, and underline.
    Automation Programmer
    Microsoft MVP [2012 - 2018]

  6. #6
    x BlueRobot's Avatar
    Join Date
    May 2013
    Location
    Minkowski Space
    Posts
    1,878

    Re: How to Make Selected Text Bold [.NET]

    Done!
    AceInfinity says thanks for this.
    Machines Can Think

    We don't make mistakes; we just have happy accidents.

Similar Threads

  1. [SOLVED] Showing text in a rich text box on button press - Visual C#
    By Cookieman in forum Programming
    Replies: 6
    Last Post: 12-09-2012, 03:55 PM
  2. Replies: 0
    Last Post: 11-03-2012, 12:16 AM

Log in

Log in