C# 如何在RichTextBox中查找TextRange(在两个TextPointer之间)
在我的System.Windows.Controls.RichTextBox中,我想查找给定单词的TextRange。但是,它没有给我第一个找到的单词后的正确位置偏移。第一个单词是正确的,然后对于下一个找到的单词,位置是不正确的。我是否使用了正确的方法 循环浏览单词列表C# 如何在RichTextBox中查找TextRange(在两个TextPointer之间),c#,wpf,richtextbox,C#,Wpf,Richtextbox,在我的System.Windows.Controls.RichTextBox中,我想查找给定单词的TextRange。但是,它没有给我第一个找到的单词后的正确位置偏移。第一个单词是正确的,然后对于下一个找到的单词,位置是不正确的。我是否使用了正确的方法 循环浏览单词列表 Word= listOfWords[j].ToString(); startPos = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.
Word= listOfWords[j].ToString();
startPos = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text.IndexOf(Word.Trim());
leftPointer = textPointer.GetPositionAtOffset(startPos + 1, LogicalDirection.Forward);
rightPointer = textPointer.GetPositionAtOffset((startPos + 1 + Word.Length), LogicalDirection.Backward);
TextRange myRange= new TextRange(leftPointer, rightPointer);
此代码改编自at示例,可以从指定位置查找单词
TextRange FindWordFromPosition(TextPointer position, string word)
{
while (position != null)
{
if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
string textRun = position.GetTextInRun(LogicalDirection.Forward);
// Find the starting index of any substring that matches "word".
int indexInRun = textRun.IndexOf(word);
if (indexInRun >= 0)
{
TextPointer start = position.GetPositionAtOffset(indexInRun);
TextPointer end = start.GetPositionAtOffset(word.Length);
return new TextRange(start, end);
}
}
position = position.GetNextContextPosition(LogicalDirection.Forward);
}
// position will be null if "word" is not found.
return null;
}
然后您可以这样使用它:
string[] listOfWords = new string[] { "Word", "Text", "Etc", };
for (int j = 0; j < listOfWords.Length; j++)
{
string Word = listOfWords[j].ToString();
TextRange myRange = FindWordFromPosition(x_RichBox.Document.ContentStart, Word);
}
string[]listOfWords=新字符串[]{“Word”、“Text”、“Etc”,};
for(int j=0;j
我认为,它也应该起作用
这就像“查找下一个”列表中的任何项目
TextPointer FindWordsFromPosition(TextPointer position, IList<string> words)
{
var firstPosition = position;
while (position != null)
{
if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
string textRun = position.GetTextInRun(LogicalDirection.Forward);
var indexesInRun = new List<int>();
foreach (var word in words)
{
var index = textRun.IndexOf(word);
if (index == 0)
index = textRun.IndexOf(word, 1);
indexesInRun.Add(index);
}
indexesInRun = indexesInRun.Where(i => i != 0 && i != -1).OrderBy(i => i).ToList();
if (indexesInRun.Any())
{
position = position.GetPositionAtOffset(indexesInRun.First());
break;
}
return firstPosition;
}
else
position = position.GetNextContextPosition(LogicalDirection.Forward);
}
return position;
}
TextPointer FindWordsFromPosition(TextPointer位置,IList单词)
{
var firstPosition=位置;
while(位置!=null)
{
if(position.GetPointerContext(LogicalDirection.Forward)=TextPointerContext.Text)
{
字符串textRun=position.gettRun(LogicalDirection.Forward);
var indexesInRun=新列表();
foreach(单词中的var单词)
{
var index=textRun.IndexOf(word);
如果(索引==0)
index=textRun.IndexOf(单词,1);
indexesInRun.Add(索引);
}
indexesInRun=indexesInRun.Where(i=>i!=0&&i!=-1).OrderBy(i=>i.ToList();
if(indexesInRun.Any())
{
position=position.GetPositionAtOffset(indexesInRun.First());
打破
}
返回第一位置;
}
其他的
位置=位置.GetNextContextPosition(逻辑方向.Forward);
}
返回位置;
}
TextPointer.GetPositionAtOffset的偏移量是“符号”而不是字符,因此此代码一般不起作用。最有可能的情况是,字符串单词包含空格,或者是单词可以跨元素的非英语语言。