locked
Given the current position in RichTextBox, how do I get the current Paragraph? RRS feed

  • Question

  • I can get the current TextPointer by  richText.Selection.Start,  then I can get the Run object from TextPointer (via .Parent).  But how can I get the Paragraph of this Run object.   There is no Parent off the Run object.

    Thanks.

    Tuesday, January 4, 2011 4:19 AM

Answers

  • Thanks, but that won't give me the current Paragraph object. That gives me the Run object.  


    After studying at RichTextBox Content model, I'm able to get the current Paragraph with this code.

     

    var tp = rtb.Selection.Start as TextPointer;

    var rn = tp.Parent as Run  

    var res =
    from block in rtb.Blocks
    from inline in (block as Paragraph).Inlines
    w
    here inline == rn
    select block;

     

    Tuesday, January 4, 2011 1:56 PM

All replies

  • you could do following 

     var startpos = richTB.Selection.Start;
                var par =(System.Windows.Documents.Run) startpos.Parent;
                MessageBox.Show(par.Text);

     var startpos = richTextBox1.Selection.Start;

                var par =(System.Windows.Documents.Run) startpos.Parent;

                MessageBox.Show(par.Text);

    Tuesday, January 4, 2011 5:24 AM
  • Thanks, but that won't give me the current Paragraph object. That gives me the Run object.  


    After studying at RichTextBox Content model, I'm able to get the current Paragraph with this code.

     

    var tp = rtb.Selection.Start as TextPointer;

    var rn = tp.Parent as Run  

    var res =
    from block in rtb.Blocks
    from inline in (block as Paragraph).Inlines
    w
    here inline == rn
    select block;

     

    Tuesday, January 4, 2011 1:56 PM