Solved

How to save and re-use listbox values?

Posted on 2006-11-01
5
183 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now