如何从 Silverlight 4 RichTextBox 控件获取当前行文本到光标
在 Winforms RichTextBox 控件中,我以前使用过 GetLineFromCharIndex 方法和 GetFirstCharIndexOfCurrentLine 来计算当前行上键入文本的起点和终点。
我正在努力使用 Silverlight 4 中的新 RichTextBox 控件,因为似乎没有等效的方法。 GetPositionFromPoint 可用,但看起来很笨重。
干杯。
更新了...我已经采取了一些措施来完成这项工作,但这需要我使用控件的 Select 方法,这感觉非常错误...
private string GetCurrentLine()
{
TextPointer prevSelStart = richTextBox1.Selection.Start;
Point lineStart = new Point(0, prevSelStart.GetCharacterRect(LogicalDirection.Forward).Y);
TextPointer prevSelEnd = richTextBox1.Selection.End;
TextPointer currentLineStart = richTextBox1.GetPositionFromPoint(lineStart);
//need to find a way to get the text between two textpointers
//other than performing a temporary selection in the rtb
richTextBox1.Selection.Select(currentLineStart, prevSelStart);
string text = richTextBox1.Selection.Text;
//revert back to previous selection
richTextBox1.Selection.Select(prevSelStart, prevSelEnd);
return text;
}
In the Winforms RichTextBox control I have previously used the GetLineFromCharIndex method and the GetFirstCharIndexOfCurrentLine to work out the start and end points of the typed text on he current line.
I am struggling with the new RichTextBox control in Silverlight 4 as there doesn't appear to be equivalent methods. GetPositionFromPoint is available but seems a but clunky.
Cheers.
Updated...I have gone someway to making this work but this requires me to use the Select method of the control, this feels very wrong...
private string GetCurrentLine()
{
TextPointer prevSelStart = richTextBox1.Selection.Start;
Point lineStart = new Point(0, prevSelStart.GetCharacterRect(LogicalDirection.Forward).Y);
TextPointer prevSelEnd = richTextBox1.Selection.End;
TextPointer currentLineStart = richTextBox1.GetPositionFromPoint(lineStart);
//need to find a way to get the text between two textpointers
//other than performing a temporary selection in the rtb
richTextBox1.Selection.Select(currentLineStart, prevSelStart);
string text = richTextBox1.Selection.Text;
//revert back to previous selection
richTextBox1.Selection.Select(prevSelStart, prevSelEnd);
return text;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您不能避免选择,这是一种正确的方法(“选择”只是一个逻辑选择),但您可以使用
TextPointer 避免
:从GetPositionFromPoint
。 GetNextInsertionPosition(LogicalDirection )richTextBox1.Selection.Start
开始并向行首移动 (char != '\n')I don't think you can't avoid the selection, it's a proper way to do it (the "selection" is just a logical one), but you can avoid
GetPositionFromPoint
withTextPointer.GetNextInsertionPosition(LogicalDirection )
: Start fromrichTextBox1.Selection.Start
and move towards the beginning of the line (char != '\n')我需要弄清楚我何时处于 RTB 的顶线或底线。为此,我使用了 GetCharacterRect 方法,然后比较顶部以查看它是在最后一行还是第一行。
您可以执行相同的操作,并使用文本指针在文本中移动以及顶部不匹配的次数。
这是查看光标是否位于第一行或最后一行的代码:
I've needed to figure out when I was on the top line or bottom line of the RTB. To do this I used the GetCharacterRect methods then compared the tops to see if it was on the last line or first line.
You could do the same and use a text pointer to move through the text and number of times the tops don't match.
Here's code to see if the cursor is on the first or last lines: