Guest User

Guest User

  • Tech Writer
  • 48
  • 11.1k

RichTextBox Find and SelectionFont not working.

Jun 27 2019 10:09 AM
Using: Windows 10, C# on Visual Studio 2017, WinForms.
I am attempting to format each line of a RichTextBox seperately, but have had some issues. Here is my current code method to create a "bold" a line:
  1. // Adds a single line to the top of the RichTextBox every time a button is pressed.  
  2. messageCounter++;  
  3. DateTime dt = DateTime.Now;  
  4. string time = dt.ToString("hh:mm tt");  
  5. string message = "A BOLD message.";  
  6. string statusText = "Message #" + messageCounter + "\t" + time + "\t\t" + message + "\n";  
  7. StatusRichTextBox.Text = StatusRichTextBox.Text.Insert(0, statusText);  
  8. StatusRichTextBox.Find(statusText);  
  9. StatusRichTextBox.SelectionFont = new System.Drawing.Font(StatusRichTextBox.Font, System.Drawing.FontStyle.Bold);  
  10.    
  11. // The following part is for viewing the found string length and will be removed later.  
  12. string howBig = StatusRichTextBox.SelectionLength.ToString();  
  13. StatusRichTextBox.Text = StatusRichTextBox.Text.Insert(0, howBig);   
My problem is that the Find will find the message variable, but not the time one or the statusText. And even when it finds the message variable, it won't make it bold, however I was able to make it bold in the past, but I forgot what I changed to do it. 
 
If anyone could help me figure out why find isn't working for the two strings, that would be great. In addition, any hints on how to get the bold to work or even how to change the color of the selected text would be most appreciated.
 
Thank you. 

Answers (1)