C#使用RichTextBox实现替换文字及改变字体颜色功能示例
作者:changuncle 时间:2023-07-04 23:04:18
本文实例讲述了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#程序设计有所帮助。
来源:https://blog.csdn.net/xiaouncle/article/details/52243346
标签:C#,RichTextBox
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#中的时间显示格式(12小时制VS24小时制)
2021-10-19 12:58:41
Java 提取照片的EXIF信息批量重命名
2023-10-05 14:11:28
![](https://img.aspxhome.com/file/2023/8/80838_0s.png)
Jpa Specification如何实现and和or同时使用查询
2023-10-17 20:49:38
Android TextWatcher监控EditText中的输入内容并限制其个数
2022-08-18 13:27:41
Android中 自定义数据绑定适配器BaseAdapter的方法
2022-02-02 21:12:50
深入多线程之:双向信号与竞赛的用法分析
2022-02-17 06:54:49
浅谈Android面向切面编程(AOP)
2022-04-03 15:14:41
![](https://img.aspxhome.com/file/2023/3/137513_0s.png)
在Java的Struts中判断是否调用AJAX及用拦截 器对其优化
2023-11-25 00:40:40
![](https://img.aspxhome.com/file/2023/0/59980_0s.png)
Java OpenCV4.0.0实现实时人脸识别
2023-11-16 07:29:14
![](https://img.aspxhome.com/file/2023/9/61779_0s.jpg)
c#根据文件类型获取相关类型图标的方法代码
2022-07-30 10:56:41
Diycode开源项目实例搭建上拉加载和下拉刷新的Fragment
2023-06-17 01:13:03
![](https://img.aspxhome.com/file/2023/4/138094_0s.gif)
Java深入了解数据结构之二叉搜索树增 插 删 创详解
2023-02-14 08:08:00
![](https://img.aspxhome.com/file/2023/4/60794_0s.png)
Android高级xml布局之输入框EditText设计
2022-09-24 14:25:56
![](https://img.aspxhome.com/file/2023/1/137531_0s.jpg)
如何关闭 IDEA 自动更新
2021-11-04 17:17:30
![](https://img.aspxhome.com/file/2023/7/62107_0s.png)
SpringCloud学习笔记之Feign远程调用
2023-08-15 09:57:01
![](https://img.aspxhome.com/file/2023/1/119111_0s.png)
Android TextView文本控件介绍
2023-08-29 10:13:53
不看后悔!揭秘游戏服务器开发
2023-08-22 16:20:30
![](https://img.aspxhome.com/file/2023/6/58306_0s.png)
Android 连接蓝牙扫码器无输入框的实现
2023-05-06 02:37:34
基于Java8 函数式接口理解及测试
2021-06-19 21:54:35
java实现滑动验证解锁
2023-06-02 12:16:36
![](https://img.aspxhome.com/file/2023/4/74894_0s.jpg)