Solved

How to save and re-use listbox values?

Posted on 2006-11-01
5
187 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!
0
Comment
Question by:sasllc
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
Jayadev Nair earned 250 total points
ID: 17849268
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
 
LVL 9

Expert Comment

by:jrscherer
ID: 17849956
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
 
LVL 3

Author Comment

by:sasllc
ID: 17853998
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
 
LVL 6

Expert Comment

by:Jayadev Nair
ID: 17857777
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
 
LVL 9

Assisted Solution

by:jrscherer
jrscherer earned 250 total points
ID: 17857969
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

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question