keep last few characters in listbox VB.NET

时光毁灭记忆、已成空白 提交于 2020-01-04 10:38:43

问题


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).

    Point_BOX.Clear()
    Point_LIST.Items.Clear()
    OpenPointDialog.ShowDialog()
    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
    Next
    sr.Close()

    'Clean up report
    Point_LIST.Items.AddRange(Point_BOX.Lines)
    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)?)


回答1:


Try something like this instead:

    Point_BOX.Clear()
    Point_LIST.Items.Clear()
    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))
        Next
    End If



回答2:


try this Microsoft.VisualBasic.Right(String, 5)



来源:https://stackoverflow.com/questions/36878946/keep-last-few-characters-in-listbox-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!