I'm creating an application where I have different libraries, books and non-book media stored in dictionaries and displayed in listboxes. The user can add and remove additional dictionaries for any of these 3 elements. My issue lies in bringing up the new form to create a link between a library and it's media.
I have a listbox for "Books at Current library" and "Non-Book Media at Current Library" Which will display the media that is linked to the specific library that is highlighted in the listbox. And the user can freely add and remove different media to the library.
frmManager: https://prnt.sc/mnd8qf
frmAssociationScreen: https://prnt.sc/mnd8sh
I'm trying to create a dictionary within a dictionary that I can manipulate the data with for adding the different media to the individual libraries. But I'm unsure where to go from here, I'd like to start by hard coding a few links to Zahnow Library and add a few books as well as one non-book media.
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
How I manipulated the data to delete library dictionary
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
Some slight changes in your code as below:
In your structure LibraryWithMedia
We added 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
In your EquippedLibrary declaration.
The declaration changed from (string, string) to simply LibraryWithMedia
Public EquippedLibrary As List(Of LibraryWithMedia)
At the end/bottom of your Form_Load event
' construct equipped library and define the library names
EquippedLibrary = New List(Of LibraryWithMedia)
' initialise each library with empty books/media dictionaries
populateEquippedLibNames
The PopulateEquippedLibNames Subroutines (this is a new subroutine)
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
And then for adding/removing each book to the SELECTED library in the TOP listbox
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
Note that to add a book/media to a library,
A library MUST BE selected in the TOP listbox
The Book or Media being added must also be selected
No error checking is performed, so you will need to add it (such as a listbox has a selection or not) etc
Update
I have added the libraries on change code for you below
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
Related
I need help, so I can compare the text from the text booths that are in these 3 groupboxes.
This is what my form looks like:
Private Sub frmCompareAdress_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim firstGroupBoxFields() As String = {TextBoxVorname1.Text, TextBoxName1.Text, TextBoxStrasse1.Text, TextBoxPLZ1.Text, TextBoxOrt1.Text, TextBoxTelefon1.Text}
Dim secondGroupBoxFields() As String = {TextBoxVorname2.Text, TextBoxName2.Text, TextBoxStrasse2.Text, TextBoxPLZ2.Text, TextBoxOrt2.Text, TextBoxTelefon2.Text}
Dim thirdGroupBoxFields() As String = {TextBoxVorname3.Text, TextBoxName3.Text, TextBoxStrasse3.Text,TextBoxNr3.Text, TextBoxPLZ3.Text, TextBoxOrt3.Text, TextBoxTelefon3.Text}
ComparisonFieldsfirstGroupBoxFields, secondGroupBoxFields, thirdGroupBoxFields)
End Sub
Public Sub ComparisonFields(ByVal firstGBFields() As String, secondGBFields() As String, thirdGBFields() As String)
Dim notCompareFileds = String.Join(", ", firstGBFields.Except(secondGBFields))
'what to do next? I'm a little confused if I'm on the right track
End Sub
Now if, for example, the textBox1Vorname.Text is different from the textBox2Vorname.Text or textBox3Vorname.Text, I want to mark them in red.
I imagined it in a way, to compare as an array, and I put it in 3 array values from textboxes.
Can anyone help me further with this function?
Thank you for your help.
Public Shared Function HaveSameText(ParamArray controls() As Control) As Boolean
Return controls.All(Function(c) c.Text = controls(0).Text)
End Function
Public Shared Sub ColorAllRed(ParamArray controls() As Control)
For Each c In controls
c.ForeColor = Color.Red
Next
End Sub
Public Shared Sub ColorAllRedIfNotSameText(ParamArray controls() As Control)
If Not HaveSameText(controls) Then ColorAllRed(controls)
End Sub
Private Sub frmCompareAdress_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ColorAllRedIfNotSameText(TextBoxVorname1, TextBoxVorname2, TextBoxVorname3)
ColorAllRedIfNotSameText(TextBoxName1, TextBoxName2, TextBoxName3)
ColorAllRedIfNotSameText(TextBoxStrasse1, TextBoxStrasse2, TextBoxStrasse3)
ColorAllRedIfNotSameText(TextBoxPLZ1, TextBoxPLZ2, TextBoxPLZ3)
ColorAllRedIfNotSameText(TextBoxOrt1, TextBoxOrt2, TextBoxOrt3)
ColorAllRedIfNotSameText(TextBoxTelefon1, TextBoxTelefon2, TextBoxTelefon3)
End Sub
I am kinda new to VB.net, so I am not sure if I try this the right way. I have the following piece of code.
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim TextLine As String
Do While objReader.Peek() <> -1
Dim newString As String = TextLine.Replace(vbCr, "").Replace(vbLf, "") & ".wav"
Dim SongName As String = My.Computer.FileSystem.GetName(newString)
Dim MyFile As String = Dir("C:\AllSongs\" & newString)
Dim Searchquery As IEnumerable(Of String) = IO.Directory.EnumerateFiles("C:\AllSongs", "*", IO.SearchOption.AllDirectories).Where(Function(f) IO.Path.GetFileNameWithoutExtension(f).IndexOf(SongName, StringComparison.CurrentCultureIgnoreCase) >= 0)
For Each Result In Searchquery
ListBox1.Items.Add(Result)
Next
I am trying to use the lines in the text file, and get the .wav in AllSongs dir that partially correspond in these files. Can it be done?
Edit: Part of the code contains a media player. I want to be able to play songs from this player, by choosing files in the list.
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
Dim variables As New Dictionary(Of String, String)()
Dim selectedItem As Object = ListBox1.SelectedItem
variables("MyDynamicVariable") = selectedItem ' Set the value of the "variable"
selectedItem1 = selectedItem
Dim value As String = variables("MyDynamicVariable") ' Retrieve the value of the variable
End Sub
Incidental to the question, but important, is that when you're working with files it's often necessary to clean up some resources (file handles, I guess) that the operating system uses even though you don't see it directly in the code as written. There is a way of doing that automatically with the Using statement, as shown in the following code.
To find out if a filename contains some text (string), you can extract the filename with no path or extension with Path.GetFileNameWithoutExtension and check if it contains the desired string with the IndexOf function, which lets you ignore uppercase/lowercase by specifying how to do the check.
I notice from an update to the question that some improvements can be made, such as using a Class for the song entries so that the displayed list can have more information behind it, which means that the song name can be shown on its own but you can get the full path to the file when you click on it.
I made a new Windows Forms project and added just a ListBox to it, and used this code to show the full path (which you can use for your media player) to the song when its name is double-clicked:
Imports System.IO
Public Class Form1
Public Class SongEntry
Property Name As String
Property FullName As String
End Class
Sub PopulateSongList(musicDirectory As String, songsList As String)
Dim songList As New List(Of SongEntry)
Using sr As New System.IO.StreamReader(songsList)
Do While Not sr.EndOfStream
Dim thisSong = sr.ReadLine()
If thisSong <> "NaN" Then
Dim fs = Directory.EnumerateFiles(musicDirectory, "*.wav", SearchOption.AllDirectories).
Where(Function(f) Path.GetFileNameWithoutExtension(f).IndexOf(thisSong, StringComparison.CurrentCultureIgnoreCase) >= 0).
Select(Function(g) New SongEntry With {.Name = Path.GetFileNameWithoutExtension(g), .FullName = g})
songList.AddRange(fs)
End If
Loop
End Using
ListBox1.DataSource = songList
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "FullName"
End Sub
Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
Dim lb = DirectCast(sender, ListBox)
If lb.SelectedIndex >= 0 Then
Dim fullPathToSong = lb.SelectedValue.ToString()
MsgBox(fullPathToSong)
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim songsList = "C:\temp\AllSongs\songsList.txt"
Dim musicDirectory = "C:\temp\AllSongs"
PopulateSongList(musicDirectory, songsList)
End Sub
End Class
I'm creating an application where I have different libraries, books and non-book media stored in dictionaries and displayed in listboxes. The user can add and remove additional dictionaries for any of these elements.
I have a listbox for "Books at Current library" and "Non-Book Media at Current Library" Which will display the media that is linked to the specific library that is highlighted in the listbox. And the user can freely add and remove different media to the library.
I'm having issues adding predefined associations together on frmAssociationScreen. I want to hardcode a few associations to LibraryWithMedia Where "Zahnow Library" will have Keys: 101 and 104 which are displayed in the "Books at Current Library" listbox before adding any from lstAllBooks.
Screenshots of the two forms:
frmManager: https://prnt.sc/mnd8qf
frmAssociationScreen: https://prnt.sc/mnd8sh
The three ways I've tried to implement but failed on 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")
Sub 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
Try this
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
Or to select the items from the listbox, use
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
I'm having an issue where I'm trying to delete an element from my dictionary at the selected index in a listbox. I added 3 elements to a SortedDictionary and printing them into a list box. I'm trying to delete the item from the dictionary that is highlighted in the listbox, however when I click remove I get the System.ArgumentNullException: 'Value cannot be null. Parameter name: key' within my Sub btnDeleteLibrary_Click Why is this?
Error occurs at the line Libraries.Remove(lstLibraries.SelectedValue)
Public Class frmManager
Dim Libraries As New SortedDictionary(Of String, String)
Private Sub frmManager_Load(sender As Object, e As EventArgs) Handles Me.Load
Libraries.Add("Zahnow Library", "SVSU")
Libraries.Add("Fleschner Memorial Library", "BR")
Libraries.Add("Scott D. James Technical Repository", "SDJ")
lstLibraries.Items.Clear()
populatelstLibrary()
End Sub
Sub populatelstLibrary()
For Each library In Libraries
lstLibraries.Items.Add(vbCrLf & library.Key & " --- " & library.Value)
Next
End Sub
Private Sub btnDeleteLibrary_Click(sender As Object, e As EventArgs) Handles btnDeleteLibrary.Click
Libraries.Remove(lstLibraries.SelectedValue)
lstLibraries.Items.Clear()
populatelstLibrary()
End Sub
End Class
Since you are constructing a custom display string for your ListBox items it makes it harder to map that directly to the dictionary.
The easiest solution would be to create a custom class that you store in the ListBox and set the ListBox's DisplayMember and ValueMember properties, telling it how to display each item as well as which property it should get from an item when you call SelectedValue.
Class:
Public Class LibraryItem
Public Property Name As String
Public Property Code As String
Public ReadOnly Property DisplayName As String
Get
Return vbCrLf & Me.Name & " --- " & Me.Code
End Get
End Property
Public Sub New()
End Sub
Public Sub New(ByVal Name As String, ByVal Code As String)
Me.Name = Name
Me.Code = Code
End Sub
End Class
Initial setup:
Private Sub frmManager_Load(sender As Object, e As EventArgs) Handles Me.Load
'Tell the ListBox which properties to use for display and value.
lstLibraries.DisplayMember = "DisplayName"
lstLibraries.ValueMember = "Name"
'Your code...
End Sub
Filling the ListBox:
Sub populatelstLibrary()
For Each library In Libraries
lstLibraries.Items.Add(New LibraryItem(library.Key, library.Value))
Next
End Sub
Now SelectedValue will get you the value of the selected item's Name property, which corresponds to the key in the dictionary.
I would change some things in your code. First it seems that you have your sorted dictionary built with the wrong values for Key and Value, change it to
Libraries.Add("SVSU","Zahnow Library")
Libraries.Add("BR", "Fleschner Memorial Library")
Libraries.Add("SDJ", "Scott D. James Technical Repository")
' and call immediately
populatelstLibrary()
Now in populatelstLibrary change the code to
Sub populatelstLibrary()
lstLibraries.DataSource = Nothing
lstLibraries.DisplayMember = "Value"
lstLibraries.ValueMember = "Key"
lstLibraries.DataSource = Libraries.ToList()
End Sub
finally in the button click just check for null and remove the SelectedValue
Private Sub btnDeleteLibrary_Click(sender As Object, e As EventArgs) Handles btnDeleteLibrary.Click
If lstLibraries.SelectedValue IsNot Nothing Then
Libraries.Remove(lstLibraries.SelectedValue)
populatelstLibrary()
End If
End Sub
So my problem is:
I have a List of a custom Type {Id as Integer, Tag() as String},
and i want to perform a multiple-criteria search on it; eg:
SearchTags={"Document","HelloWorld"}
Results of the Search will be placed a ListBox (ListBox1) in this format:
resultItem.id & " - " & resultItem.tags
I already tried everything i could find on forums, but it didn't work for me (It was for db's or for string datatypes)
Now, i really need your help. Thanks in advance.
For Each MEntry As EntryType In MainList
For Each Entry In MEntry.getTags
For Each item As String In Split(TextBox1.Text, " ")
If Entry.Contains(item) Then
If TestIfItemExistsInListBox2(item) = False Then
ListBox1.Items.Add(item & " - " & Entry.getId)
End If
End If
Next
Next
Next
Example Custom Array:
(24,{"snippet","vb"})
(32,{"console","cpp","helloworld"})
and so on...
I searched for ("Snippet vb test"):
snippet vb helloWorld - 2
snippet vb tcpchatEx - 16
cs something
test
So, i'll get everything that contains one of my search phrases.
I expected following:
snippet vb tcp test
snippet vb dll test
snippet vb test metroui
So, i want to get everything that contains all my search phrases.
My entire, code-likely class
Imports Newtonsoft.Json
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Dim MainList As New List(Of EntryType)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MainList.Clear()
Dim thr As New Threading.Thread(AddressOf thr1)
thr.SetApartmentState(Threading.ApartmentState.MTA)
thr.Start()
End Sub
Delegate Sub SetTextCallback([text] As String)
Private Sub SetTitle(ByVal [text] As String) ' source <> mine
If Me.TextBox1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetTitle)
Me.Invoke(d, New Object() {[text]})
Else
Me.Text = [text]
End If
End Sub
Sub thr1()
Dim linez As Integer = 1
Dim linex As Integer = 1
For Each line As String In System.IO.File.ReadAllLines("index.db")
linez += 1
Next
For Each line As String In System.IO.File.ReadAllLines("index.db")
Try
Application.DoEvents()
Dim a As saLoginResponse = JsonConvert.DeserializeObject(Of saLoginResponse)(line) ' source <> mine
Application.DoEvents()
MainList.Add(New EntryType(a.id, Split(a.tags, " ")))
linex += 1
SetTitle("Search (loading, " & linex & " of " & linez & ")")
Catch ex As Exception
End Try
Next
SetTitle("Search")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim searchTags() As String = TextBox1.Text.Split(" ")
Dim query = MainList.Where(Function(et) et.Tags.Any(Function(tag) searchTags.Contains(tag))).ToList
For Each et In query
ListBox1.Items.Add(et.Id)
Next
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) ' test
MsgBox(Mid(ListBox1.SelectedItem.ToString, 1, 6)) ' test
End Sub 'test, removeonrelease
End Class
Public Class EntryType
Public Property Id As Integer
Public Property Tags() As String
Public Sub New(ByVal _id As Integer, ByVal _tags() As String)
Me.Id = Id
Me.Tags = Tags
End Sub
Public Function GetTags() As String
'to tell the Listbox what to display
Return Tags
End Function
Public Function GetId() As Integer
'to tell the Listbox what to display
Return Id
End Function
End Class
I also edited your EntryType class; I added a constructor, removed toString and added GetTags and GetID.
Example "DB" im working with ("db" as "index.db" in exec dir):
{"tags":"vb.net lol test qwikscopeZ","id":123456}
{"tags":"vb.net lol test","id":12345}
{"tags":"vb.net lol","id":1234}
{"tags":"vb.net","id":123}
{"tags":"cpp","id":1}
{"tags":"cpp graphical","id":2}
{"tags":"cpp graphical fractals","id":3}
{"tags":"cpp graphical fractals m4th","id":500123}
Error:
Debugger:Exception Intercepted: _Lambda$__1, Form2.vb line 44
An exception was intercepted and the call stack unwound to the point before the call from user code where the exception occurred. "Unwind the call stack on unhandled exceptions" is selected in the debugger options.
Time: 13.11.2014 03:46:10
Thread:<No Name>[5856]
Here is a Lambda query. The Where filters on a predicate, since Tags is an Array you can use the Any function to perform a search based on another Array-SearchTags. You can store each class object in the Listbox since it stores Objects, you just need to tell it what to display(see below).
Public Class EntryType
Public Property Id As Integer
Public Property Tags() As As String
Public Overrides Function ToString() As String
'to tell the Listbox what to display
Return String.Format("{0} - {1}", Me.Id, String.Join(Me.Tags, " "))
End Function
End Class
Dim searchTags = textbox1.Text.Split(" "c)
Dim query = mainlist.Where(Function(et) et.Tags.Any(Function(tag) searchTags.Contains(tag))).ToList
For Each et In query
Listbox1.Items.Add(et)
Next