Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Visual basic code adding 3 of each item to listbox?

I am having a problem with a program i am creating in visual basic. When it reads in the data and adds it to a listbox it adds 3 of each item not one.

eg. 12352 Red Shoe 2

12352 Red Shoe 2

12352 Red Shoe 2

I'm sure its a simple problem as earlier while trying to make it work i got ride of the problem. But at the time it wasn't working so i wiped out the bit of code that fixed this problem.... :(

Here is my code:

Private Sub btnGetData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetData.Click

Dim sr As StreamReader = New StreamReader("parts.csv")

Dim storagelist As New List(Of numbers)

Dim numarray(0) As String

Try

While sr.Peek <> -1

' Trying to split the data

numarray = sr.ReadLine.Split(",")

Dim num1 As New numbers

num1.Ppartnumber = numarray(0)

num1.Ppartname = numarray(1)

num1.Pinventorybalance = numarray(2)

For Each w As String In numarray

storagelist.Add(num1)

Next

End While

sr.Close()

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

'''''''''''''''''

' Works but adds everything 3 times and is rather messy.

For Each test1 As numbers In storagelist

lstData.Items.Add(test1.Ppartnumber & "," & (test1.Ppartname) & "," & (test1.Pinventorybalance))

Next

End Sub

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    Take this loop out:

    For Each w As String In numarray

    storagelist.Add(num1)

    Next

    and just use:

    storagelist.add(num1.Ppartnumber, num1.Ppartname) ' or whatever data you want displayed

    Source(s): VB.NET Programmer
Still have questions? Get your answers by asking now.