How to check if 2 listbox have the same items collection? - vb.net

I load items from a database on lstData then I make a copy of this state on another listbox called lstOriginal using this code
lstOriginal.Items.Clear()
For Each Item In lstData.Items
lstOriginal.Items.Add(Item)
Next
Then the user is available to make changes, add or delete items on lstData and when the user press UPDATE I need to know if the items collection of lstData and lstOriginal are the same.
I donĀ“t care about what changed, if there's a difference I will update the complete items collection on the data base.
So I need to know if the items collection of lstData and lstOriginal are the same.
The only way I know is make a loop like
Dim lstOriginalString as String
lstOriginalString = ""
For Each Item In lstOriginal.Items
lstOriginalString = lstOriginalString & Item
Next
Then another loop for lstData and a simple string compare, but I think there's gonna be a simpler way

You could use LINQ:
Dim isSame As Boolean = lstData.Items.Count = lstOriginal.Items.Count
If isSame Then
Dim newItems = lstData.Items.Cast(Of String)()
Dim originalItems = lstOriginal.Items.Cast(Of String)()
isSame = Not newItems.Except(originalItems).Any()
End If
If you wanted to know if it's even in the same order:
isSame = newItems.SequenceEqual(originalItems)

Related

How to get selected items from listbox for SQL where statement

How do you get the selected items from the listbox so that I can use it on my SQL where statement... selected items wont work because it shows datarowview.. I've tried using
Dim Studente As String = ListBox1.GetItemText(ListBox1.SelectedItems())
But it didn't do anything
To be specific:
Dim students As String() = ListBox1.SelectedItems.
Cast(Of Object)().
Select(Functions(item) ListBox1.GetItemText(item)).
ToArray()
Without LINQ:
Dim students As New List(Of String)
For Each item In ListBox1.SelectedItems
students.Add(ListBox1.GetItemText(item))
Next
You can then call ToArray on that List if you specifically need an array.

Form control change to runtime object VB.net

I have a form what contains about 10-15 CheckedListBox objects on a Panel named PanelFilterBoxes.
Each CheckedListBox can contains lots of items (1 to max ~2000).
I have to reload regulary the items of this controls based upon a datatable's column unique values (plus some extras).
Now I have a code like this (this below is just a shorten extract):
For c = 0 To PanelFilterBoxes.Controls.Count - 1
'--get unique datas from datatable
Dim dtUnique() As String = ....
'--change items of the control
Dim contr As CheckedListBox = PanelFilterBoxes.Controls.Item(c)
contr.Items.Clear()
contr.Items.Add("*All", True)
contr.Items.Add("*Search: ", False)
For Each myValue As String In dtUnique
'--add items with checked true
contr.Items.Add(myValue, True)
Next
Next
It is not so long, but takes some times. I'd like to try what if I create a runtime object (I can put data in a runtime created object faster than a form object), and "replace" the control on the form with this?
So my "big challenge" how can I do this?
I tried this ways, but not works :/
For c = 0 To PanelFilterBoxes.Controls.Count - 1
'--get unique datas from datatable
Dim dtUnique() As String = ....
'--change items of the control
Dim contr As CheckedListBox = PanelFilterBoxes.Controls.Item(c)
'--runtime temporary object
Dim tempContr As new CheckedListBox
contr.Items.Clear()
tempContr .Items.Add("*All", True)
tempContr .Items.Add("*Search: ", False)
For Each myValue As String In dtUnique
'--add items with checked true
tempContr .Items.Add(myValue, True)
Next
'--1 - this doesn't works, but doesn't produce any error
contr=tempContr
'--2 - it works, but the items aren't checked
contr.Items.AddRange(tempContr.Items)
Next
Anybody has a solution for this?
Thank you

How to transfer items from a ListBox to a collection VB.NET

In one of my forms I have got a ListBox (formListBox) which contains a list of string items. What I would like to do is transfer all the items in the list box into a collection. So far I've tried the following without success:
Dim newItems As New ListBox.ObjectCollection(formListBox)
For Each item As String In newItems
myArrayList.addNewItem(item)
Next
After this is executed the number of items in the arraylist comes back as 0. I have a feeling I am misunderstanding the "ListBox.ObjectCollection(formListBox)" part - the impression I got from this is that it returns a collection from the ListBox but the results I get suggest otherwise.
Try this
Dim arr1()
ReDim arr1(ListBox1.Items.Count - 1)
ListBox1.Items.CopyTo(arr1, 0)
OR
Dim ArrayItems() As String
'//ADD ITEMS INTO ARRAY
'//FIRST FIND HOW MANY ITEMS IN LISTBOX AND REDIM THE ARRAT FOR SIZE
ReDim ArrayItems(List1.ListCount)
'//NOW ADD ITEMS
For i = 1 To List1.ListCount
List1.ListIndex = i - 1
ArrayItems(i) = List1.Text
Next i
OR
Using LINQ
(From item As Object In yourListBox.ObjectCollection Select item.ToString()).ToArray()

How do I use LINQ to get all the Items that have a particular SubItem?

My software is designed to encrypt and decrypt files. The user loads the files to be processed into a ListView control. In the control, each item is the file path with one subitem, the type of process (ENCRYPT or DECRYPT).
I need to get a list of all ITEMS (the file paths) that have the "ENCRYPT" subitem, preferably with LINQ. Currently, my code looks like this:
Dim enclist As New ArrayList()
For i As Int32 = 0 To (lvwLoad.Items.Count - 1)
If lvwLoad.Items(i).SubItems(1).Text = "ENCRYPT" Then
enclist.Add(lvwLoad.Items.Item(i).Text)
count += 1
End If
Next
I tried this:
Dim list As IEnumerable(Of String) = From item In lvwLoad.Items
Where item.SubItems(1).Text = "ENCRYPT"
But this statement can't access the SubItems() array. I know there's probably something simple I'm missing, but I can't figure it out.
EDIT:
I know I can do this:
Dim enclist As New List(Of String)
For Each item As ListViewItem In lvwLoad.Items
If item.SubItems(1).Text = "ENCRYPT" Then
enclist.Add(item.Text)
End If
Next
But I really want to know how to do this with LINQ.
Try code below, you need to cast items to ListViewItem so that you can access SubItems
Dim list = From item In lvwLoad.Items.Cast(Of ListViewItem) () _
Where item.SubItems(1).Text = "ENCRYPT"
Select item.Text
return lvwLoad.Items.Where(item => item.SubItems(1).Text = "ENCRYPT").Select(s => s.Text);
Dim list As IEnumerable(Of String) = From item In lvwLoad.Items
Where item.SubItems(1).Text = "ENCRYPT"
Select item.Text
I hope this will help.

Generic Lists copying references rather than creating a copiedList

I was developing a small function when trying to run an enumerator across a list and then carry out some action. (Below is an idea of what I was trying to do.
When trying to remove I got a "Collection cannot be modified" which after I had actually woken up I realised that tempList must have just been assigned myLists reference rather than a copy of myLists. After that I tried to find a way to say
tempList = myList.copy
However nothing seems to exist?? I ended up writing a small for loop that then just added each item from myLsit into tempList but I would have thought there would have been another mechanism (like clone??)
So my question(s):
is my assumption about tempList receiving a reference to myList correct
How should a list be copied to another list?
private myList as List (Of something)
sub new()
myList.add(new Something)
end sub
sub myCalledFunction()
dim tempList as new List (Of Something)
tempList = myList
Using i as IEnumerator = myList.getEnumarator
while i.moveNext
'if some critria is met then
tempList.remove(i.current)
end
end using
end sub
By writing tempList = myList you don't make a copy oh the collection, you only make tempList reference myList. Try this instead : dim tempList as new List (Of Something)(myList)
I think if you called myCalledFunction(byVal aListCopy as Something) you can let the framework do the work.
If your list consists of value types you can just create a new list with the old list passed in the constructor. If you are going to be doing a deep copy of a reference object your best bet is to have your reference type implement ICloneable (example). You can then loop through and clone each object or you could add an extension method (like this c# example).
Try this - use LINQ to create a new list from the original, for example:
Sub Main()
Dim nums As New List(Of Integer)
nums.Add(1)
nums.Add(2)
nums.Add(3)
nums.Add(4)
Dim k = (From i In nums _
Select i).ToList()
For Each number As Integer In nums
k.Remove(number)
Next
End Sub
k will then be a new list of numbers which are not linked to the source.