C# 从RichTextBox获取部分文本
谁能告诉我这有什么问题吗。我试图获取插入符号和插入符号前几个字符之间的文本。“comparable”永远不会比C# 从RichTextBox获取部分文本,c#,wpf,richtextbox,C#,Wpf,Richtextbox,谁能告诉我这有什么问题吗。我试图获取插入符号和插入符号前几个字符之间的文本。“comparable”永远不会比RichTextBox中的实际文本长 这是我的代码: int coLen = comparable.Length; TextPointer caretBack = rtb.CaretPosition.GetPositionAtOffset(coLen, LogicalDirection.Backward); TextRange rtbText = new TextRange(c
RichTextBox
中的实际文本长
这是我的代码:
int coLen = comparable.Length;
TextPointer caretBack = rtb.CaretPosition.GetPositionAtOffset(coLen,
LogicalDirection.Backward);
TextRange rtbText = new TextRange(caretBack, rtb.CaretPosition);
string text = rtbText.Text;
这将返回text=”“
请帮忙 这一切正常,我得到了
ia
一段代码:
RichTextBox rtb = new RichTextBox();
rtb.AppendText("I am adding some texts to the richTextBox");
rtb.CaretPosition = rtb.CaretPosition.DocumentEnd;
int coLen = 3;
TextPointer caretBack = rtb.CaretPosition.GetPositionAtOffset(-coLen);
TextRange rtbText = new TextRange(caretBack, rtb.CaretPosition);
string ttt = rtbText.Text;
编辑
下面是一个MSTest方法来解释插入符号和读取的行为:
[TestMethod]
public void TestRichtTextBox()
{
RichTextBox rtb = new RichTextBox();
rtb.AppendText("I am adding some texts to the richTextBox");
int offset = 3;
TextPointer beginningPointer = rtb.CaretPosition.GetPositionAtOffset(offset);
TextPointer endPointer = rtb.CaretPosition.DocumentEnd;
TextRange rtbText = new TextRange(beginningPointer, endPointer);
Assert.IsTrue(rtbText.Text == "m adding some texts to the richTextBox\r\n");
// Now we if we keep the same beggining offset but we change the end Offset to go backwards.
beginningPointer = rtb.CaretPosition.GetPositionAtOffset(3);
endPointer = rtb.CaretPosition; // this one is the beginning of the text
rtbText = new TextRange(beginningPointer, endPointer);
Assert.IsTrue(rtbText.Text == "I a");
// Nowe we want to read from the back three characters.
// so we set the end Point to DocumentEnd.
rtb.CaretPosition = rtb.CaretPosition.DocumentEnd;
beginningPointer = rtb.CaretPosition.GetPositionAtOffset(-offset);
endPointer = rtb.CaretPosition; // we already set this one to the end document
rtbText = new TextRange(beginningPointer, endPointer);
Assert.IsTrue(rtbText.Text == "Box");
}
此外,以下是MSDN关于负指数的评论:
偏移类型:System.Int32以符号表示的偏移量,用于
计算并返回位置。如果偏移量为负,则
位置的计算逻辑方向与该方向相反
由LogicalDirection属性指示
这就像预期的一样,我得到了
ia
一段代码:
RichTextBox rtb = new RichTextBox();
rtb.AppendText("I am adding some texts to the richTextBox");
rtb.CaretPosition = rtb.CaretPosition.DocumentEnd;
int coLen = 3;
TextPointer caretBack = rtb.CaretPosition.GetPositionAtOffset(-coLen);
TextRange rtbText = new TextRange(caretBack, rtb.CaretPosition);
string ttt = rtbText.Text;
编辑
下面是一个MSTest方法来解释插入符号和读取的行为:
[TestMethod]
public void TestRichtTextBox()
{
RichTextBox rtb = new RichTextBox();
rtb.AppendText("I am adding some texts to the richTextBox");
int offset = 3;
TextPointer beginningPointer = rtb.CaretPosition.GetPositionAtOffset(offset);
TextPointer endPointer = rtb.CaretPosition.DocumentEnd;
TextRange rtbText = new TextRange(beginningPointer, endPointer);
Assert.IsTrue(rtbText.Text == "m adding some texts to the richTextBox\r\n");
// Now we if we keep the same beggining offset but we change the end Offset to go backwards.
beginningPointer = rtb.CaretPosition.GetPositionAtOffset(3);
endPointer = rtb.CaretPosition; // this one is the beginning of the text
rtbText = new TextRange(beginningPointer, endPointer);
Assert.IsTrue(rtbText.Text == "I a");
// Nowe we want to read from the back three characters.
// so we set the end Point to DocumentEnd.
rtb.CaretPosition = rtb.CaretPosition.DocumentEnd;
beginningPointer = rtb.CaretPosition.GetPositionAtOffset(-offset);
endPointer = rtb.CaretPosition; // we already set this one to the end document
rtbText = new TextRange(beginningPointer, endPointer);
Assert.IsTrue(rtbText.Text == "Box");
}
此外,以下是MSDN关于负指数的评论:
偏移类型:System.Int32以符号表示的偏移量,用于
计算并返回位置。如果偏移量为负,则
位置的计算逻辑方向与该方向相反
由LogicalDirection属性指示
谢谢你的输入。我应该得到“盒子”,因为我想让它从插入符号所在的结尾算起。我想让它在caretCheck my edit之前获取文本。如果你想倒转,你需要将CaretPosition设置为endDocument。倒转似乎没有帮助,但doing-coLen有帮助。既然我花了这么多时间在这方面,你能解释一下为什么以及如何工作吗。非常感谢再次使用这个小测试方法检查编辑以解释发生了什么:)感谢您的输入。我应该得到“框”,因为我希望它从插入符号所在的末尾开始计算背景词。我想让它在caretCheck my edit之前获取文本。如果你想倒转,你需要将CaretPosition设置为endDocument。倒转似乎没有帮助,但doing-coLen有帮助。既然我花了这么多时间在这方面,你能解释一下为什么以及如何工作吗。再次感谢你!用这个小测试方法检查编辑,解释发生了什么:)