简体   繁体   中英

extract lines from a flowdocument in a richtextbox starting with specific words WPF

I'm very new to wpf and I would like to make a text analysing tool. I already know how to import text into rich textbox and format it properly, but now I want to run a method that extracts all lines in the flowdocument that start with INT or EXT and place them in a listbox. It seems to be quite easier to do this in winforms than in WPF.

Is there someone who can guide me with this?

I wish I could already provide some code but the flowdocument is new to me as is wpf.

I have written a code snippet to collect the lines that begin with INT or EXT. I am sure the code is not optimal, because i am not practised with RichTextBox, but i think it is very easy to understand.

private List<string> CollectLines()
{
    TextRange textRange = new TextRange(
        // TextPointer to the start of content in the RichTextBox.
        TestRichTextBox.Document.ContentStart,
        // TextPointer to the end of content in the RichTextBox.
        TestRichTextBox.Document.ContentEnd);

    // The Text property on a TextRange object returns a string 
    // representing the plain text content of the TextRange. 
    var text = textRange.Text;

    List<string> resultList = new List<string>();

    // Collect all line that begin with INT or EXT
    // Or use .Contains if the line could begin with a tab (\t), spacing or whatever
    using (StringReader sr = new StringReader(text))
    {
        var line = sr.ReadLine();
        while (line != null)
        {

            if (line.StartsWith("INT") || line.StartsWith("EXT"))
            {
                resultList.Add(line);
            }

            line = sr.ReadLine();
        }
    }

    return resultList;
}

Maybe you can find out how you can put the list into a listbox yourself :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM