好的,所以我的大学工作的一部分得到了帮助,但是现在还有一步要做:显示数据。现在,有问题的分配让用户能够通过 ID 号搜索产品,在表单上的 TextBox 控件中显示它的描述、每日、每周、每月租金以及库存数量,数据来自另一个类的字典。这也是出于编辑目的(请参阅下面的注释)。此外,在读入并将项目添加到 Dictionary 时,我在将 ID 变量添加到 ComboBox 的集合中时也遇到了问题,我敢肯定,这应该是一个简单的修复,但我只是想要一些帮助来记住它是什么,仅此而已。
这是 frmMain 的代码,其中包含显示按钮的代码(请不要介意它看起来有多粗糙):
Imports System.IO
Public Class frmMain
Dim dItem As New Inventory
Dim filePath = ofdOpenFile.FileName
Public Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim ID As String = cboID.Text
Dim Desc As String = txtDescription.Text
Dim DRate As Double = txtDaily.Text
Dim WRate As Double = txtWeekly.Text
Dim MRate As Double = txtMonthly.Text
Dim Quantity As Integer = txtQuantity.Text
dItem.addItem(ID, Desc, DRate, WRate, MRate, Quantity)
End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
Dim ID As String = cboID.Text
dItem.removeItem(ID)
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
' Display data should go here
End Sub
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If ofdOpenFile.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim FileInfo As New InventoryFile
FileInfo.RFile(filePath, dItem)
cboID.DataSource = dItem.returnKeys()
End If
End Sub
End Class
这是我的字典类的代码:
Public Class Inventory
Public ItemInventory As New Dictionary(Of String, Item)
Public Function iItem(ByVal key As String) As Item
Return ItemInventory(key)
End Function
Public Sub addItem(ByVal item As String, ByVal Desc As String, ByVal DRate As Double, ByVal WRate As Double, _
ByVal MRate As Double, ByVal Quantity As Integer)
With ItemInventory
.Add(item, New Item(item, Desc, DRate, WRate, MRate, Quantity))
End With
End Sub
Public Sub removeItem(ByVal item As String)
With ItemInventory
.Remove(item)
End With
End Sub
Public Function returnKeys() As String()
Dim Keys() As String
With ItemInventory
Keys = .Keys.ToList.ToArray
End With
Return Keys
End Function
End Class
我相信应该是这样的。我可以根据需要添加其他类,但除此之外,Dictionary 有键和“Item”,它是对 Item 类的引用,其中包含需要显示的附加数据(在我的“添加”代码应该回答“项目”类中的所有内容)。我应该如何准确地引用字典来显示条目?