Нужна помощь в исправлении Exception Unhandled - PullRequest
0 голосов
/ 01 октября 2019

Я получаю следующую ошибку:

System.ArgumentOutOfRangeException: 'Индекс и длина должны указывать на местоположение в строке. Имя параметра: длина '

(Ищите жирный курсив по коду ***, вот где мне нужно это исправить)

Не уверен, в чем проблема. Вот весь код:

Imports GroceryApp.GroceryItem
Imports System.IO

Public Class GroceryItemForm
    Private strFileName As String = String.Empty
    Private Sub btnAddToBasket_Click(sender As Object, e As EventArgs) Handles btnAddToBasket.Click, AddToolStripMenuItem.Click
        Dim gi As GroceryItem
        Dim price As Double

        ' Validate that brand name is entered
        If txtBrandName.Text = "" Then
            MsgBox("Please input an Brand Name", , "Value Required")
            txtBrandName.Focus()
            Exit Sub
        End If

        ' Validate that price is entered
        If Not Double.TryParse(numPrice.Text, price) Then
            MsgBox("Please input an Price", , "Value Required")
            numPrice.Focus()
            Exit Sub
        End If

        ' Validate that Aisle is selected
        If cboAisle.Text = "" Then
            MsgBox("Please select an Aisle", , "Value Required")
            cboAisle.Focus()
            Exit Sub
        End If

     ***txtScanNumber.Text = txtBrandName.Text.Substring(0, 3) & "1019"***

        gi = New GroceryItem(txtScanNumber.Text, txtBrandName.Text, price)
        gi.Type = [Enum].Parse(GetType(Aisle), cboAisle.Text)
        gi.Description = txtDescription.Text

        basket.Add(gi)

    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub ViewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewToolStripMenuItem.Click
        'Dim result As String = ""
        'Dim i As Integer = 1
        'For Each gi As GroceryItem In basket
        'result = result & "Item " & i & vbNewLine & "Aisle: " & gi.Type.ToString & vbNewLine & "Scan Number: " & gi.ScanNumber & vbNewLine & "Brand Name: " & gi.BrandName & vbNewLine & vbNewLine
        'i = i + 1
        'Next

        'MsgBox(result, , "Basket Details")
        Dim oForm As BasketDisplayForm
        oForm = New BasketDisplayForm()
        oForm.Show()
        oForm = Nothing


    End Sub

    Private Sub GroceryItemForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim oForm As LoginForm
        oForm = New LoginForm()
        oForm.Show()
        oForm = Nothing
    End Sub

    Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
        Dim rowLine As String = ""
        'If strFileName = String.Empty Then
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            strFileName = SaveFileDialog1.FileName

            Dim fsStream As New FileStream(strFileName, FileMode.Append, FileAccess.Write)
            Dim sw As New StreamWriter(fsStream)

            Dim sb As New System.Text.StringBuilder
            For Each Item As GroceryItem In basket
                sb.AppendLine(String.Concat(Item.ScanNumber, ",", Item.Type.ToString, ",", Item.BrandName, ",", Item.Description, ",", Item.Price))
                'rowLine = rowLine + Item.ScanNumber + "," + Item.Type.ToString + "," + Item.BrandName + "," + Item.Description + "," + Item.Price.ToString
            Next
            'IO.File.WriteAllText(strFileName, sb.ToString)
            'rowLine = rowLine.Remove(rowLine.Length - 1, 1)
            sw.WriteLine(sb)
            sw.Flush()
            MsgBox("Data Saved Successfully")
            sw.Close()
            basket.Clear()

        End If
        'End If
    End Sub

    Private Sub LoadToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoadToolStripMenuItem.Click
        Dim basketFile As StreamReader
        Dim gi As GroceryItem
        Dim sNo, brand, desc, aisle As String
        Dim price As Double
        Dim y As Integer

        basket.Clear()

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            strFileName = OpenFileDialog1.FileName
            basketFile = File.OpenText(strFileName)

            ' Read Power rating from file
            Using sr As StreamReader = New StreamReader(strFileName)
                Do While sr.Peek() > -1
                    For Each c As String In sr.ReadToEnd().Split(CType(Chr(10), Char))
                        sNo = ""
                        For Each d As String In c.Split(",")
                            If y = 0 Then
                                sNo = d
                            End If
                            If y = 1 Then
                                aisle = d
                            End If
                            If y = 2 Then
                                brand = d
                            End If
                            If y = 3 Then
                                desc = d
                            End If
                            If y = 4 Then
                                price = d
                            End If
                            y += 1
                        Next

                        If (sNo <> "") Then
                            gi = New GroceryItem(sNo, brand, price)
                            gi.Type = [Enum].Parse(GetType(Aisle), aisle)
                            gi.Description = desc

                            If (sNo <> "" & vbCr & "") Then
                                basket.Add(gi)
                            End If
                        End If
                        y = 0
                    Next
                Loop
            End Using

            basketFile.Close()
        End If
    End Sub
End Class
...