将文件内容移动到数组中

时间:2016-10-17 09:05:46

标签: arrays vb.net

我正在努力让这段代码工作,以便我可以读取文本文件,将内容移动到数组中然后显示某个列(例如价格)

Imports System.IO
Public Class Form1
Dim FileName As String
Dim i As Integer = 0
Dim Alpha As Integer = 0
Dim Products(31) As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    FileName = "Products.txt"
End Sub
Private Sub btnCreateArray_Click(sender As Object, e As EventArgs) Handles btnCreateArray.Click
    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("Products.txt")
        MyReader.TextFieldType = FileIO.FieldType.Delimited
        MyReader.SetDelimiters(",")
        Dim currentRow As String()

        While Not MyReader.EndOfData
            currentRow = MyReader.ReadFields()
            Dim currentField As String
            For Each currentField In currentRow
                'MsgBox(currentField)
                'txtShowNo.Text = currentField
                'txtShowP.Text = i
                i = i + 1
                Products(i) = currentField
            Next
        End While
    End Using
    Do While Alpha <= i
        If InStr((txtFileSearch.Text), (Products(Alpha))) Then
            lstDisplayFile.Items.Add(Products(Alpha))
            Alpha = Alpha + 1
        End If
    Loop
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
    txtFileSearch.Text = ""
End Sub
Private Sub btnAddToFilePrintFile_Click(sender As Object, e As EventArgs) Handles btnAddToFilePrintFile.Click
    Dim Name As String
    Dim SName As String
    Dim IDNo As Integer
    Name = txtName.Text
    SName = txtSName.Text
    IDNo = txtIDNo.Text
    FileOpen(1, FileName, OpenMode.Append)          ' create a new empty file & open it in append mode'
    WriteLine(1, IDNo, Name, SName)                         ' Writes a line of data'
    FileClose(1)
    txtName.Text = ""
    txtSName.Text = ""
    txtIDNo.Text = ""
    txtName.Focus()
End Sub
End Class

程序在这行代码崩溃了:

lstDisplayFile.Items.Add(Products(Alpha))

以及以下消息:

An unhandled exception of type 'System.ArgumentNullException' occurred in System.Windows.Forms.dll

Alpha是我的计数器,我的思考过程就是如果文本框中的输入当前在数组中,它将在数组中显示已完成的文本。 以下是我的文本文件中的当前内容:

  1. “£5.00”,“50”
  2. “2.50£”, “30”
  3. 如果有人能帮我解决这个问题,我会很感激:)

0 个答案:

没有答案