Search code examples
c#flowdocumentparagraph

Systematically remove first line


I am trying to remove the first line of a paragraph when the total lines exceed a predetermined number of entries. This is for a kind of chat window and I do not want too many lines displayed at one time.

private Paragraph paragraph = new Paragraph();
public void WriteMessage(string output)
    {
string outputFormat = string.Format("{0}", output);
            string[] parts = output.Split(new char[]{':'}, 2);
            string user = parts[0];
            string[] username = parts[0].Split('!');
            paragraph.Inlines.Add(new Run(username[0].Trim() + ": "){Foreground = UserColor});
            paragraph.Inlines.Add(new Run(parts[1]) { Foreground = MessageColor});
            paragraph.Inlines.Add(new LineBreak());

if (paragraph.Inlines.Count >= 50) { 
                //???
                //The count does not actually count lines the way I would expect.
            }
}

Not sure of the easiest way to do this, everything I have tried thus far has not worked.


Solution

  • Solved it by creating a FlowDocument and adding the paragraph to the block. Then each entry is it's own block and it retains the original formatting.

    private Paragraph paragraph = new Paragraph();
    _rtbDocument = new FlowDocument(paragraph);
    
    public void WriteMessage(string output)
        {
            string outputFormat = string.Format("{0}", output);
            string[] parts = output.Split(new char[]{':'}, 2);
            string user = parts[0];
            string[] username = parts[0].Split('!');
    
            Paragraph newline = new Paragraph();
    
            newline.LineHeight = 2;
            newline.Inlines.Add(new Run(username[0].Trim() + ": ") { Foreground = UserColor });
            newline.Inlines.Add(new Run(parts[1]) { Foreground = MessageColor });
    
            _rtbDocument.Blocks.Add(newline);
    
            if (_rtbDocument.Blocks.Count > 10) 
                { 
                   _rtbDocument.Blocks.Remove(_rtbDocument.Blocks.FirstBlock); 
                }
    }