none
Formatting Font in RichTextbox (C#)

    Question

  • I'm having this line:

    richTextBox1.Text = richTextBox1.Text + "Patient's Name : " + patient.Name + "\n";
    Which fetches the name from my xml file. I want to Color & Bold the name part only in the richtextbox.

    But I'm not able to do that in my richtextbox display field.
    I'm not perfect, neither are u. But who's here to compare anyway, let's cherish the togetherness.
    Friday, July 01, 2011 7:19 AM

Answers

  • Try this code:

    Font fnt=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);
    string mystring=@"my first string";
    if (richTextBox1.Find(mystring)>0)
    {
        int my1stPosition=richTextBox1.Find(mystring);
        richTextBox1.SelectionStart=my1stPosition;
        richTextBox1.SelectionLength=mystring.Length;
        richTextBox1.SelectionFont=fnt;
        richTextBox1.SelectionColor=Color.CadetBlue;
    }
    
    



    Mitja
    • Marked as answer by gsvirdi Saturday, July 02, 2011 8:17 AM
    Saturday, July 02, 2011 5:14 AM

All replies

  • Hi,

    Pls have a look at the following link.

    http://msdn.microsoft.com/en-us/library/aa287595(v=vs.71).aspx


    Thank you and have great day! Vivek Kulkarni MCAD.net
    Saturday, July 02, 2011 4:44 AM
  • Try this code:

    Font fnt=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);
    string mystring=@"my first string";
    if (richTextBox1.Find(mystring)>0)
    {
        int my1stPosition=richTextBox1.Find(mystring);
        richTextBox1.SelectionStart=my1stPosition;
        richTextBox1.SelectionLength=mystring.Length;
        richTextBox1.SelectionFont=fnt;
        richTextBox1.SelectionColor=Color.CadetBlue;
    }
    
    



    Mitja
    • Marked as answer by gsvirdi Saturday, July 02, 2011 8:17 AM
    Saturday, July 02, 2011 5:14 AM
  • Forced/Required code in my application is:
    richTextBox1.Text = richTextBox1.Text + "Patient's Name : " + patient.Name + "\n";
    

    Hi,

    Pls have a look at the following link.

    http://msdn.microsoft.com/en-us/library/aa287595(v=vs.71).aspx


    Thank you and have great day! Vivek Kulkarni MCAD.net

    Thx vivek for the reply, but as u can see that the text line I'm talking about is a line which is inserted inbetween the other text displayed in the richtextbox, the escape sequence in Rtf will not work here. To make things worse I'm also combining the Text value + the xml value "patient.Name" for displaying in the richtextbox. I'm not able to figureout the proper escapse sequence in this condition.

    xml text value is being derived with xmlDoc.Descendants("patients") and the Name is the patient.Element("name").Value, Hope now u understand that I'm not using the static text value here. If aware then kindly tell me the escapse sequence in this case.

    richTextBox1.Text = richTextBox1.Text + "Patient's Name : " + patient.Name + "\n";


    I'm not perfect, neither are u. But who's here to compare anyway, let's cherish the togetherness.
    Saturday, July 02, 2011 6:38 AM
  • I'm not sure if this is the best way to do it, but it seems to work:

     

    Color red = Color.Red;
    richTextBox1.Rtf = string.Format(@"{{\rtf1\ansi{{\colortbl;\red{0}\green{1}\blue{2};}}Patient's Name: \cf1\b {3}\b0\cf0\par text on next line}}", 255, 0, 0, patient.Name);
    
    /* OR */
    richTextBox1.Rtf = @"{\rtf1\ansi{\colortbl;\red" + red.R + @"\green" + red.G + @"\blue" + red.B + @";}Patient's Name: \cf1\b " + patient.Name + @"\b0\cf0\par" + " text on next line" + "}";
    

    Here's one reference:
    http://www.biblioscape.com/rtf15_spec.htm

     


    Brad

    http://www.renchdev.com

    Saturday, July 02, 2011 6:54 AM
  • Perfect.

    Below my code I just made a small change in the code as:

    string

     

    mystring = @"" + patient.Name;

    Thx a lot Mitja.

     
    I'm not perfect, neither are u. But who's here to compare anyway, let's cherish the togetherness.
    Saturday, July 02, 2011 6:55 AM
  • I'm not sure if this is the best way to do it, but it seems to work:

     

    Color red = Color.Red;
    
    richTextBox1.Rtf = string.Format(@"{{\rtf1\ansi{{\colortbl;\red{0}\green{1}\blue{2};}}Patient's Name: \cf1\b {3}\b0\cf0\par text on next line}}", 255, 0, 0, patient.Name);
    
    
    
    /* OR */
    
    richTextBox1.Rtf = @"{\rtf1\ansi{\colortbl;\red" + red.R + @"\green" + red.G + @"\blue" + red.B + @";}Patient's Name: \cf1\b " + patient.Name + @"\b0\cf0\par" + " text on next line" + "}";
    
    

    Here's one reference:
    http://www.biblioscape.com/rtf15_spec.htm


    Brad http://www.renchdev.com


    thx for the reply Brad, but somehow it is not working for me. not able to see color/bold part of the string line

     


    I'm not perfect, neither are u. But who's here to compare anyway, let's cherish the togetherness.
    Saturday, July 02, 2011 8:36 AM
  • Not sure why it wouldn't be working for you.

    I'm using Visual Studio 2010.  In a Windows Forms application, I added a System.Windows.Forms.RichTextBox.  Then I just call this code when the Form first loads:

     

    Patient p = new Patient{ Name = "Some Patient" };
    Color red = Color.Red;
    richTextBox1.Rtf = string.Format(@"{{\rtf1\ansi{{\colortbl;\red{0}\green{1}\blue{2};}}Patient's Name: \cf1\b {3}\b0\cf0\par text on next line}}", red.R, red.G, red.B, p.Name);
    

     


    Screenshot here:

    Photobucket


    Brad

    http://www.renchdev.com
    Saturday, July 02, 2011 9:47 AM