在我的System.Windows.Controls.RichTextBox中,我想找到给定单词的TextRange.但是,在第一个找到的单词后,它没有给我正确的PositionAtOffset.第一个是正确的,然后对于下一个找到的单词,位置不正确.我使用正确的方法吗?
循环遍历listOfWords
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);
Run Code Online (Sandbox Code Playgroud) 我的richbox中有几个空行,我需要将它们从rich中删除。
我可以细化空白行,但不能删除它们。我该怎么做?我使用了这段代码,但没有用:
RichTextBox rtfBox = new RichTextBox();
rtfBox.Rtf = SOME NTEXT DATA;
int lineNumber = 0;
foreach (string a in rtfBox.Lines)
{
if (a == "")
{
int start_index = rtfBox.GetFirstCharIndexFromLine(lineNumber);
int countLineChar = rtfBox.Lines[lineNumber].Length;
// Eat new line chars
if (lineNumber < rtfBox.Lines.Length - 1)
{
countLineChar += rtfBox.GetFirstCharIndexFromLine(lineNumber + 1) -
((start_index + countLineChar - 1) + 1);
}
rtfBox.Text = rtfBox.Text.Remove(start_index, countLineChar);
}
else
lineNumber++;
}
Run Code Online (Sandbox Code Playgroud)
这条线rtfBox.Text = rtfBox.Text.Remove(start_index, countLineChar);不起作用。
谢谢
更新
谢谢大家,当 Richbox 内容只是文本时,您的建议很有用,但我的丰富内容中也有图像和表格。当我使用 …