简体   繁体   English

从富文本框中的流程文档中提取以特定单词WPF开头的行

[英]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. 我是wpf的新手,我想制作一个文本分析工具。 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. 我已经知道如何将文本导入到RTF文本框中并正确设置其格式,但是现在我想运行一种方法,该方法可提取FlowDocument中以INT或EXT开头的所有行并将它们放置在列表框中。 It seems to be quite easier to do this in winforms than in WPF. 在Winforms中执行此操作似乎比在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. 我希望我已经可以提供一些代码,但是flowdocument和wpf对我来说都是新的。

I have written a code snippet to collect the lines that begin with INT or EXT. 我编写了一个代码段来收集以INT或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. 我确定代码不是最佳的,因为我没有使用RichTextBox进行练习,但是我认为它很容易理解。

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 :) 也许您可以自己找到如何将列表放入列表框的方法:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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