简体   繁体   中英

TextBox is not creating newline when reading text file C#

I have this program that supposed to reads multiple text files on the same folder, in that folder there's 2 text files which are supposed to be read, ok now I have to generate a new TextBox based on the total numbers of text files in that folder.

Main Goal

Load the contents of those files in each textbox

  • File1.txt contents will be loaded into TextBox1.
  • File2.txt contents will be loaded into TextBox2.

Content of File1.txt:

Title 1
ABCDEFG

Content of File2.txt:

Title 2
1234567890

The problem

Loading the contents of those files into each TextBoxes works fine, but the problem is that the newline isn't created on the TextBoxes.

Instead I got this on each textboxes:

TextBox 1:

Title 1ABCDEFG

TextBox 2:

Title 21234567890

Do the necessary stuff as soon the program loads:

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

A fix could be using the Lines property instead of the Text property.

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(),

But a better approach is to use this instead:

// 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++;
   .....
}             

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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