Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27420

VS 2022 [RESOLVED] Text from text file into a ListBox

$
0
0
I have a text file that has the following on separate lines:

Skeleton
Goblin
Kobold
Vampire
Orc

I want to read the file and place them line-by-line in a Listbox. I have the following code which almost works:

Code:

Private Sub btnRefreshMonsterList_Click(sender As Object, e As EventArgs) Handles btnRefreshMonsterList.Click
    ' Load the monster file into the monster list box
    lstMonList.Items.Clear()
    Dim monfile As String
    Dim monline As String
    Dim monreader As StreamReader
    Dim monlist As New List(Of String)
    ' Get the path to the monster file
    monfile = txtAdventLoc.Text & "\AFFMonsters\MonsterList.txt"
    ' Open the monster file
    monreader = New StreamReader(monfile)
    ' Read the monster file line by line and add it to monlist until there is no more to read
    Do
        monline = monreader.ReadLine()
        If monline IsNot Nothing Then
            monlist.Add(monline)
        End If
    Loop Until monline Is Nothing
    ' Close the monster file
    monreader.Close()
    ' Populate the monster text box with the monster file
    lstMonList.Items.Add(String.Join(vbCrLf, monlist))
 End Sub

The problem is that it places it all on one line rather than one under the other.

I must admit, Copilot wrote that code initially but I kinda understand it and tweaked it. It seems not to be putting the newline in. Any help (and explanation) would be appreciated. :)

Viewing all articles
Browse latest Browse all 27420

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>