Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Format text in Rich Text Box

I am using the following code to load a text file into a rich text box:

System.IO.StreamReader l_reader = new System.IO.StreamReader("c:\\test.txt");
           
 richTextBox1.Text = l_reader.ReadToEnd();

How do I format this text once it's displayed in the text box?  Say, I wanted to bold every instance of the word "Date", for example.

Thanks!
0
fattumsdad
Asked:
fattumsdad
  • 3
  • 2
1 Solution
 
Chester_M_RagelCommented:
Do you want something like this?

System.IO.StreamReader l_reader = new System.IO.StreamReader("c:\\test.txt");
richTextBox1.Text = l_reader.ReadToEnd();
int pos = -1;
for(int i=0;i<richTextBox1.Text.Length;i++)
{
  pos = richTextBox1.Find("Date",pos+1,RichTextBoxFinds.MatchCase);
  if(pos!=-1)
     richTextBox1.Select(pos,4);
   richTextBox1.SelectionFont = new Font(
   richTextBox1.SelectionFont.FontFamily,
   richTextBox1.SelectionFont.Size,
   FontStyle.Bold
  );
i = i+pos;
}
0
 
fattumsdadAuthor Commented:
Chester,

Yes!  However, it only bolds the first few instances of the word "Date", and highlights one as well.  The rest are the same.
0
 
Chester_M_RagelCommented:
Sorry for that non-perfect code, try with this one,

System.IO.StreamReader l_reader = new System.IO.StreamReader("c:\\a.txt");
richTextBox1.Text = l_reader.ReadToEnd();
int pos = -1;
for(int i=0;i<richTextBox1.Text.Length;i++)
{
      pos = richTextBox1.Find("Date",pos+1,RichTextBoxFinds.MatchCase);
      if(pos!=-1)
      {
            richTextBox1.Select(pos,4);
            richTextBox1.SelectionFont = new Font(
                            richTextBox1.SelectionFont.FontFamily,
                  richTextBox1.SelectionFont.Size,
                  FontStyle.Bold);
      }
}
0
 
Chester_M_RagelCommented:
Sorry for that non-perfect code, try with this one,

System.IO.StreamReader l_reader = new System.IO.StreamReader("c:\\test.txt");
richTextBox1.Text = l_reader.ReadToEnd();
int pos = -1;
for(int i=0;i<richTextBox1.Text.Length;i++)
{
      pos = richTextBox1.Find("Date",pos+1,RichTextBoxFinds.MatchCase);
      if(pos!=-1)
      {
            richTextBox1.Select(pos,4);
            richTextBox1.SelectionFont = new Font(
                            richTextBox1.SelectionFont.FontFamily,
                  richTextBox1.SelectionFont.Size,
                  FontStyle.Bold);
      }
}
0
 
fattumsdadAuthor Commented:
Chester,

Works GREAT!  Thank you so much for the help!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now