Listboxes, Strings And Arrays
-
Hi, I'm probably missing something really simple here, but i'm stuck. Basically I have a listbox, which a user puts items into via a textbox and button, this works fine, the items go in. Now, on exit the items save to a text file, this also works fine, ive checked all the items are contained in the text file. Now, when the user re-opens the application there is a button called load previous items, when i click this i have a streamreader reading the text document, also works fine, checked with a messagebox, and it saves it to the string Now, I want the string to be saved into a array which is then put back into the textbox, the problem being, I dont know how to count the items in the string (each on a diffrent line) to put them into a array, and then after that I need a example of adding the array into my listbox. Im guessing its something to do with the
listbox1.Addrange();
command. Cheers, Ben. -
Hi, I'm probably missing something really simple here, but i'm stuck. Basically I have a listbox, which a user puts items into via a textbox and button, this works fine, the items go in. Now, on exit the items save to a text file, this also works fine, ive checked all the items are contained in the text file. Now, when the user re-opens the application there is a button called load previous items, when i click this i have a streamreader reading the text document, also works fine, checked with a messagebox, and it saves it to the string Now, I want the string to be saved into a array which is then put back into the textbox, the problem being, I dont know how to count the items in the string (each on a diffrent line) to put them into a array, and then after that I need a example of adding the array into my listbox. Im guessing its something to do with the
listbox1.Addrange();
command. Cheers, Ben.If the file content is indeed strings each on a new line, then the easy way is:
string\[\] lines = File.ReadAllLines(@"c:\\Temp.txt"); ListBox1.Items.AddRange(lines);
Which means you don't need to know how many lines there are. [edit]Where did my "[" and "]" characters go? Must remember to preview...[/edit]
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
If the file content is indeed strings each on a new line, then the easy way is:
string\[\] lines = File.ReadAllLines(@"c:\\Temp.txt"); ListBox1.Items.AddRange(lines);
Which means you don't need to know how many lines there are. [edit]Where did my "[" and "]" characters go? Must remember to preview...[/edit]
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones