C#使用RichTextBox實現(xiàn)替換文字及改變字體顏色功能示例
本文實例講述了C#使用RichTextBox實現(xiàn)替換文字及改變字體顏色功能。分享給大家供大家參考,具體如下:
替換文字
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"不能進行原有規(guī)則替換
string result = Regex.Replace(content, "nvarchar", "string");
//進行下一步替換的時一定要以上一步替換的返回結(jié)果為數(shù)據(jù)源而不是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,普通的文本框不能實現(xiàn)為某些特殊文字添加顏色的功能。
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("http:/// <summary>",Color.Gray);
ChangeKeyColor("http:///", Color.Gray);
ChangeKeyColor("http:/// </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);
//找出內(nèi)容中所有的要替換的關(guān)鍵字
MatchCollection collection = regex.Matches(txtContent.Text);
//對所有的要替換顏色的關(guān)鍵字逐個替換顏色
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);
}
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《C#窗體操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
將ocx文件轉(zhuǎn)換成C#程序引用的DLL文件的辦法
將ocx文件轉(zhuǎn)換成C#程序引用的DLL文件的辦法,需要的朋友可以參考一下2013-03-03
利用Distinct()內(nèi)置方法對List集合的去重問題詳解
這篇文章主要給大家介紹了關(guān)于利用Distinct()內(nèi)置方法對List集合的去重問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-06-06
C#進行圖像處理的常見方法(Bitmap,BitmapData,IntPtr)使用詳解
這篇文章主要為大家詳細介紹了C#進行圖像處理的幾個常見方法(Bitmap,BitmapData,IntPtr)具體使用,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2024-01-01
WinFrom中l(wèi)abel背景透明的實現(xiàn)方法
這篇文章主要介紹了WinFrom中l(wèi)abel背景透明的實現(xiàn)方法,方法簡單實用,是C#程序設(shè)計中非常實用的技巧,需要的朋友可以參考下2014-09-09

