views:

251

answers:

2

I'm having some trouble getting control of the WPF richtextbox control.

What I want to is as follows : I have a RichTextBox control called richTextBox1 that I filled up with data from a database.

I need to get the text on a single line (meaning - a single paragraph) when I click the control.

All I found over the net is a code to copy ALL the RTB text.

Any ideas how to get just the text in the line that was clicked ?

+1  A: 

I made serious web digging and here is 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 = textAfterCursor+textBeforeCursor;
  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 )

Ohad
A: 

OOPS, I concated the strings in reverse order. Here is the revised 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);


    }