using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace RichTextBox描红指定内容
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.btnColor.Click += new EventHandler(BtnColor_Click);
this.btnStart.Click += new EventHandler(BtnStart_Click);
this.textBox2.Click += new EventHandler(TextBox_Click);
}
/// <summary>
/// 选择颜色后,将按钮背景色设置成选中的色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnColor_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
btnColor.BackColor = colorDialog1.Color;
}
}
private int temp = 0;
private void BtnStart_Click(object sender, EventArgs e)
{
string key = textBox1.Text.Trim();
string content = richTextBox1.Text.Trim();
if (richTextBox1.Text.Trim() != "")
{
Regex re = new Regex(key);
MatchCollection m = re.Matches(richTextBox1.Text, temp);
foreach (Match sm in m)
{
richTextBox1.Select(sm.Index, key.Length);
richTextBox1.SelectionColor = colorDialog1.Color;
richTextBox1.SelectionFont = fontDialog1.Font;
}
}
}
private void TextBox_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = fontDialog1.Font.ToString();
}
}
}
}
RichTextBox中用指定字体和颜色高亮显示指定字符
最新推荐文章于 2024-02-26 10:08:17 发布