【问题标题】:Inventory Collection in Visual BasicVisual Basic 中的库存收集
【发布时间】:2014-06-02 02:54:40
【问题描述】:

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

关于商品的信息显示在右侧(描述、零售价、单位)

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

小计、税收和总计显示

用户点击完成购买按钮,出现确认订单框。用户单击“确定”,表单将清除以进行另一笔交易。

这就是我所做的: 关于为什么我不断收到此错误的任何建议"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

【问题讨论】:

  • 那里有很多代码。您可能会尝试指出引发异常的位置,这样我们就不必浪费时间尝试解决您已经知道的事情。请提供所有相关信息。
  • 我认为列表框无法读取库存集合。
  • Private Sub lstInventory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstInventory.SelectedIndexChanged Dim objInventory As Inventory '查看是否选择了一个项目 If lstInventory.SelectedIndex -1 Then Try objInventory = CType(inventoryCollection .Item(lstInventory.SelectedItem), Inventory) Catch ex As Exception '显示错误信息。 MessageBox.Show(ex.Message) Console.WriteLine("") End Try End If End Sub

标签: vb.net


【解决方案1】:

大概问题出在这里:

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

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

【讨论】:

  • 是的,我想我没有声明任何收藏。就是这样:将 InventoryCollection 调暗为新集合。我需要添加什么。
  • 您的评论听起来好像您没有阅读我的回答。如果要将 SelectedItem 用作索引,则每个项目都必须是有效索引。他们是吗? ListBox 中有哪些项目?
  • 知道了。我将索引这些项目并重试代码。希望有效
  • 如何索引项目,像这样:私有子索引(ByVal Inventory) InventoryItem(1).InventoryNumberString = "A804" InventoryItem(1).DescriptionString = "College Logo Tshirt" IInventoryItem(1)。 PartCost = 15 InventoryItem(1).OnHand = 18.99D End Sub
  • 我没有告诉你索引这些项目。请读字。项目本身必须是索引。我在之前的评论中问了你一个问题,我这样做是有原因的。 ListBox 中有哪些项目?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多