VB.Net向LibraryWithMedia添加预定义词典

时间:2019-02-20 05:02:54

标签: vb.net dictionary

我正在创建一个应用程序,在该应用程序中,字典中存储了不同的库,书籍和非书籍媒体,并在列表框中显示了它们。用户可以为这些元素中的任何一个添加和删除其他字典。

我有一个“当前库中的书籍”和“当前库中的非书籍媒体”的列表框,它们将显示链接到该列表框中突出显示的特定库的媒体。并且用户可以自由地在库中添加和删除其他媒体。

我在将frmAssociationScreen上的预定义关联添加在一起时遇到问题。我想将一些关联硬编码为LibraryWithMedia,其中“ Zahnow Library”将具有键:101和104,这些键将显示在“当前库中的书”列表框中,然后再添加lstAllBooks中的任何键。

两种形式的屏幕截图:

frmManager:https://prnt.sc/mnd8qf

frmAssociationScreen:https://prnt.sc/mnd8sh

我尝试实现但在frm_Load上失败的三种方式

    frmManager.LibraryWithMedia("Zahnow Library").dicBooks.Add("101", "Zen and the Art of Appliance Wiring")
    frmManager.EquippedLibrary(lstAllLibraries.SelectedIndex).dicBooks.Add("104", "Data Structures for Fun and Profit")

    tmp = New frmManager.LibraryWithMedia(frmManager.Libraries.Keys(0))
    tmp.dicBooks.Add("101", "Zen and the Art of Appliance Wiring")

frmAssociationScreen_Load

Private Sub frmAssociationScreen_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim tmp As frmManager.LibraryWithMedia

    lstAllLibraries.Items.Clear()

    For Each library In frmManager.Libraries
        lstAllLibraries.Items.Add(library.Value & " --- " & library.Key)
    Next

    For Each book In frmManager.Books
        lstAllBooks.Items.Add(book.Value & " --- " & book.Key)
    Next

    For Each nonBook In frmManager.nonBookMedia
        lstAllMedia.Items.Add(nonBook.Value & " --- " & nonBook.Key)
    Next

    ' The code i'm struggling to implement
    ' Three different ways I've tried to implement it
    ' construct equipped library and define the library names
    frmManager.EquippedLibrary = New List(Of frmManager.LibraryWithMedia)
    frmManager.LibraryWithMedia("Zahnow Library").dicBooks.Add("101", "Zen and the Art of Appliance Wiring")
    frmManager.EquippedLibrary(lstAllLibraries.SelectedIndex).dicBooks.Add("104", "Data Structures for Fun and Profit")

    tmp = New frmManager.LibraryWithMedia(frmManager.Libraries.Keys(0))
    tmp.dicBooks.Add("101", "Zen and the Art of Appliance Wiring")

    ' initialise each library with book/media dictionary
    populateEquippedLibNames()

End Sub

frmManager:

Public Class frmManager

    Public Libraries As New Dictionary(Of String, String)
    Public Books As New Dictionary(Of String, String)
    Public nonBookMedia As New Dictionary(Of String, String)

    Public EquippedLibrary As New List(Of LibraryWithMedia)

    Structure LibraryWithMedia

        Dim strLibraryName As String
        Dim dicBooks As Dictionary(Of String, String)
        Dim nonBookMedia As Dictionary(Of String, String)

        Sub New(ByVal LibName As String)

            strLibraryName = LibName
            dicBooks = New Dictionary(Of String, String)
            nonBookMedia = New Dictionary(Of String, String)

        End Sub

    End Structure

    Private Sub frmManager_Load(sender As Object, e As EventArgs) Handles Me.Load

        Libraries.Add("SVSU", "Zahnow Library")
        Libraries.Add("BR", "Fleschner Memorial Library")
        Libraries.Add("SDJ", "Scott D. James Technical Repository")

        Books.Add("104", "Data Structures for Fun and Profit")
        Books.Add("103", "Doing More With Less - Naval Lint Art")
        Books.Add("102", "Interpretive Klingon Poetry")
        Books.Add("105", "Programming with the Bidgoli")
        Books.Add("101", "Zen and the Art of Appliance Wiring")

        nonBookMedia.Add("201", "CD - IEEE Computer: the Hits")
        nonBookMedia.Add("203", "DVD - Databases and You: the Video Experience")
        nonBookMedia.Add("202", "DVD - The Pirates of Silicon Valley")

        populatelstLibrary()
        populatelstBooks()
        populatelstBookMedia()

    End Sub

frmAssociationScreen:

Public Class frmAssociationScreen

    Sub populateEquippedLibNames()

        Dim counter As Integer
        Dim tmpSingleLib As frmManager.LibraryWithMedia

        For counter = 0 To frmManager.Libraries.Count - 1
            tmpSingleLib = New frmManager.LibraryWithMedia(frmManager.Libraries.Values(counter))
            frmManager.EquippedLibrary.Add(tmpSingleLib)
            tmpSingleLib = Nothing
        Next

    End Sub

populateLstLibrary()

Sub populatelstLibrary()

    lstLibraries.Items.Clear()

    For Each library In Libraries
        lstLibraries.Items.Add(library.Value & " --- " & library.Key)
    Next

End Sub

populatelstBooks()

Sub populatelstBooks()

    lstBooks.Items.Clear()

    For Each book In Books
        lstBooks.Items.Add(book.Value & " --- " & book.Key)
    Next

End Sub

populatelstBookMedia()

Sub populatelstBookMedia()

    lstBookMedia.Items.Clear()

    For Each bookMedia In nonBookMedia
        lstBookMedia.Items.Add(bookMedia.Value & " --- " & bookMedia.Key)
    Next

End Sub

1 个答案:

答案 0 :(得分:1)

尝试一下

For Each library As frmManager.LibraryWithMedia In frmManager.EquippedLibrary
    If library.strLibraryName = "Zahnow Library" Then
        library.dicBooks.Add("101", "Zen and the Art of Appliance Wiring")
    End If
Next

或者要从列表框中选择项目,请使用

For Each library As frmManager.LibraryWithMedia In frmManager.EquippedLibrary
    If library.strLibraryName =  lstAllLibraries.Text Then
        library.dicBooks.Add("101", "Zen and the Art of Appliance Wiring")
    End If
Next