簡體   English   中英

讀取文本文件 C# 時,TextBox 未創建換行符

[英]TextBox is not creating newline when reading text file C#

我有這個程序應該讀取同一個文件夾中的多個文本文件,在那個文件夾中有 2 個文本文件應該被讀取,好的,現在我必須根據該文件夾中的文本文件總數生成一個新的 TextBox .

主要目標

在每個文本框中加載這些文件的內容

  • 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");
            }
    }

修復可能是使用 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++;
   .....
}             

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM