473,471 Members | 4,687 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Filling a listbox with collection items

I have set up a test program on how to use colletions saved and loaded from
a file. It was a part of a other program until this happends.
The saving process working fine but it looks like something goes wrong in
the read and refill the listbox. Say there is three items listed in the
file. It semes that the reading process works but I only get the last item
three times insted of three different one.

This is the reading code that was put in a module
Public Excludelist As New ExListCollection

Public Fexclude As New ExFile

Dim path As String = "c:\MyTest.txt"

Dim fs As FileStream

Public Sub ReadExclude()

Try

Dim sr As StreamReader = File.OpenText(path)

Dim inp As String

Do While sr.Peek() >= 0

Fexclude.ExFilename = sr.ReadLine

Excludelist.Add(Fexclude)

MessageBox.Show(Fexclude.ExFilename)

Loop

sr.Close()

Catch e As Exception

MessageBox.Show("The process failed: {0}", e.ToString())

End Try

End Sub

And this was put in the forms form_Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim count As Integer

ReadExclude()

For count = 0 To Excludelist.Count - 1

MessageBox.Show("List " & Excludelist.Item(count).ExFilename)

lstExclude.Items.Add(Excludelist.Item(count).ExFil ename)

Next

End Sub

It looks like the collection as got the data from the file right but when it
is presented again it get wrong.
Have someone any clue what is wrong her ?
PetterL
Nov 21 '05 #1
2 1403
Petter,

Each time through your loop you need to create a new instance of the
Fexclude object:

Do While sr.Peek() >= 0

Fexclude = New ExFile '<--------------- create a new instance

Fexclude.ExFilename = sr.ReadLine

Excludelist.Add(Fexclude)

MessageBox.Show(Fexclude.ExFilename)

Loop
"Petter L" wrote:
I have set up a test program on how to use colletions saved and loaded from
a file. It was a part of a other program until this happends.
The saving process working fine but it looks like something goes wrong in
the read and refill the listbox. Say there is three items listed in the
file. It semes that the reading process works but I only get the last item
three times insted of three different one.

This is the reading code that was put in a module
Public Excludelist As New ExListCollection

Public Fexclude As New ExFile

Dim path As String = "c:\MyTest.txt"

Dim fs As FileStream

Public Sub ReadExclude()

Try

Dim sr As StreamReader = File.OpenText(path)

Dim inp As String

Do While sr.Peek() >= 0

Fexclude.ExFilename = sr.ReadLine

Excludelist.Add(Fexclude)

MessageBox.Show(Fexclude.ExFilename)

Loop

sr.Close()

Catch e As Exception

MessageBox.Show("The process failed: {0}", e.ToString())

End Try

End Sub

And this was put in the forms form_Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim count As Integer

ReadExclude()

For count = 0 To Excludelist.Count - 1

MessageBox.Show("List " & Excludelist.Item(count).ExFilename)

lstExclude.Items.Add(Excludelist.Item(count).ExFil ename)

Next

End Sub

It looks like the collection as got the data from the file right but when it
is presented again it get wrong.
Have someone any clue what is wrong her ?
PetterL

Nov 21 '05 #2
Petter,

Why not split up the problem yourself first.

First try it with an arraylist to get one field from a record
Than build your own class to hold the data which you add as an object to the
arraylist
Than make your own collection to make it complete.

You have now taken to much problems in one time, from which I get the idea
that you don't know exactly how to handle them.

In addition, be aware that the Listbox has self a fine itemArray which you
can fill with objects (and set than the displaymember to the property you
want to display).

Just my thought,

Cor
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Grant Schenck | last post by:
Hello, I have a ListBox control on a form. I add members of a class to the Items collection. They show up and I can select them. The text shown is from my classes ToString override. Now,...
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
6
by: Valerian John | last post by:
I have a ListBox webcontrol on an aspx page. Items are added to the ListBox using client-side code. However, when the page is posted back the items are missing/not available. (It is like the...
3
by: Stimp | last post by:
I have a listbox of values that I populate from a database. I want the user to be able to re-order the list (by first selecting an item and then clicking 'up' or 'down' buttons) and then save...
6
by: Janaka | last post by:
Help! I have two ListBox controls on my web form. The first one gets populated on entry with values from the DB. I then use JavaScript to copy options from this ListBox to my second one. (I...
2
by: COHENMARVIN | last post by:
I've been doing a simple for-each loop to remove multiple selected items from one listbox, and to dump them in another listbox. for each item in LeftListBox.Items if (item.Selected = true) Then...
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but...
0
by: Work007 | last post by:
Hi. I really need help on how to use a value stored in a listbox. I have a form on which it has the following code. This form allows the user to add a value to the textbox which is on this page...
5
by: markr1000 | last post by:
I must have looked searched in 500+ places that showed up in Google searchs, but not one has an example of what I want to do. I have a Listbox on a User Control because I want to control the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.