Visual Basic中的库存集合

时间:2014-06-02 02:54:41

标签: vb.net

这就是老师的要求: 用户从列表中选择一个项目

有关该项目的信息显示在右侧(描述,零售价格,单位)

用户输入数量并点击添加到购物车

小计,税金和总计显示

用户点击完成购买按钮,系统会显示确认订单框。用户单击“确定”,表单将清除另一个事务。

这就是我所做的: 关于我为什么一直收到此错误的任何建议"Argument Index is not a valid value"

Imports System.IO


Public Class MainForm

Const strFILENAME As String = "Inventory.txt"
Dim dblTaxRate As Double = 8.75

Dim InventoryCollection As New Collection

Public Sub AddRecord(ByVal InvItem As Inventory)
    Try


        inventoryCollection.Add(InvItem, InvItem.InventoryNumber)


    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub



Private Sub ClearMainForm()
    txtDesc.Text = String.Empty
    txtRetail.Text = ""
    txtOnHand.Text = ""
    txtInvNumber.Text = String.Empty
End Sub




Private Sub UpdateListBox()
    lstInventory.Items.Clear()

    Dim InvItem As Inventory

    For Each InvItem In inventoryCollection
        lstInventory.Items.Add(InvItem.InventoryNumber)
    Next
    If lstInventory.Items.Count > 0 Then
        lstInventory.SelectedIndex = 0
    Else
        ClearMainForm()
    End If

End Sub


Private Sub SaveRecord(ByVal objInventory As Inventory)

    Dim Writer As StreamWriter
    Try

        Writer = File.AppendText("Inventory.txt")
        Writer.WriteLine(objInventory.InventoryNumber)
        Writer.WriteLine(objInventory.Description)
        Writer.WriteLine(objInventory.Retail.ToString())
        Writer.WriteLine(objInventory.OnHand.ToString())


    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim objInventory As New Inventory
    Dim inventoryFile As System.IO.StreamReader
    Dim blnFound As Boolean = False
    Dim inventoryCollection As New Collection


    Try
        ' Open the file.
        If System.IO.File.Exists(strFILENAME) Then

        End If


        inventoryFile = System.IO.File.OpenText(strFILENAME)


        'Enter loop and read till end of file.
        Do Until inventoryFile.Peek = -1


            'Read lines from file, save into Inventory object properties.
            objInventory.InventoryNumber = inventoryFile.ReadLine
            objInventory.Description = inventoryFile.ReadLine
            objInventory.PartCost = inventoryFile.ReadLine
            objInventory.Retail = inventoryFile.ReadLine
            objInventory.OnHand = inventoryFile.ReadLine

            'Display data in text boxes.
            lstInventory.Items.Add(objInventory.InventoryNumber)




        Loop
        'Close the file.
        inventoryFile.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)

    End Try


End Sub

Private Sub DisplayInput(ByVal InvItem As Inventory)
    'Display from Collection to Label boxes

    Try

        txtDesc.Text = InvItem.Description
        txtOnHand.Text = InvItem.OnHand.ToString()
        txtRetail.Text = InvItem.Retail.ToString()
        txtInvNumber.Text = InvItem.InventoryNumber

    Catch ex As Exception
        MessageBox.Show(ex.Message)

    End Try
End Sub


Private Sub lstInventory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstInventory.SelectedIndexChanged

    Dim objInventory As Inventory

    'See if an Item is Selected
    If lstInventory.SelectedIndex <> -1 Then

        'Retrieve student's data from inventoryCollection. Convert object into Inventory object.
        Try

            objInventory = CType(inventoryCollection.Item(lstInventory.SelectedItem), Inventory)

        Catch ex As Exception
            'Display error message.
            MessageBox.Show(ex.Message)
            Console.WriteLine("")
        End Try
    End If
End Sub


Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    'Clear Form
    ClearMainForm()
End Sub

Private Sub btnExits_Click(sender As Object, e As EventArgs) Handles btnExits.Click
    'Close the Form
    Me.Close()
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click


    Dim InvID As New Inventory



    InventoryCollection.Add(InvID, InvID.InventoryNumber)

End Sub
End Class

Public Class Inventory


Private StrinvNumber As String
Private strdesc As String
Private decCost As Decimal
Private decretailPrice As Decimal
Private IntqtyOnHand As Integer



'Constructor
Public Sub New()
    StrinvNumber = String.Empty
    strdesc = String.Empty
    decCost = 0.0
    decretailPrice = 0.0
    IntqtyOnHand = 0.0

End Sub



Public Property InventoryNumber() As String
    Get
        Return StrinvNumber
    End Get
    Set(ByVal value As String)
        StrinvNumber = value
    End Set
End Property

Public Property Description() As String
    Get
        Return strdesc
    End Get
    Set(ByVal value As String)
        strdesc = value
    End Set
End Property

Public Property PartCost() As Decimal
    Get
        Return decCost
    End Get
    Set(ByVal value As Decimal)
        decCost = value
    End Set
End Property

Public Property Retail() As Decimal
    Get
        Return decretailPrice
    End Get
    Set(ByVal value As Decimal)
        decretailPrice = value
    End Set
End Property

Public Property OnHand() As Integer
    Get
        Return IntqtyOnHand
    End Get
    Set(ByVal value As Integer)
        IntqtyOnHand = value

    End Set
End Property




End Class

1 个答案:

答案 0 :(得分:0)

据推测问题在于:

objInventory = CType(inventoryCollection.Item(lstInventory.SelectedItem), Inventory)

lstInventory是否包含Integer值并且这些值是inventoryCollection的有效索引吗?我不指望。这两个列表是否应该相互对应?如果是,那么您应该使用SelectedIndex而不是SelectedItem。可能你应该首先将列表绑定到ListBox,然后SelectedItem就是你需要的对象。