欢迎来到工商注册核名查询系统!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

C#使用RichTextBox实现替换文字及改变字体颜 功能示例

来源:本站原创|时间:2022-11-25|栏目:C#教程|

本文实例讲述了C#使用RichTextBox实现替换文字及改变字体颜 功能。分享给大家供大家参考,具体如下:

替换文字

private void GenerateEntity()
{
  try
  {
    string result = ChangeWords("specific content...");
    txtContent.Text = result;
    ChangeColor();
  }
  catch (Exception ex)
  {
    MessageBox.Show("类生成失败!错误信息:" + ex.Message);
  }
}
private string ChangeWords(string content)
{
  //先替换"nvarchar"、"varchar"、"nchar",再替换"char"
  //不然"nvarchar"、"varchar"、"nchar"就会被替换为
  //nvarstring"、"varstring"、"nstring"不能进行原有规则替换
  string result = Regex.Replace(content, "nvarchar", "string");
  //进行下一步替换的时一定要以上一步替换的返回结果为数据源而不是content
  //因为content值没有改变
  result = Regex.Replace(result, "varchar", "string");
  result = Regex.Replace(result, "nchar", "string");
  result = Regex.Replace(result, "char", "string");
  result = Regex.Replace(result, "tinyint", "int");
  result = Regex.Replace(result, "smallint", "int");
  result = Regex.Replace(result, "bigint", "int");
  result = Regex.Replace(result, "datetime", "DateTime");
  return result;
}

改变字体颜

要改变字体颜 一定要使用RichTextBox,普通的文本框不能实现为某些特殊文字添加颜 的功能。

private void ChangeColor()
{
  txtContent.SelectionStart = 0;
  txtContent.SelectionLength = txtContent.Text.Length;
  txtContent.SelectionColor = Color.Black;
  //列注释不为空时,改变列注释颜   
  if (listDescription.Count > 0)
  {
    ChangeKeyColor(listDescription, Color.Green);
  }
  ChangeKeyColor("namespace", Color.Blue);
  ChangeKeyColor("public", Color.Blue);
  ChangeKeyColor("class", Color.Blue);
  ChangeKeyColor("/// <summary>",Color.Gray);
  ChangeKeyColor("///", Color.Gray);
  ChangeKeyColor("/// </summary>", Color.Gray);
  ChangeKeyColor("int", Color.Blue);
  ChangeKeyColor("double", Color.Blue);
  ChangeKeyColor("float", Color.Blue);
  ChangeKeyColor("char", Color.Blue);
  ChangeKeyColor("string", Color.Blue);
  ChangeKeyColor("bool", Color.Blue);
  ChangeKeyColor("decimal", Color.Blue);
  ChangeKeyColor("enum", Color.Blue);
  ChangeKeyColor("const", Color.Blue);
  ChangeKeyColor("struct", Color.Blue);
  ChangeKeyColor("DateTime", Color.CadetBlue);
  ChangeKeyColor("get",Color.Blue);
  ChangeKeyColor("set", Color.Blue);
}
public void ChangeKeyColor(string key, Color color)
{
  Regex regex = new Regex(key);
  //找出内容中所有的要替换的关键字
  MatchCollection collection = regex.Matches(txtContent.Text);
  //对所有的要替换颜   的关键字逐个替换颜   
  foreach (Match match in collection)
  {
    //开始位置、长度、颜   缺一不可
    txtContent.SelectionStart = match.Index;
    txtContent.SelectionLength = key.Length;
    txtContent.SelectionColor = color;
  }
}
public void ChangeKeyColor(List<string> list, Color color)
{
  foreach (string str in list)
  {
    ChangeKeyColor(str, color);
  }
}

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#常见控件用法教程》、《C#窗体操作技巧汇总》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》

希望本文所述对大家C#程序设计有所帮助。

更多C#教程

您可能感兴趣的文章

阅读排行

本栏相关

随机阅读

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 工商注册核名查询系统 版权所有