0

好的,所以我的大学工作的一部分得到了帮助,但是现在还有一步要做:显示数据。现在,有问题的分配让用户能够通过 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 类的引用,其中包含需要显示的附加数据(在我的“添加”代码应该回答“项目”类中的所有内容)。我应该如何准确地引用字典来显示条目?

4

1 回答 1

1

似乎那里有几个问题......要添加到组合中,有几种方法。一种方法是向返回描述的 Inven 类添加一个 .ToString 方法。然后,当您将库存项目添加到 cbo 时,您正在添加 OBJECTS 但 cbo 将使用 ToString 方法来显示有价值的文本。

for each kvp as KeyValuePair In ItemInventory 
   theCbo.Items.Add(kvp.Value)        ' adding Inven OBJECTS to it
next kvp

另一种简单的方法是使字典成为 cbo 的数据源:

theCbo.DataSource = New BindingSource(ItemInventory, Nothing)
theCbo.ValueMember = "Key"
theCbo.DisplayMember = "Value"    ' still relies on ToString

由于它们是 cbo.Items 中的 OBJECTS,因此您可以轻松找到他们选择的对象:

thisID = cbo.SelectedItem.ID

这可以传递给DisplayItemInventory 中的一个新方法,该方法会将数据发布到表单控件:

dInven.Display(thisID)

Friend Sub Display(theID as String)   '
    If ItemInventory.ContainsKey(theID) Then
       With theForm
           .tbDescr.Text = ItemInventory(theId).Description
            ...
       End With
    End If
End Sub
于 2013-10-29T15:46:10.527 回答