简体   繁体   中英

Loading text file into listbox. lines of code need to be Split

Im trying to use the openfiledialog to load a text file. string string bool Name, year in highschool, failing? code in text file

Chris | Junior | False
Jake Peters | Freshman | True
Jasmine Baker | Senior | False

So i use an array to take the code. then create a foreach loop to add to a string list

then split the line by " | " using a for loop

Then create a new object and save the data using variables of the object.

Finally add to list box

converting isnt needed. you can ignore the bool. I would like to understand the concept more

What I meantioned was the method I tried. Did not load at all.

string[] b = File.ReadAllLines(filePath);

List<string> ...
foreach(var a in b)
{
   list.Add(a);
}

for (int i =0; i<students.Count; i++)
{
list[i].Split('|');

//Human object...

Human x = new Human();

x.name =
listbox.add(x);
failinglistbox.Items.Add(x);

i think this is what you wanted.

string[] students = File.ReadAllLines(filePath);

for (int i = 0; i < students.Length; i++)
{
    string[] cells = students[i].Split('|');

    //Human object...

    Human x = new Human();

    x.name =cells[0].Trim();

////////edited////////////
        x.year =cells[1].Trim();
        x.failing =cells[2].Trim();

    listbox.add(x);
    failinglistbox.Items.Add(x);
}

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