WPF 中 RichTextBox 关键词着色
使用RichTextBox进行文字编辑但需要把其中的()括号显示成红色用户输入文字时,只要输入括号就显示为红色怎么做呢?...
使用RichTextBox 进行文字编辑
但需要把其中的 ( ) 括号显示成红色
用户输入文字时,只要输入括号就显示为红色
怎么做呢? 展开
但需要把其中的 ( ) 括号显示成红色
用户输入文字时,只要输入括号就显示为红色
怎么做呢? 展开
2个回答
展开全部
用户输入时加红色标记,就需要再每次按键都判段,下面这段代码倒是实现了,但是效率很低,你可以试着优化下
private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
//获取richTextBox文本内容
TextRange tr1 = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
tr1 .ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
string content = tr1.Text;
//正则表达式,获取匹配()的子字符串集信息
MatchCollection mc = Regex.Matches(content, @"\(|\)");
//遍历每个匹配子字符串
foreach (Match m in mc)
{
if (m.Success)
{
//获取子字符串起始点
TextPointer txStart = GetPointerFromCharOffset(m.Index, richTextBox1.Document.ContentStart, richTextBox1.Document);
//获取子字符串结束
TextPointer txEnd = GetPointerFromCharOffset(m.Index + m.Length, richTextBox1.Document.ContentStart, richTextBox1.Document);
//获取子字符串位置
TextRange tr = new TextRange(txStart, txEnd);
//应用样式
tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
}
}
}
//根据匹配字符串的索引获取TextPointer
public TextPointer GetPointerFromCharOffset(Int32 charOffset, TextPointer startPointer, FlowDocument document)
{
TextPointer navigator = startPointer;
if (charOffset == 0)
{
return navigator;
}
TextPointer nextPointer = navigator;
Int32 counter = 0;
while (nextPointer != null && counter <= charOffset)
{
if (nextPointer.CompareTo(document.ContentEnd) == 0)
{
return nextPointer;
}
nextPointer = nextPointer.GetNextInsertionPosition(LogicalDirection.Forward);
counter++;
}
return nextPointer;
}
private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
//获取richTextBox文本内容
TextRange tr1 = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
tr1 .ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
string content = tr1.Text;
//正则表达式,获取匹配()的子字符串集信息
MatchCollection mc = Regex.Matches(content, @"\(|\)");
//遍历每个匹配子字符串
foreach (Match m in mc)
{
if (m.Success)
{
//获取子字符串起始点
TextPointer txStart = GetPointerFromCharOffset(m.Index, richTextBox1.Document.ContentStart, richTextBox1.Document);
//获取子字符串结束
TextPointer txEnd = GetPointerFromCharOffset(m.Index + m.Length, richTextBox1.Document.ContentStart, richTextBox1.Document);
//获取子字符串位置
TextRange tr = new TextRange(txStart, txEnd);
//应用样式
tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
}
}
}
//根据匹配字符串的索引获取TextPointer
public TextPointer GetPointerFromCharOffset(Int32 charOffset, TextPointer startPointer, FlowDocument document)
{
TextPointer navigator = startPointer;
if (charOffset == 0)
{
return navigator;
}
TextPointer nextPointer = navigator;
Int32 counter = 0;
while (nextPointer != null && counter <= charOffset)
{
if (nextPointer.CompareTo(document.ContentEnd) == 0)
{
return nextPointer;
}
nextPointer = nextPointer.GetNextInsertionPosition(LogicalDirection.Forward);
counter++;
}
return nextPointer;
}
参考资料: http://social.msdn.microsoft.com/Forums/zh-SG/wpfzhchs/thread/89af5ff2-3918-4b97-8c53-95bfc37c4f0e
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询