0
我有一个winform应用程序,我需要提供查找和突出显示用户输入文本中所有数字的功能。为此,我将一个wpf richtextbox添加到表单上的元素主机。点击一个按钮,我阅读文本框中的文本以查找所有数字。问题是仅数字的最后一个实例被突出显示,而不是所有实例。对于前 - 在文中 - “订单是12个百吉饼,地址是13456 Lame st。”只有13456被突出显示。代码如下:在wpf richtextbox中查找文本中的所有数字实例
private void btnSave_Click(object sender, EventArgs e)
{
var wpfTextBox = (System.Windows.Controls.RichTextBox)elementHost1.Child;
Regex reg = new Regex("[0-9 -()+]+$");
var start = wpfTextBox.Document.ContentStart;
while (start != null && start.CompareTo(wpfTextBox.Document.ContentEnd) < 0)
{
if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
var match = reg.Match(start.GetTextInRun(LogicalDirection.Forward));
var textrange = new TextRange(start.GetPositionAtOffset(match.Index, LogicalDirection.Forward), start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward));
textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
}
start = start.GetNextContextPosition(LogicalDirection.Forward);
}
}
谢谢你的时间!
谢谢你指出 - 我在另一个应用程序中使用了相同的正则表达式,它适用于我正在寻找的数字模式,所以我不认为正则表达式可能是一个问题。 – user6166859