winforms 阅读文本文件C#时,TextBox不创建换行符

ulmd4ohb  于 2022-11-17  发布在  C#
关注(0)|答案(1)|浏览(201)

我有这个程序,应该读取同一个文件夹上的多个文本文件,在该文件夹中有2个文本文件应该被读取,现在我必须生成一个新的文本框的基础上,该文件夹中的文本文件总数.

主要目标

在每个文本框中加载这些文件的内容

  • File1.txt内容将加载到TextBox1中。
  • File2.txt内容将加载到TextBox2中。

File1.txt的内容:

Title 1
ABCDEFG

File2.txt的内容:

Title 2
1234567890

问题

将这些文件的内容加载到每个TextBox中可以正常工作,但问题是没有在TextBox上创建换行符。
相反,我在每个文本框上都看到了:
文本框1:

Title 1ABCDEFG

文本框2:

Title 21234567890

程序加载后立即执行必要的操作:

private void Form1_Load(object sender, EventArgs e) {

        flowLayoutPanel1.AutoScroll = true;

        var dirs_notes = @"C:\MAIN_LOC\DATA_LOC\";
        var count_notes = Directory.GetFiles(dirs_notes,"*.*",SearchOption.AllDirectories).Count();

        string setup_path = @"C:\MAIN_LOC\DATA_LOC\";
        if(Directory.Exists(setup_path)) {
            string[] get_notes = Directory.GetFiles(dirs_notes, "*.txt", SearchOption.AllDirectories);
            string[] get_texts = get_notes.Select(x => File.ReadAllText(x)).ToArray();

            for(int i=0; i<count_notes; i++) {
                int top = 25;
                int h_p = 170;
                var load_note = new Guna2TextBox() {
                    Text = "\n" + get_texts[i],
                    Name = "Note" + i,
                    Multiline = true,
                    AcceptsTab = true,
                    AcceptsReturn = true,
                    WordWrap = false,
                    Width = 230,
                    Height = 145,
                    BorderRadius = 8,
                    Font = new Font("Bahnschrift", 13),
                    ForeColor = Color.White,
                    FillColor = ColorTranslator.FromHtml("#1E1E1E"),
                    BorderColor = ColorTranslator.FromHtml("#2C2C2C"),
                    Location = new Point(450, top)
                };
                top += h_p;
                flowLayoutPanel1.Controls.Add(load_note);
                }
            } else {
                MessageBox.Show("There's problem with loading notes..", "Flow Notes System");
            }
    }
s4chpxco

s4chpxco1#

修复方法是使用Lines属性而不是Text属性。

var dirs_notes = @"C:\MAIN_LOC\DATA_LOC\";

// Be sure to count only txt files here...
var count_notes =  Directory.GetFiles(dirs_notes,"*.txt",SearchOption.AllDirectories).Count();

string setup_path = @"C:\MAIN_LOC\DATA_LOC\";
if(Directory.Exists(setup_path)) {
    string[] get_notes = Directory.GetFiles(dirs_notes, "*.txt", SearchOption.AllDirectories);
    var get_texts = get_notes.Select(x => File.ReadLines(x));

    for(int i=0; i<count_notes; i++) {
        ....
        var load_note = new Guna2TextBox() {
        Lines = "\n" + get_texts[i].ToArray(),

但更好的方法是使用以下代码:

// No counting here (counting means load all 
// files names in a memory array and the get its length
var files = Directory.EnumerateFiles(dirs_notes,"*.txt",SearchOption.AllDirectories)

int i = 1;
// Enumerate files one by one without loading all names in memory
foreach(string file in files) {
   int top = 25;
   int h_p = 170;
   var load_note = new Guna2TextBox() {
       // Set the textbox with the current file lines content
       // again one by one without loading all texts in memory
       Lines = File.ReadLines(file).ToArray(),
       Name = "Note" + i,
       ...
   }
   i++;
   .....
}

相关问题