How to save and re-use listbox values?

I need to understand how to best save listbox values when I leave a form, and later reload those values into the listbox when I go back into that form.  Note that my program is running in a CE environment, so I need to be careful with resources.

I have a text box (txtScanBale) where the user keys or scans in numbers, one at a time, and as he does, I save each entry in a listbox (lstBales), with the most recent entry at the top, using this code:

Me.lstBales.Items.Insert(0, Me.txtScanBale.Text)

If Me.lstBales.Items.Count > 200 Then

Me.lstBales.Items.RemoveAt(200)

End If

And, as shown, I limit it to the 200 most recent entries.  But when I leave the form, the listbox values all go away (as far as I know), because when I come back to the form, the listbox is empty.  

I'm wondering if there is an easy way to load these listbox entries into memory--maybe as an array??--either at the time I'm adding them to the listbox, or as I leave the form.  Or, maybe a way to save the listbox values themselves somehow, so they don't 'disappear' when I leave the form?  And then when I come back to this form, I need to be able to easily reload these entries into the listbox, in the same order they were in when I left the form.

If someone can give me a code example, or point me to a resource, it would be greatly appreciated!
LVL 3
sasllcAsked:
Who is Participating?
 
Jayadev NairConnect With a Mentor Application DeveloperCommented:
Hi

"But when I leave the form, the listbox values all go away (as far as I know), because when I come back to the form, the listbox is empty."

This sounds like you are disposing (closing) the listboxForm and calling the other form. When you try to show back the same form you might be probably creating new instance of ListBoxForm which made you loose the Listbox values which were in the Old Form instance which already close.

'> You shows listbox form
ListBoxForm.Show

'> SHows another form
ListBoxForm.Close()   '> Here you loose values
OtherForm=New Form
OtherForm.Show

'> Again Go back to ListBox Form
ListBoxForm = New Form() '> This is new instance and donot have listbox values
ListBoxForm.Show()

So either you keep the ListBoxForm unclosed or keep the ListBox values globally.

Hope this helped you
Thanks.
0
 
jrschererCEOCommented:
You say "When I leave the form" What do you mean? Do you close the form? Or do you just activate another form, such as a Child form and come back later?
Please explain the workflow you use.
Jack.net
0
 
sasllcAuthor Commented:
Yes, I close the form, which I'm sure is why the listbox values are cleared from memory.

I do need to close the listbox form in my project.  So how do I 'keep the listbox values globally'?  Is there a code example for that somewhere?
0
 
Jayadev NairApplication DeveloperCommented:
Do you have any form that lives all the time app runs? Then declare in it or you can just declare
Public Shared values() as String in a module or in a live form and access it from wherever you want

Thanks
0
 
jrschererConnect With a Mentor CEOCommented:
If you don't have any form open you could use as a host for a Public Shared string-array, then you probably need to write it on disk and retrieve it when you reload the form.
An easy way to do this safely is to serialize a class or structure containing the string array (SOAP or Binary) and write it in the FormClosing event and retrieve it in the Load event. Let me know if you need code snippets  Jack.net
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.