2

我在控制 WPF 富文本框控件时遇到了一些麻烦。

我想要如下: 我有一个名为richTextBox1 的RichTextBox 控件,我用数据库中的数据填充了它。

单击控件时,我需要将文本放在一行(意思是单个段落)上。

我在网上找到的只是复制所有 RTB 文本的代码。

任何想法如何获取被点击的行中的文本?

4

3 回答 3

2

我进行了认真的网络挖掘,这是一个可行的解决方案。

private void richTextBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
  TextPointer t = richTextBox1.GetPositionFromPoint(e.GetPosition(richTextBox1), true);

  string textAfterCursor  = t.GetTextInRun(LogicalDirection.Forward);
  string textBeforeCursor = t.GetTextInRun(LogicalDirection.Backward);

  string FullParagraphText = textBeforeCursor+textAfterCursor;
  MessageBox.Show(FullParagraphText);
}

(感谢贾斯汀约瑟夫的帖子:http: //blogs.microsoft.co.il/blogs/justinangel/archive/2008/01/29/tapuz-net-getting-wpf-s-flowdocument-and-flowdoucmentreader-鼠标悬停文本.aspx )

于 2009-09-17T08:23:11.173 回答
0

以下代码是正确的:

private void richTextBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    TextPointer tp = richTextBox1.GetPositionFromPoint(e.GetPosition(richTextBox1), true);

    TextPointer line_start = tp.GetLineStartPosition(0);
    var nextStart = pos.GetLineStartPosition(1);
    TextPointer lineEnd = (nextStart != null ? nextStart : pos.DocumentEnd).GetInsertionPosition(LogicalDirection.Backward);

    TextRange tr = new TextRange(line_start, lineEnd);
    string line = tr.Text;
    MessageBox.Show(line);
}
于 2014-02-19T22:30:57.127 回答
-1

糟糕,我以相反的顺序连接了字符串。这是修改后的代码... :) Ohad。

   private void richTextBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        TextPointer t = richTextBox1.GetPositionFromPoint(e.GetPosition(richTextBox1), true);

        string textAfterCursor  = t.GetTextInRun(LogicalDirection.Forward);
        string textBeforeCursor = t.GetTextInRun(LogicalDirection.Backward);

        string FullParagraphText = textBeforeCursor+textAfterCursor;
        MessageBox.Show(FullParagraphText);


    }
于 2009-09-17T09:08:38.570 回答