We help IT Professionals succeed at work.

How to save and re-use listbox values?

sasllc
sasllc asked
on
Medium Priority
206 Views
Last Modified: 2010-04-23
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!
Comment
Watch Question

Sr.Software Engineer
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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

Author

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?
Jayadev NairSr.Software Engineer

Commented:
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
Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.