简体   繁体   中英

How to save values from input file to be used later in Visual Basic?

The goal of this visual basic program is to store inputs from a file, then output them in a label. They should be displayed one value at a time. My idea is to store the values in a index, but it's clunky, and it does not work.

The program has a next and previous buttons that increase and decrease the value of the index in the output.

This is the code I'm using.

    Public Sub FileModeButton_Click(sender As System.Object, e As System.EventArgs) Handles FileModeButton.Click

    Dim Result As DialogResult = OpenFileDialog.ShowDialog()

    If Result = DialogResult.Cancel Then
        Return
    End If

    FileOpen(1, OpenFileDialog.FileName, OpenMode.Input)

    Index = 1
    Do While Not EOF(1)
        'Need to keep more than one stored set of outputs.
        Input(1, FileOutput1)
        Input(1, FileOutput2)

        FileOut1(Index) = FileOutput1
        FileOut2(Index) = FileOutput2

        Index += 1
    Loop
    FileClose(1)

End Sub

     Private Sub ApplyButton_Click(sender As System.Object, e As System.EventArgs)         Handles ApplyButton.Click
    Index = 1

    If ModeComboBox.Text = "File" Then
        FileModeGroupBox.Enabled = True
        TextboxModeGroupBox.Enabled = False
        Output1Label.Text = FileOut1(Index)
        Output2Label.Text = FileOut2(Index)
    End If
    End Sub

Your file reading is not really optimal. Try this instead (import system.IO):

Dim readText() As String = File.ReadAllLines(OpenFileDialog.FileName);

Then you can just do (readText must be a global variable at this point,same goes for index):

Output1label.Text = readText(index)
Output2label.text = readText(index)

On Next and Prev buttons do:

index = index+1 (on prev index-1)
Output1label.Text = readText(index)
Output2label.text = readText(index)

Also don't forget to check readText.Length to make sure you don't go out of the array size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM