Results 1 to 19 of 19

Thread: [RESOLVED] Saving Items in listbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    Resolved [RESOLVED] Saving Items in listbox

    How do I save the contents of a listbox?

  2. #2
    Fanatic Member
    Join Date
    Jun 2008
    Location
    Portland, OR, USA
    Posts
    659

    Re: Saving Items in listbox

    Saving to Where? A database? a file?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    Re: Saving Items in listbox

    Quote Originally Posted by RunsWithScissors View Post
    Saving to Where? A database? a file?
    File/My.Settings

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving Items in listbox

    Quote Originally Posted by werido View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    Re: Saving Items in listbox

    Quote Originally Posted by jmcilhinney View Post
    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving Items in listbox

    Assuming that the ListBox contains just Strings:
    vb.net Code:
    1. Dim items(Me.ListBox1.Items.Count - 1) As String
    2.  
    3. Me.ListBox1.Items.CopyTo(items, 0)
    4. IO.File.WriteAllLines("file path here", items)
    or with LINQ:
    vb.net Code:
    1. IO.File.WriteAllLines("file path here", _
    2.                       Me.ListBox1.Items.Cast(Of String)().ToArray())
    To reload:
    vb.net Code:
    1. Me.ListBox1.Items.AddRange(IO.File.ReadAllLines("file path here"))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    Re: Saving Items in listbox

    Quote Originally Posted by jmcilhinney View Post
    Assuming that the ListBox contains just Strings:
    vb.net Code:
    1. Dim items(Me.ListBox1.Items.Count - 1) As String
    2.  
    3. Me.ListBox1.Items.CopyTo(items, 0)
    4. IO.File.WriteAllLines("file path here", items)
    or with LINQ:
    vb.net Code:
    1. IO.File.WriteAllLines("file path here", _
    2.                       Me.ListBox1.Items.Cast(Of String)().ToArray())
    To reload:
    vb.net Code:
    1. 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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving Items in listbox

    Quote Originally Posted by werido View Post
    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?
    Quote Originally Posted by werido View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    Re: Saving Items in listbox

    Quote Originally Posted by jmcilhinney View Post
    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.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    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[]

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    Re: Saving Items in listbox

    Quote Originally Posted by jmcilhinney View Post
    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?

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving Items in listbox

    Quote Originally Posted by werido View Post
    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.
    Quote Originally Posted by werido View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    Re: Saving Items in listbox

    Quote Originally Posted by jmcilhinney View Post
    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

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving Items in listbox

    Quote Originally Posted by werido View Post
    How do I
    -Use WriteAllLines without overwriting?
    You don't.
    Quote Originally Posted by werido View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    Re: Saving Items in listbox

    Quote Originally Posted by jmcilhinney View Post
    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
    Code:
    System.String[]

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving Items in listbox

    Quote Originally Posted by werido View Post
    Also, when I use the code you save me, SaveFile.WriteLine(Items)
    The textfile gets this
    Code:
    System.String[]
    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width