SoFunction
Updated on 2025-03-07

C# Use RichTextBox to implement the function of replacing text and changing font color

This article describes the use of RichTextBox to implement the functions of replacing text and changing font color in C#. Share it for your reference, as follows:

Replace text

private void GenerateEntity()
{
  try
  {
    string result = ChangeWords("specific content...");
     = result;
    ChangeColor();
  }
  catch (Exception ex)
  {
    ("Class generation failed! Error message:" + );
  }
}
private string ChangeWords(string content)
{
  //Replace "nvarchar", "varchar", "nchar", and then replace "char"  //Otherwise "nvarchar", "varchar", and "nchar" will be replaced with  //nvarstring", "varstring", and "nstring" cannot be replaced by the original rules  string result = (content, "nvarchar", "string");
  // When performing the next step of replacement, be sure to return the result of the above step of replacement as the data source instead of the content  //Because the content value has not changed  result = (result, "varchar", "string");
  result = (result, "nchar", "string");
  result = (result, "char", "string");
  result = (result, "tinyint", "int");
  result = (result, "smallint", "int");
  result = (result, "bigint", "int");
  result = (result, "datetime", "DateTime");
  return result;
}

Change the font color

To change the font color, you must use RichTextBox. Ordinary text boxes cannot implement the function of adding colors to certain special texts.

private void ChangeColor()
{
   = 0;
   = ;
   = ;
  // When the column comment is not empty, change the color of the column comment  if ( > 0)
  {
    ChangeKeyColor(listDescription, );
  }
  ChangeKeyColor("namespace", );
  ChangeKeyColor("public", );
  ChangeKeyColor("class", );
  ChangeKeyColor("/// <summary>",);
  ChangeKeyColor("///", );
  ChangeKeyColor("/// </summary>", );
  ChangeKeyColor("int", );
  ChangeKeyColor("double", );
  ChangeKeyColor("float", );
  ChangeKeyColor("char", );
  ChangeKeyColor("string", );
  ChangeKeyColor("bool", );
  ChangeKeyColor("decimal", );
  ChangeKeyColor("enum", );
  ChangeKeyColor("const", );
  ChangeKeyColor("struct", );
  ChangeKeyColor("DateTime", );
  ChangeKeyColor("get",);
  ChangeKeyColor("set", );
}
public void ChangeKeyColor(string key, Color color)
{
  Regex regex = new Regex(key);
  //Find out all the keywords to replace in the content  MatchCollection collection = ();
  //Replace colors one by one for all keywords to replace colors  foreach (Match match in collection)
  {
    //The start position, length and color are indispensable     = ;
     = ;
     = color;
  }
}
public void ChangeKeyColor(List<string> list, Color color)
{
  foreach (string str in list)
  {
    ChangeKeyColor(str, color);
  }
}

For more information about C# related content, please check out the topic of this site:Tutorial on the usage of common C# controls》、《Summary of C# form operation skills》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.