|
-
Oct 19th, 2009, 07:37 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Saving Items in listbox
How do I save the contents of a listbox?
-
Oct 19th, 2009, 07:58 PM
#2
Fanatic Member
Re: Saving Items in listbox
Saving to Where? A database? a file?
-
Oct 19th, 2009, 08:09 PM
#3
Thread Starter
Hyperactive Member
Re: Saving Items in listbox
 Originally Posted by RunsWithScissors
Saving to Where? A database? a file?
File/My.Settings
-
Oct 19th, 2009, 08:15 PM
#4
Re: Saving Items in listbox
 Originally Posted by werido
File/My.Settings
Which? They are quite different. I would suggest in future that you decide exactly what you want to do and provide a full and clear description of that. If you don;t know exactly what you want to do then by all means ask advice but, again, post a full and clear description of what you want. Just posting a few words is kinda impolite and it also means we have to guess and assume in many cases.
-
Oct 19th, 2009, 08:33 PM
#5
Thread Starter
Hyperactive Member
Re: Saving Items in listbox
 Originally Posted by jmcilhinney
Which? They are quite different. I would suggest in future that you decide exactly what you want to do and provide a full and clear description of that. If you don;t know exactly what you want to do then by all means ask advice but, again, post a full and clear description of what you want. Just posting a few words is kinda impolite and it also means we have to guess and assume in many cases.
Sorry. I want to save the items in a File, how do I do that?
Also, after I save the items, how do I get my program to read it?
Last edited by werido; Oct 19th, 2009 at 08:51 PM.
-
Oct 19th, 2009, 09:03 PM
#6
Re: Saving Items in listbox
Assuming that the ListBox contains just Strings:
vb.net Code:
Dim items(Me.ListBox1.Items.Count - 1) As String Me.ListBox1.Items.CopyTo(items, 0) IO.File.WriteAllLines("file path here", items)
or with LINQ:
vb.net Code:
IO.File.WriteAllLines("file path here", _ Me.ListBox1.Items.Cast(Of String)().ToArray())
To reload:
vb.net Code:
Me.ListBox1.Items.AddRange(IO.File.ReadAllLines("file path here"))
-
Oct 19th, 2009, 09:05 PM
#7
Thread Starter
Hyperactive Member
Re: Saving Items in listbox
 Originally Posted by jmcilhinney
Assuming that the ListBox contains just Strings:
vb.net Code:
Dim items(Me.ListBox1.Items.Count - 1) As String Me.ListBox1.Items.CopyTo(items, 0) IO.File.WriteAllLines("file path here", items)
or with LINQ:
vb.net Code:
IO.File.WriteAllLines("file path here", _ Me.ListBox1.Items.Cast(Of String)().ToArray())
To reload:
vb.net Code:
Me.ListBox1.Items.AddRange(IO.File.ReadAllLines("file path here"))
I would like to store some other things in the Text File, how do I get it to only read part of the file?
Reloading it doesnt work. I put the code in my Formloading, and nothing happened. I checked the Text File and there was text
Last edited by werido; Oct 19th, 2009 at 09:11 PM.
-
Oct 19th, 2009, 09:17 PM
#8
Re: Saving Items in listbox
 Originally Posted by werido
I would like to store some other things in the Text File, how do I get it to only read part of the file?
I seem to recall having asked at some point in the recent past that you provide a full and clear description of what you want. Now here you are saying that there's actually more to what you want than what you posted. As such, I wasted my time providing that solution because it doesn't actually do what you want. Do you see why you need to actually tell us what it is that you want and not expect us to guess? Is it your desire that we waste our time and yours providing irrelevant solutions?
 Originally Posted by werido
Reloading it doesnt work. I put the code in my Formloading, and nothing happened. I checked the Text File and there was text
It works if it's done properly so you must have not done it properly. As you haven't shown us exactly what you're doing I can't say what might be wrong with it. If the code is executed and it refers to the correct file then the data will be loaded.
-
Oct 19th, 2009, 09:49 PM
#9
Re: Saving Items in listbox
I can't be specific because you haven't been but, in general, if you want to read part of a text file then you're going to have to create a StreamReader and read the file line by line. You will have start at the first line and keep reading until you've read all the data you need.
As for writing, you can really write part of a file. You'll need to read in all the current data, add edit or delete the relevant parts and write out the new contents in their entirety, which you could do by calling File.WriteAllText, File.WriteAllLines or with a StreamWriter, depending on which works best in the circumstances.
-
Oct 20th, 2009, 05:22 PM
#10
Thread Starter
Hyperactive Member
Re: Saving Items in listbox
 Originally Posted by jmcilhinney
I can't be specific because you haven't been but, in general, if you want to read part of a text file then you're going to have to create a StreamReader and read the file line by line. You will have start at the first line and keep reading until you've read all the data you need.
As for writing, you can really write part of a file. You'll need to read in all the current data, add edit or delete the relevant parts and write out the new contents in their entirety, which you could do by calling File.WriteAllText, File.WriteAllLines or with a StreamWriter, depending on which works best in the circumstances.
What I want to do is basically save settings in a text file. So I only want to get the Items that were saved from part of the Text file, so for example, only lines 10~20. How would I do that?
Also, lets say I have a checkbox on my form. When I close the form, my program will write a 1 or 0 depending on Checked/Unchecked. How do I get my program to search for text like
Code:
[Settings]
CheckBox1 = 1
And then somethign like,
If the program finds this text, it will check the checkbox on formloading
Last edited by werido; Oct 20th, 2009 at 05:26 PM.
-
Oct 20th, 2009, 05:58 PM
#11
Re: Saving Items in listbox
There now, that wasn't so hard, was it? Anyway, I'm not sure what else you intend to store in that file but it sounds a lot like an INI file from what you've described. There are plenty of examples of reading and writing INI files on the forum and elsewhere on the web.
That said, INI files are outdated and you should probably use the .NET configuration system instead, which has been introduced to essentially replace Registry use, which was introduced to replace INI files. If you want to store setting then I'd suggest that you use My.Settings. With the CheckBox you could even bind the Checked property to the setting so there'd be no code at all. For the ListBox you would use a StringCollection to store the items.
-
Oct 20th, 2009, 06:02 PM
#12
Thread Starter
Hyperactive Member
Re: Saving Items in listbox
Code:
SaveFile.WriteLine("[ListBoxItems]")
IO.File.WriteAllLines(Application.StartupPath & "\Settings.txt", Items)
While Debugging:
The process cannot access the file 'PathHere' because it is being used by another process.
When I use
Code:
SaveFile.WriteLine(Items.ToString)
I check the TXT file and it has
Code:
[ListBoxItems]
System.String[]
-
Oct 20th, 2009, 06:31 PM
#13
Re: Saving Items in listbox
You don't use a StreamWriter AND WriteAllLines. It's one or the other. WriteAllLines will create a StreamWriter itself internally, which it can't do if you've already got one open on the file.
-
Oct 20th, 2009, 08:55 PM
#14
Thread Starter
Hyperactive Member
Re: Saving Items in listbox
 Originally Posted by jmcilhinney
You don't use a StreamWriter AND WriteAllLines. It's one or the other. WriteAllLines will create a StreamWriter itself internally, which it can't do if you've already got one open on the file.
So I basically change all my SaveFile.WriteLines to the WriteAllLines?
Also, How would I get it to read part of the TXT file?
-
Oct 20th, 2009, 09:00 PM
#15
Re: Saving Items in listbox
 Originally Posted by werido
So I basically change all my SaveFile.WriteLines to the WriteAllLines?
That depends on which is more appropriate in your situation. WriteAllLines will write an entire String array to a file, overwriting everything it currently contains. Is that what you want? If so then call WriteAllLines, otherwise don't.
 Originally Posted by werido
Also, How would I get it to read part of the TXT file?
The StreamReader is the complement to the StreamWriter and File.ReadAllLines is the complement to File.WriteAllLines.
-
Oct 20th, 2009, 09:03 PM
#16
Thread Starter
Hyperactive Member
Re: Saving Items in listbox
 Originally Posted by jmcilhinney
That depends on which is more appropriate in your situation. WriteAllLines will write an entire String array to a file, overwriting everything it currently contains. Is that what you want? If so then call WriteAllLines, otherwise don't.The StreamReader is the complement to the StreamWriter and File.ReadAllLines is the complement to File.WriteAllLines.
How do I
-Use WriteAllLines without overwriting?
-Use File.ReadAllLines to read inbetween text, example,
Reading text between Settings and Other, ignore everything else
Settings
1
0
1
Other
text
text
text
-
Oct 20th, 2009, 09:28 PM
#17
Re: Saving Items in listbox
 Originally Posted by werido
How do I
-Use WriteAllLines without overwriting?
You don't.
 Originally Posted by werido
How do I
-Use File.ReadAllLines to read inbetween text
You don't.
Read what I posted again:
WriteAllLines will write an entire String array to a file, overwriting everything it currently contains. Is that what you want? If so then call WriteAllLines, otherwise don't.
WriteAllLines does what it does. If what it does is not what you want then don't use it. The same goes for ReadAllLines. If you don't want to read all the lines in the file then don't call ReadAllLines. If you want line-by-line control over what you read and write then you need to use StreamReaders and StreamWriters.
That said, when writing a file you have two choices: overwrite the entire file or append to it. You can just edit part of it. If you only want to change part of the file then you have to read it all in, make the change and write it all back out.
So, when reading you basically have two choices:
1. Call ReadAllLines to create a String array and then work with that array.
2. Use a StreamReader and call ReadLine repeatedly, working with one line at a time.
Similarly, you have two choices when writing:
1. Create a String array that contains all the lines of the file and call WriteAlllines.
2. Use a StreamWriter and call WriteLine repeatedly, writing one line at a time.
-
Oct 20th, 2009, 10:02 PM
#18
Thread Starter
Hyperactive Member
Re: Saving Items in listbox
 Originally Posted by jmcilhinney
You don't.You don't.
Read what I posted again:WriteAllLines does what it does. If what it does is not what you want then don't use it. The same goes for ReadAllLines. If you don't want to read all the lines in the file then don't call ReadAllLines. If you want line-by-line control over what you read and write then you need to use StreamReaders and StreamWriters.
That said, when writing a file you have two choices: overwrite the entire file or append to it. You can just edit part of it. If you only want to change part of the file then you have to read it all in, make the change and write it all back out.
So, when reading you basically have two choices:
1. Call ReadAllLines to create a String array and then work with that array.
2. Use a StreamReader and call ReadLine repeatedly, working with one line at a time.
Similarly, you have two choices when writing:
1. Create a String array that contains all the lines of the file and call WriteAlllines.
2. Use a StreamWriter and call WriteLine repeatedly, writing one line at a time.
Ill go with the ReadLine using StreamReader. Could you give me an example on that?
Also, when I use the code you save me, SaveFile.WriteLine(Items)
The textfile gets this
-
Oct 20th, 2009, 10:38 PM
#19
Re: Saving Items in listbox
 Originally Posted by werido
Also, when I use the code you save me, SaveFile.WriteLine(Items)
The textfile gets this
That's not the code I gave. As I have said, File.WriteAllLines writes an entire String array in one go. If you've created a StreamWriter and you're calling WriteLine then you're only writing one line at a time. Notice how one has "All" in the name and the other doesn't. If you're only writing one line at a time then you're not going to pass a String array, are you?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|