1

我想阅读一个文本文件,其中包含多个由新行分隔的段落。如何单独阅读每个段落RichTextBox以及如何通过按钮下一个按钮转移到下一个段落并通过之前在表单中设计的按钮返回第一段。我的代码

private void LoadFile_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    dialog.Title = "Select a text file";
    dialog.ShowDialog();

    if (dialog.FileName != "")
    {
        System.IO.StreamReader reader = new System.IO.StreamReader(dialog.FileName);
        string Text = reader.ReadToEnd();
        reader.Close();
        this.Input.TextChanged -= new System.EventHandler(this.Input_TextChanged);
        Input.Clear();
        Input.Text = Text;
    } 
} 
4

2 回答 2

3

使用此代码。

var text = File.ReadAllText(inputFilePath);
var paragraphs = text .Split('\n');

段落将是一个包含所有段落的字符串数组。

于 2013-03-27T11:56:47.563 回答
0

用于String.split()将其拆分为'\n'。然后遍历 Button next上的数组。

private string[] paragraphs;
private int index = 0;
private void LoadFile_Click(object sender, EventArgs e)
{
   OpenFileDialog dialog = new OpenFileDialog();
   dialog.Filter =
      "txt files (*.txt)|*.txt|All files (*.*)|*.*";
   dialog.Title = "Select a text file";

   dialog.ShowDialog();

   if (dialog.FileName != "")
   {
       System.IO.StreamReader reader = new System.IO.StreamReader(dialog.FileName);
       string Text = reader.ReadToEnd();
       reader.Close();
       this.Input.TextChanged -= new System.EventHandler(this.Input_TextChanged);
       Input.Clear();
       paragraphs = Text.Split('\n');
       index = 0;
       Input.Text = paragraphs[index];
   } 
} 

private void Next_Click(object sender, EventArgs e)
{
   index++;
   Input.Text = paragraphs[index];
}

(我知道这可能不是最优雅的解决方案,但它应该给出一个关于做什么的想法。)

于 2013-03-27T11:54:29.580 回答