Turk Turk - 1 year ago 66
Vb.net Question

keep last few charaters in listbox VB.NET

I am trying to load a .txt file into a richtextbox (Point_BOX), then delete all but the last 5 characters into a listbox (Point_LIST).
I've searched online and so far the only way I can get it to work is by removing the first 75 characters from the line (the lines in the .txt file should be 80 characters but sometimes is more/less).

FileName = OpenPointDialog.FileName
Dim sr As IO.StreamReader = IO.File.OpenText(FileName)
Dim line As String = ""
Point_BOX.Text = sr.ReadToEnd
For i As Integer = 0 To sr.Peek = -1
line = sr.ReadLine()
Dim allText As String = sr.ReadToEnd()
Point_BOX.Text = Point_BOX.Text & line & vbNewLine

'Clean up report
Dim ir As Integer = Point_LIST.Items.Count
Dim xr As Integer
For xr = 0 To ir - 2
Point_LIST.Items(xr) = Point_LIST.Items(xr).substring(75)
Next xr

This works if there are no lines that are less than 80 characters but sometimes the report can have some single words in a line.
I thought about making another loop that checks how many characters are in a line and if it less than 80 then go to next line but I cant help but think there is a better way. Like reverse of "substring(75)" (string(5)?)

Answer Source

Try something like this instead:

    If OpenPointDialog.ShowDialog = DialogResult.OK Then
        FileName = OpenPointDialog.FileName
        Point_BOX.Lines = System.IO.File.ReadAllLines(FileName)
        For Each line As String In Point_BOX.Lines
            Point_LIST.Items.Add(Microsoft.VisualBasic.Strings.Right(line, 5))
    End If