For Each Item in ListBox1 do something then add item to listbox2 vb

GHoStyaiRo picture GHoStyaiRo · Nov 1, 2013 · Viewed 20.5k times · Source

I made an app to convert certain numbers to other format
i.e

  • 1 = A
  • 2 = B
  • 3 = C
  • 4 = D
  • 5 = E
  • ETC

I have made that function with no problem and I have been using it for quite sometime, but now I would like to do things faster and in a batch.
So it's really difficult for me to copy from a text file to my Textbox1 then press button1 then copy textbox2 to other text file.

So I was thinking in loading the text file into a List Box then do a loop for each item in that list into a second list that I can export to another text file.

The importing and exporting I have it covered but where I'm stuck at is to make the loop.

Here's what I have please if you know a better way to do it let me know or tell me how to fix it this way.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Using FD As New OpenFileDialog()
        FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(IO.File.ReadAllLines(FD.FileName))
        End If
    End Using
End Sub


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Do
    Dim Item As String = ""
    For Each i As String In ListBox1.Items
        Item &= i
        TextBox1.Text = Item
        TextBox2.Text = MYFUNCTION(TextBox1.Text)
        ListBox2.Items.Add(TextBox2.Text)
        TextBox1.Text = ""
        TextBox2.Text = ""
    Next
    Loop Until TextBox1.Text = "END"

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    'TextBox2.Text = MeidHexToDec(TextBox1.Text)

    Using FD As New SaveFileDialog()
        FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim FileContent As String = ""
            For Each i As String In ListBox2.Items
                FileContent &= i & vbCrLf
            Next
            IO.File.WriteAllText(FD.FileName, FileContent)
        End If
    End Using
End Sub

So my final aim is to do something like this:

TextFile1.txt

  • 1
  • 2
  • 5
  • 5
  • 1
  • 3
  • 2
  • END

then after the conversion output

TextFile2.txt

  • A
  • B
  • E
  • E
  • A
  • C
  • B

The text file size will vary sometimes it will have only 10 items sometimes will be 50... Thanks.

Answer

Karl Anderson picture Karl Anderson · Nov 1, 2013

You do not need the Do loop at all and you can simplify the rest of your loop logic, like this:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    For Each i As String In ListBox1.Items
        ListBox2.Items.Add(MYFUNCTION(i))
    Next
End Sub

You do not need to look out for the END marker, because everything in the file was read into the ListBox1.Items collection, thus once you have looped through all of the string values in ListBox1.Items, then you are at the end of the file.

The MYFUNCTION logic returns the transformation from number to letter, thus just add the result of that function into ListBox2.Items and you are done.