C# 如何在给定索引和长度的情况下选择RichTextBox文本
如果只给您一个要选择的特定文本的索引和长度(或EndIndex),您如何在RichTextBox的WPF版本中执行此操作 这在Textbox中非常可行,因为您可以调用Textbox.Select(startIndex,Length),但我在RTB中没有看到任何等价的东西 编辑:我找到了选择的答案C# 如何在给定索引和长度的情况下选择RichTextBox文本,c#,.net,wpf,richtextbox,C#,.net,Wpf,Richtextbox,如果只给您一个要选择的特定文本的索引和长度(或EndIndex),您如何在RichTextBox的WPF版本中执行此操作 这在Textbox中非常可行,因为您可以调用Textbox.Select(startIndex,Length),但我在RTB中没有看到任何等价的东西 编辑:我找到了选择的答案 internal string Select(RichTextBox rtb, int index, int length) { TextRange textRa
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;
}
但是,当我在选择后尝试高亮显示该行时:
rtb.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.LightBlue));
高亮显示功能仅在第一次尝试时有效,第二次尝试后会中断。有人知道原因吗?使用属性上的方法
Blockquote您可以获取空格之间的文本
私有字符串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);
}
int pos=rch.GetCharIndexFromPosition(新点(x,y));
如果(位置=0;开始位置--)
{
char ch=txt[start_pos];
如果(!char.isleterordigit(ch)和&!(ch=''中断);
}
启动_pos++;
int end_pos;
用于(end_pos=pos;end_pos结束位置)返回“”;
返回txt.Substring(开始位置,结束位置-开始位置+1);
}
private void rchText_鼠标单击(对象发送方,鼠标目标)
{
Show(RichWordOver(rchText,e.X,e.Y));
}好的,这个问题很老了,但我终于找到了答案,所以我把它放在这里 当我尝试使用RichTextBox制作一些语法高亮时,我也遇到了类似的问题。 我发现,当您使用ApplyPropertyValue玩arround时,您不能再简单地使用GetPositionAtOffset。我相信应用PropertyValue似乎会改变文档中TextToken的“内部位置”,从而“阻止”此功能 解决办法: 每次需要使用GetPositionAtOffset时,首先对文档的完整文本范围调用ClearAllProperties,然后使用ApplyPropertyValue重新应用所有属性,但这次是从右向左。(右表示最高偏移) 我不知道您是否应用了除选择突出显示之外的任何属性值,因此您可能需要在其中进行更多思考 这是导致问题的代码的外观:
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));
}
}
我把它改成这样:
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));
}
}
Select方法接受两个文本指针,我只需要一个索引和长度,它们都是整数。在这种情况下,如何正确设置textpointer变量?