wpf 从富文本框中读取文本行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24127380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:52:43  来源:igfitidea点击:

Reading in lines of text from a rich text box

c#wpf

提问by user3494110

I am making a small application for myself that concatenates some statements based on text that is entered into a couple rich text boxes on a form.

我正在为自己制作一个小应用程序,它根据输入到表单上几个富文本框中的文本连接一些语句。

One text box may have 10-20 lines of text, and each line is it its own separate entry, so I need to be able to read the text line-by-line.

一个文本框可能有 10-20 行文本,每一行都是它自己单独的条目,所以我需要能够逐行阅读文本。

However, when researching WPFs, I have only seen one statement on the net in regards to reading text and it is reading the content of the box from start to finish. I would like to somehow loop through it or separate it line-by-line.

但是,在研究WPF时,我只在网上看到过一种关于阅读文本的说法,它是从头到尾阅读框的内容。我想以某种方式循环遍历它或将其逐行分开。

According to MSDN, to extract text from a WPF rich text box into a string, you need to use:

根据 MSDN,要将 WPF 富文本框中的文本提取为字符串,您需要使用:

string StringFromRichTextBox(RichTextBox rtb)
{
    TextRange textRange = new TextRange(
      // TextPointer to the start of content in the RichTextBox.
      rtb.Document.ContentStart, 
      // TextPointer to the end of content in the RichTextBox.
      rtb.Document.ContentEnd
    );

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

However, if you were to look at my rich text box, you would see that the text inside is a list of values, so for instance, one box may look like below:

但是,如果您查看我的富文本框,您会看到里面的文本是一个值列表,例如,一个框可能如下所示:

000423523

324

93489290099

823342342

0003242342

44400298889

I want to be able to read those values line by line within the RichTextBox, but in WPF, there doesn't seem to be a richtextbox1.Linesoption.

我希望能够在 中逐行读取这些值RichTextBox,但在 WPF 中,似乎没有richtextbox1.Lines选项。

回答by har07

You can use String.Split()method to split RichTextBoxtext by line-break and exclude empty lines from result set, for example :

您可以使用String.Split()方法RichTextBox按换行符拆分文本并从结果集中排除空行,​​例如:

String[] lines = 
        StringFromRichTextBox(rtb).Split(new[]{Environment.NewLine}
                                          , StringSplitOptions.RemoveEmptyEntries);

foreach (var line in lines)
{
    MessageBox.Show(line);
}

回答by Luis Romero-Sevilla

To read lines of a WPF RichTextBox you can use the following code.

要读取 WPF RichTextBox 的行,您可以使用以下代码。

TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd); 
string[] rtbLines = textRange.Text.Split(Environment.NewLine);
foreach(line in rtbLines)
{
    //do something with line
}

for this specific code, you will need to use the following library

对于此特定代码,您将需要使用以下库

using System.Windows.Documents;

回答by quetzalcoatl

Sorry for brevity, but how about something as simple as

抱歉为简洁,但像这样简单的事情怎么样?

var lines = richTextBox1.Text.Split('\n');