0

我无法让下面的代码正常工作,实际上我想按下后退按钮以便逐行上移文本文件,然后将链切割成 3 部分。

Dim LinesOfText() As String = System.IO.File.ReadAllLines("c:\essai.librairie", Encoding.UTF8)
    For line As Integer = LinesOfText.Length - 1 To 0 Step -1

        Dim currentLine As String = LinesOfText(line)

        Dim value As String = currentLine
        Dim startIndex As Integer = 0
        Dim length As Integer = 17

        Dim substring As String = value.Substring(startIndex, length)
        Dim subs As String = value.Substring(17, 90)
        Dim subst As String = value.Substring(107, 120)

        TextBox1.Text = substring
        TextBox2.Text = subs
        TextBox3.Text = subst

    Next

在此处输入图像描述

4

1 回答 1

1

如果您存储行和行计数器以便它们在需要的范围内,您可以这样做:

Imports System.IO

Public Class Form1

    Dim lines As String()
    Dim currentLine As Integer = -1

    Sub LoadData(filename As String)
        lines = File.ReadAllLines(filename)
        currentLine = lines.Length - 1

    End Sub

    Sub ShowCurrentLine()
        If currentLine >= 0 Then
            TextBox1.Text = lines(currentLine).Substring(0, 3)
            TextBox2.Text = lines(currentLine).Substring(4, 3)
            TextBox3.Text = lines(currentLine).Substring(8, 3)
            lblLineNo.Text = (currentLine + 1) & "/" & lines.Length
        Else
            TextBox1.Clear()
            TextBox2.Clear()
            TextBox3.Clear()
            lblLineNo.Text = "-"
        End If

    End Sub

    Private Sub bnNext_Click(sender As Object, e As EventArgs) Handles bnNext.Click
        If currentLine < lines.Length - 1 Then
            currentLine += 1
            ShowCurrentLine()
        End If

    End Sub

    Private Sub bnPrev_Click(sender As Object, e As EventArgs) Handles bnPrev.Click
        If currentLine > 0 Then
            currentLine -= 1
            ShowCurrentLine()
        End If

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData("C:\temp\SO67209265.txt")
        ShowCurrentLine()

    End Sub

End Class

您需要为您的数据设置子字符串参数(我只使用了一个小文本文件)以及要加载的文件名。

标签lblLineNo用于显示当前行号,从 1 开始。

在此处输入图像描述

于 2021-04-25T16:48:44.497 回答