2016-11-14 4 views
-2

Ich habe versucht, diese Methode zu verwenden, aber es ändert sich nur das erste Mal, dass textToMark in dem RichTextBox gezeigt wird, und ich will es jedes Mal ändern textToMark enthalten ist:Bold Teil der Zeichenfolge in C#

public static void ChangeTextcolor(string textToMark, Color color, RichTextBox richTextBox) 
    { 
     int startIndex = 0; 

     string text = richTextBox.Text; 
     startIndex = text.IndexOf(textToMark); 

     System.Drawing.Font newFont = new Font("Verdana", 10f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 178, false); 

     try 
     { 
      foreach (string line in richTextBox.Lines) 
      { 
       if (line.Contains(textToMark)) 
       { 
        richTextBox.Select(startIndex, textToMark.Length); 
        richTextBox.SelectionColor = color; 
        richTextBox.SelectionFont = newFont; 
       } 
      } 
     } 
     catch{ } 
    } 

Can Du hilfst mir? Danke

+3

Verwenden Sie eine RichTextBox. Die einfachere TextBox hat keine Möglichkeit, einzelne Wörter einzeln zu formatieren – Steve

+1

Verwenden Sie stattdessen eine RichTextBox. Und du hast nicht sehr gesucht. –

Antwort