0

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.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50
  • You want to remove just the first line? – IndieTech Solutions Apr 05 '15 at 20:27
  • Yes, each time a new entry is added once there are 50(in the example) I want to delete the oldest entry so that there is never more than 50 lines displayed. – CircuitSix Apr 05 '15 at 20:30
  • What do you expect it to do? What is an "entry"? What does it do? What is the actual count? – CodeCaster Apr 05 '15 at 20:30
  • So an entry consists of the 3 paragraph.Inlines.Add entries, this would be one "line". I never want more than 50 of these at one time in the paragraph. When a new enrty of those 3 inlines is added I want to remove the oldest one. – CircuitSix Apr 05 '15 at 20:33

2 Answers2

0

Suggest you use List verus array. It gives you some functionality you need.

    public List<string> TrimParagraph(List<string> paragraph)
    { 
        int count = paragraph.Count;

        if (count > 50)
            paragraph = paragraph.Skip(count - 50).ToList();

        return paragraph;
    }

Edit... Use something like this when constructing your paragraph object.

0

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