我正在创建一个应用程序,在该应用程序中,字典中存储了不同的库,书籍和非书籍媒体,并在列表框中显示了它们。用户可以为这三个元素中的任何一个添加和删除其他字典。我的问题在于提出新的表单,以在图书馆与其媒体之间建立链接。
我有一个“当前库中的书籍”和“当前库中的非书籍媒体”的列表框,它们将显示链接到该列表框中突出显示的特定库的媒体。并且用户可以自由地在库中添加和删除其他媒体。
frmManager:https://prnt.sc/mnd8qf
frmAssociationScreen:https://prnt.sc/mnd8sh
我正在尝试在字典中创建一个字典,以便可以操纵数据以将不同的媒体添加到各个库中。但是我不确定从哪里开始,我想从硬编码到Zahnow Library的一些链接开始,添加一些书籍以及一种非书籍媒体。
Public Class frmManager
' Global data structures
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 Dictionary(Of String, LibraryWithMedia)
Structure LibraryWithMedia
Dim strLibraryName As String
Dim dicBooks As Dictionary(Of String, String)
Dim nonBookMedia As Dictionary(Of String, String)
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
Sub populatelstLibrary()
lstLibraries.Items.Clear()
For Each library In Libraries
lstLibraries.Items.Add(library.Value & " --- " & library.Key)
Next
End Sub
我如何处理数据以删除库字典
Private Sub btnDeleteLibrary_Click(sender As Object, e As EventArgs) Handles btnDeleteLibrary.Click
Dim key As String = ""
Dim tmpLibraries As New Dictionary(Of String, String)
' If an index is selected in listbox then continue
' If nothing selected, the button does nothing
If lstLibraries.SelectedIndex > -1 Then
If MsgBox("Are you sure you want to delete this library?", MsgBoxStyle.YesNoCancel, "Delete confirmation") = MsgBoxResult.Yes Then
For Each library In Libraries
If lstLibraries.SelectedItem.Equals(library.Value & " --- " & library.Key) Then
' DoNothing
' the selected item is not added to temp library
Else
' Add all other values to temp library
tmpLibraries.Add(library.Key, library.Value)
End If
Next
lstLibraries.Items.Clear() ' Clear the list box
Libraries = tmpLibraries ' Set dictionary Libraries equal to temp libararies
tmpLibraries = Nothing ' Set temp library back to nothing
populatelstLibrary() ' Repopulate the list box
End If
End If
End Sub
frmAssociationScreen.vb
Public Class frmAssociationScreen
Private Sub frmAssociationScreen_Load(sender As Object, e As EventArgs) Handles Me.Load
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
End Sub
Private Sub btnManagerScreen_Click(sender As Object, e As EventArgs) Handles btnManagerScreen.Click
Me.Close() ' Close current form
frmManager.Visible = True ' Make manager form visible
End Sub
Private Sub btnAddBook_Click(sender As Object, e As EventArgs) Handles btnAddBook.Click
End Sub
Private Sub btnRemoveBook_Click(sender As Object, e As EventArgs) Handles btnRemoveBook.Click
End Sub
Private Sub lstAllLibraries_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstAllLibraries.SelectedIndexChanged
End Sub
End Class
答案 0 :(得分:1)
您的代码中有一些细微的变化,如下所示:
在您的结构 LibraryWithMedia 我们添加了 SUB NEW
' Structure of single library
Structure LibraryWithMedia
'
Dim strLibraryName As String
Dim dicBooks As Dictionary(Of String, String)
Dim nonBookMedia As Dictionary(Of String, String)
'
'new library constructor
Sub New(ByVal LibName As String)
strLibraryName = LibName
dicBooks = New Dictionary(Of String, String)
nonBookMedia = New Dictionary(Of String, String)
End Sub
'
End Structure
在您的配备的库声明中。
声明从(字符串,字符串)更改为简单的LibraryWithMedia
Public EquippedLibrary As List(Of LibraryWithMedia)
Form_Load 事件的结尾/底部
' construct equipped library and define the library names
EquippedLibrary = New List(Of LibraryWithMedia)
' initialise each library with empty books/media dictionaries
populateEquippedLibNames
PopulateEquippedLibNames 子例程(这是一个新的子例程)
Sub populateEquippedLibNames()
'
Dim Counta As Integer
Dim tmpSingleLib As LibraryWithMedia
'
For Counta = 0 To Libraries.Count - 1
tmpSingleLib = New LibraryWithMedia(Libraries.Values(Counta))
EquippedLibrary.Add(tmpSingleLib)
tmpSingleLib = Nothing
Next
'
End Sub
然后将每本书添加/删除到TOP列表框中的SELECTED库中
Private Sub btnAddBook_Click(sender As Object, e As EventArgs) Handles btnAddBook.Click
'
EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Add(Books.Keys(lstBooks.SelectedIndex), Books.Values(lstBooks.SelectedIndex))
lstSelectedBooks.Items.Add(lstBooks.SelectedItem)
'
End Sub
Private Sub btnRemoveBook_Click(sender As Object, e As EventArgs) Handles btnRemoveBook.Click
'
EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Remove(Books.Keys(lstBooks.SelectedIndex))
'
End Sub
请注意,要将图书/媒体添加到图书馆, 必须在TOP列表框中选择一个库 还必须选择要添加的图书或媒体
不执行错误检查,因此您需要添加它(例如列表框是否具有选择项)等
我在下面为您添加了更改代码库
Private Sub lstLibraries_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstLibraries.SelectedIndexChanged
'
Dim Counta As Integer
'
lstSelectedBooks.Items.Clear()
lstSelectedMedia.Items.Clear()
If EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Count > 0 Then
For Counta = 0 To EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Count - 1
lstSelectedBooks.Items.Add(EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Keys(Counta) & " --- " & EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Values(Counta))
Next
End If
Counta = Nothing
'
End Sub