0

I cannot get my code below to work correctly, in fact I want to press a back button in order to go up the lines one by one of a text file then I cut the chain into 3 parts.

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

enter image description here

claudebl
  • 55
  • 1
  • 6
  • Are you saying that there should be a "current line" which has its parts displayed in TextBox1, TextBox2, and TextBox3? – Andrew Morton Apr 22 '21 at 10:42
  • Yes and no, it is a selected line, no matter which one, which is cut out. The split works. Only the fact of passing this selected line or another in order to go back to the preceding line. Thanks Andrew. – claudebl Apr 22 '21 at 11:27

1 Answers1

1

If you store the lines and a line counter so that they are in scope where needed, you can do it like this:

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

You will need to set the substring parameters for your data—I just used a small text file—and the filename to be loaded.

The label lblLineNo is used to show the current line number, starting at 1.

enter image description here

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • 1
    Super Andrew! You have exactly understood my concern. I thank you in advance. Beautiful achievement. Claude. I will test tomorrow, it is now time for my meal and bedtime! Thanks again. – claudebl Apr 25 '21 at 17:00