-1
私はvb.netを使用してプログラムを終了しようとしていました。 "Stock.xlsx"というファイルをDataGridViewにインポートする際に、Backgroundworkderを使用するプログラム。しかし、私がスタートボタンをクリックすると、 "インデックスが配列の境界外にある"というエラーが発生します。誰かが私を助けてください。backgroundworderエラー - インデックスが配列の範囲外です。vb.net
プログラムとスナップショットエラー下記をご覧ください:
パブリック・クラスのForm1 strRow(3)
またはstrRow(4)
を行うときは、第四にアクセスしようとしている
Dim strRow As String() 'String array to read all fields in a row
Dim dblAmount As Double 'Variable for total amount
Dim blnReported As Boolean = True 'Flag to check progress report completed or not
'Create TextFieldParser class to parse the stock.csv file in the current location
Dim txtFile As FileIO.TextFieldParser
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Allow background operation to be cancelled
bgWorker.WorkerSupportsCancellation = True
'Allow background operation to report progress
bgWorker.WorkerReportsProgress = True
'Disable Stop Button. It is enabled only after start
'button is pressed
btnStop.Enabled = False
'set status message
lblStatus.Text = "Press Start to import data from csv."
'Add columns to the grid
dgvCSVData.Columns.Add("colNo", "No")
dgvCSVData.Columns("colNo").Width = 30
dgvCSVData.Columns.Add("colDate", "Date")
dgvCSVData.Columns("colDate").Width = 60
dgvCSVData.Columns.Add("colItem", "Item")
dgvCSVData.Columns("colItem").Width = 120
dgvCSVData.Columns.Add("colQty", "Quantity")
dgvCSVData.Columns("colQty").Width = 50
dgvCSVData.Columns.Add("colUnit", "Unit")
dgvCSVData.Columns("colUnit").Width = 30
dgvCSVData.Columns.Add("colAmt", "Amt")
dgvCSVData.Columns("colAmt").Width = 60
End Sub
Private Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStart.Click
'Disable Start button and Enable Stop
btnStart.Enabled = False
btnStop.Enabled = True
'set status message
lblStatus.Text = "Importing data from CSV file."
'Start time-consuming operation in background
Call bgWorker.RunWorkerAsync()
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
'Enable Start button and disable Stop
btnStart.Enabled = True
btnStop.Enabled = False
'Stop background operation
bgWorker.CancelAsync()
'set status message
lblStatus.Text = "Import cancelled. Press Start again."
End Sub
Private Sub bgWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker.DoWork
Dim intCount As Int16 = 0
txtFile = New FileIO.TextFieldParser("stock.xlsx")
'Specify structure of the file
txtFile.TextFieldType = FileIO.FieldType.Delimited
txtFile.SetDelimiters(",")
'Skip header row
txtFile.ReadFields()
'Start reading data from file
While Not txtFile.EndOfData
If bgWorker.CancellationPending Then
e.Cancel = True
Exit Sub
Else
'Wait for Progress Report to finish
While Not blnReported
Application.DoEvents()
End While
'Read all field in a row into a string array
strRow = txtFile.ReadFields()
'Do some calculations and assign value to data grid
dblAmount = CDbl(strRow(3)) * CDbl(strRow(4))
'Add some sleep to simulate a long running operation
System.Threading.Thread.Sleep(500)
'Progress report pending
blnReported = False
'increment counter
intCount += 1
'Report the progress
bgWorker.ReportProgress(10, intCount)
End If
End While
End Sub
Private Sub bgWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgWorker.ProgressChanged
'Copy values to data grid
dgvCSVData.Rows.Add(strRow)
dgvCSVData.Rows(dgvCSVData.CurrentRow.Index - 1).Cells("colAmt").Value = dblAmount
pgbCopyProgress.Value = e.UserState
'Progress report finished
blnReported = True
End Sub
Private Sub bgWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted
If e.Cancelled Or Not IsNothing(e.Error) Then
'Clear the data grid
dgvCSVData.Rows.Clear()
Else
'Progress completed
pgbCopyProgress.Value = 100
'Release txtfile
txtFile.Dispose()
btnStop.Enabled = False
btnStart.Enabled = True
End If
End Sub
エンドクラス