CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2009
    Posts
    6

    Question Save content of listbox

    Hello.

    I am using Visual Studio 2008 which should be VB.NET 3.0 language.

    I have searched and searched for finding the answer on saving content / items in a listbox with strings. As you know the code for saving a text in textbox and get information again in application is:

    Code:
            Dim stringtxt As String
    
            stringtxt = textbox1.text
            My.Settings.mytextstring = stringtxt
    and load it you do
    Code:
            textbox1.text = My.Settings.mytextstring
    But i do not know how to save and load the items in a listbox - is it even possible?

    Thank you for your help.

    sorry for my bad english, i am from denmark

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Save content of listbox

    To save you would loop through the items in the listbox one by one until you reach the end. To load you would clear the list box and read your saved list adding each item to the list as you go.

  3. #3
    Join Date
    Jun 2009
    Posts
    6

    Re: Save content of listbox

    So it isn't possible with string (in My.Settings) ?

    How would the code look example.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Save content of listbox

    Not really. You'd have to save an ARRAY LIST of ITEMS, and then parse them by hand to load them. If you save them into a table, then you can bind to the table automatically.

    Search around for the avenue to want to pursue. There are lots of examples on each method
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Save content of listbox

    Junday

    I did not say it wasn't possible to save in My.Settings but you are not dealing with a single text property here. You are dealing with any number of strings, 1 per list item. It is possible to read them in one by one and add them to a string with a seperator and then save that string doing the reverse to load them. It is possible to save each entry as a seperate string, It is possible to save them to a file of your choice, a database, registry or whatever you like but you need to loop through the list box to do so or at least you should. The only way that I am aware of that would not require looping through the list would be binding to a database and that has its own set of problems.

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

Featured