wpf 如何选择给定索引和长度的 RichTextBox 文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12065616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select RichTextBox text given an index and length
提问by l46kok
If you are only given an index and length (or EndIndex) of a certain text to select, how do you perform this in WPF version of RichTextBox?
如果只为您提供了要选择的某个文本的索引和长度(或 EndIndex),您如何在 RichTextBox 的 WPF 版本中执行此操作?
This is very doable in Textbox as you can call Textbox.Select(startIndex,Length) but I don't see anything equivalent in RTB.
这在 Textbox 中非常可行,因为您可以调用 Textbox.Select(startIndex,Length) 但我在 RTB 中看不到任何等效项。
Edit: I have found the answer to making a selection
编辑:我找到了做出选择的答案
internal string Select(RichTextBox rtb, int index, int length)
{
TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
if (textRange.Text.Length >= (index + length))
{
TextPointer start = textRange.Start.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer end = textRange.Start.GetPositionAtOffset(index + length, LogicalDirection.Backward);
rtb.Selection.Select(start, end);
}
return rtb.Selection.Text;
}
However, when I try to highlight the line after the selection has been made:
但是,当我尝试在选择后突出显示该行时:
rtb.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.LightBlue));
The highlighting feature works only on the first try and breaks after second try. Anyone know the reason for this?
突出显示功能仅适用于第一次尝试并在第二次尝试后中断。有谁知道这是什么原因?
采纳答案by CSharpie
Ok this question is old but i finally found the Answer so i put this here.
好的,这个问题很老,但我终于找到了答案,所以我把它放在这里。
I was having similiar issues when i tried to make some Syntaxhighlighter using the RichTextBox. What I found out is, that when you play arround with ApplyPropertyValueyou cannot simply use GetPositionAtOffsetanymore. I believe that applying propertyvalues seems to change the "internal positions" of TextTokens inside the Document, hence 'braking' this functionality.
当我尝试使用 RichTextBox 制作一些 Syntaxhighlighter 时,我遇到了类似的问题。我发现,当您使用ApplyPropertyValue进行游戏时,您不能再简单地使用GetPositionAtOffset了。我相信应用 propertyvalues 似乎改变了 Document 内 TextTokens 的“内部位置”,因此“制动”了这个功能。
The Workaround:
解决方法:
Everytime you need to work with GetPositionAtOffsetfirst call ClearAllPropertieson the complete TextRange of the Document, then reapply all your properties using ApplyPropertyValuebut thistime from right to left. (right means highest offset)
每次您需要使用GetPositionAtOffset 时,首先在文档的完整 TextRange 上调用ClearAllProperties,然后使用ApplyPropertyValue重新应用所有属性,但这次是从右到左。(右表示最高偏移)
I dont know if you had applied any PropertyValues expect the Selection highlighting, so you might need to put more thinking inthere.
我不知道您是否应用了任何 PropertyValues 期望选择突出显示,因此您可能需要在其中进行更多思考。
This is how my code looked when it caused the Problem:
这是我的代码在导致问题时的样子:
private void _highlightTokens(FlowDocument document)
{
TextRange fullRange = new TextRange(document.ContentStart, document.ContentEnd);
foreach (Token token in _tokenizer.GetTokens(fullRange.Text))
{
TextPointer start = fullRange.Start.GetPositionAtOffset(token.Position);
TextPointer end = start.GetPositionAtOffset(token.Length);
TextRange range = new TextRange(start, end);
range.ApplyPropertyValue(TextElement.ForegroundProperty, _getTokenColor(token));
}
}
And i fixed it like this:
我是这样修复的:
private void _highlightTokens(FlowDocument document)
{
TextRange fullRange = new TextRange(document.ContentStart, document.ContentEnd);
fullRange.ClearAllProperties(); // NOTICE: first remove allProperties.
foreach (Token token in _tokenizer.GetTokens(fullRange.Text).Reverse()) // NOTICE: Reverse() to make the "right to left" work
{
TextPointer start = fullRange.Start.GetPositionAtOffset(token.Position);
TextPointer end = start.GetPositionAtOffset(token.Length);
TextRange range = new TextRange(start, end);
range.ApplyPropertyValue(TextElement.ForegroundProperty, _getTokenColor(token));
}
}
回答by Bernard
Use the Select()method on the RichTextBox.Selectionproperty.
在RichTextBox.Selection属性上使用Select()方法。
回答by suhas
Blockquote you can get the text between spaces.....
Blockquote 你可以得到空格之间的文本.....
private string RichWordOver(RichTextBox rch, int x, int y) {
私人字符串 RichWordOver(RichTextBox rch, int x, int y) {
int pos = rch.GetCharIndexFromPosition(new Point(x, y));
if (pos <= 0) return "";
string txt = rch.Text;
int start_pos;
for (start_pos = pos; start_pos >= 0; start_pos--)
{
char ch = txt[start_pos];
if (!char.IsLetterOrDigit(ch) && !(ch=='_')) break;
}
start_pos++;
int end_pos;
for (end_pos = pos; end_pos < txt.Length; end_pos++)
{
char ch = txt[end_pos];
if (!char.IsLetterOrDigit(ch) && !(ch == '_')) break;
}
end_pos--;
if (start_pos > end_pos) return "";
return txt.Substring(start_pos, end_pos - start_pos + 1);
}
private void rchText_MouseClick(object sender, MouseEventArgs e) { MessageBox.Show(RichWordOver(rchText, e.X, e.Y)); }
private void rchText_MouseClick(object sender, MouseEventArgs e) { MessageBox.Show(RichWordOver(rchText, eX, eY)); }

