显示字典数据

时间:2013-10-29 15:30:17

标签: vb.net dictionary

好的,所以我得到了大学工作的一部分帮助,但现在还有一个步骤要做:显示数据。现在,有问题的分配使用户能够按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类的引用,它包含需要显示的附加数据(有一系列变量)我的“添加”代码应该回答“Item”类中的所有内容。我究竟应该如何引用字典来显示条目?

1 个答案:

答案 0 :(得分:1)

似乎有几个问题......对于添加到组合,有几种方法。一种方法是将一个.ToString方法添加到返回Description的Inven类中。然后,当您将库存ITEMS添加到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

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

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