Search code examples
c#wpfrichtextboxflowdocument

How to remove a string from a flowdocument paragraph without disturbing the MarkUp?


Being new to RichTextBox and Googling for hours, I'm stumped. Having isolated the paragraph part of a flow document. I now wish to remove all the text upto and including the first ":" in the paragraph--but retain the rest of the paragraph (including all its formatting --XAML). (This in WPF vs2010).

I'm sure its simple but how is this done?

What I have so far will read up to the first ":", but how do you remove this text as well? Is there a better way??

  public InkRichViewModel(int p, Paragraph paragraph)
    {
        this.p = p;

        //Read the paragraph title from the paragraph
        TextPointer start = paragraph.ContentStart;
        TextPointer end = FindWordFromPosition(start, ":");

        TextRange textRange = new TextRange(start, end);
        String Title = textRange.Text;

        //Remove the title from the paragraph
        TextPointer endp = paragraph.ContentEnd;
        TextRange t2 = new TextRange(start, endp);

          ?????????????????????????????????????????


        this.Note = paragraph;   <---Note is the paragraph without the leading string.
    }

Thanks for all help, regards.


Solution

  • In my case, this solves the above problem. Is there a better way?

     // Read paragraph upto and including first ":"
            string text =  (paragraph.Inlines.FirstInline as Run).Text;
            int r = text.IndexOf(":")+1;
            string Title = text.Substring(0,r).Trim();
    
            // Remove Title from text;
            string newtext = text.Substring(r).Trim();
    
            // Insert new inline and remove the old inline.
            Inline z = paragraph.Inlines.FirstInline;
            Run runx = new Run(newtext);
            paragraph.Inlines.InsertBefore(z, runx);
            paragraph.Inlines.Remove(z);
    
            this.Note = paragraph;