How to get paragraph text from richtextbox when double clicking on it

I am having problems managing a WPF richtextbox control.

I want it to be as follows: I have a RichTextBox control called richTextBox1 that I have populated with data from a database.

I need to get the text on one line (which means - one paragraph) when I click on the control.

All I found on the net is the code to copy all the RTB text.

Any ideas how to get only the text on the line that was clicked?

+2


source to share


3 answers


I've done some major rework on the web and here's a working solution.

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);
}

      



(thanks to Justin-Josef with his post: http://blogs.microsoft.co.il/blogs/justinangel/archive/2008/01/29/tapuz-net-getting-wpf-s-flowdocument-and-flowdoucmentreader-mouseover -text.aspx )

+2


source


Correct code:



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);
}

      

0


source


OOPS, I made the lines in reverse order. Here's the reworked code ... :) 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);


    }

      

-1


source







All Articles