How to get selected items from listbox for SQL where statement - vb.net

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.

Related

Value of type List(Of Long) cannot be converted to List(Of ListItem)

I am filling data from removeActiveService (list of listItem) into a new list listRemove. However listRemove is turning into a list of Long.
Dim listRemove = removeActiveService.Select(Function(item) item.Text.Replace(serviceRemove, "") And item.Text.Split("-"c)(0).Trim And item.Value.Split("-"c)(1).Trim).ToList()
If I change it to Dim listRemove As List(Of ListItem), it results in the error
Value of type List(Of Long) cannot be converted to List(Of ListItem)
I need to perform replace and split on the text and value (see my code). What is the correct syntax here so that it can be a List(Of ListItem)?
EDIT
Starting with checkboxlist items in removeActiveService
Copy that into a brand new list of items called listRemove.
I need to perform these on the TEXT of the list items in listRemove
item.Text.Replace(serviceRemove, "")
item.Text.Split("-"c)(0).Trim
And I need to perform this on the VALUE of the list items in listRemove
item.Value.Split("-"c)(1).Trim
If you expect to output a List(Of ListItem) then you need to create ListItem objects somewhere, which you're not doing now. You're performing operations on the input data but you're not doing anything useful with the results. And doesn't do anything that could be considered useful there. If you expect to output a list of ListItem objects containing the results of those operations then you actually have to create ListItem objects containing the result of those operations. As far as I can tell, this would be the way to go:
Dim listRemove = removeActiveService.Select(Function(item) New ListItem With
{
.Text = item.Text.Replace(serviceRemove, "").Split("-"c)(0).Trim(),
.Value = item.Value.Split("-"c)(1).Trim()
}).
ToList()

how to check that a list contains another list item in VB.Net

I have two lists like this.
Dim List1 As IList(Of String) = New List(Of String)
Dim ListMatch As IList(Of String) = New List(Of String)
I need to figure out if List1 contains all items of ListMatch. How can i do this is VB.Net?
You can use Not SmallCollection.Except(LargeCollection).Any:
Dim containsAll = Not ListMatch.Except(List1).Any()
Documentation of Enumerable.Except:
This method returns those elements in first that do not appear in
second. It does not also return those elements in second that do not
appear in first.
Since Except is a set method, it doesn't take duplicates into account. So if you also want to know if List1 contains the same count of items of ListMatch you could use(less efficient):
Dim list1Lookup = List1.ToLookup(Function(str) str)
Dim listMatchLookup = ListMatch.ToLookup(Function(str) str)
Dim containsSameCount = listMatchLookup.
All(Function(x) list1Lookup(x.Key).Count() = x.Count())

How to check if 2 listbox have the same items collection?

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)

passing all the items of listbox in the richtextbox

how can I pass the items of listbox to richtextbox?. I have multiple items and I want it to display in the richtextbox. this items is like a receipt. it display the items along with its amount.
This is just a way to do it, using LINQ:
Dim Items As String() =
From s As String In ListBox1.Items
RichTextBox1.Text = String.Join(Environment.NewLine, Items)
One of the problems doing this with a listbox, is that listboxes store the items as objects and uses the ToString method of each object to display a representation of that object.
Something like this should work:
RichTextBox1.Lines = (From o In ListBox1.Items
Let ostr = o.ToString
Select ostr).ToArray
This way if you use objects that can't be implicitly cast as string it will still work. Or, if you use custom objects all that's required is a ToString override in the class.

VB: List(Of List(Of String)) keeps changing the content of the outer list when I change the inner one?

I'm writing a program in VB, and I need to make a list of lists (I've already figured out how to do that one). The problem is, the outer list is going to need a different number of elements depending on other variables elsewhere in the program.
I've looped this code:
Dim rep As Long = 1023
Dim items As List(Of String)
items.Add("First Entry")
items.Add("Second Entry")
items.Add("Third Entry")
items.Add("Fourth Entry")
'(sake of argument, these are the variables
'that will be changing vastly earlier
'in the program, I put them in this way to simplify
'this part of my code and still have it work)
Dim myList As New List(Of List(Of String))
Dim tempList As New List(Of String)
For index = 1 To Len(rep.ToString)
tempList.Add(items(CInt(Mid(rep.ToString, index, 1))))
Next
myList.Add(tempList)
tempList.Clear()
My issue is with that last part; every time I add the tempList to myList, it's fine, but when I clear tempList, it also clears the version of tempList in myList.
myList will have a count of 1, but the list inside it has a count of 0 as soon as I clear tempList. And I have to clear tempList because I'm looping this section of code over and over, a variable number of times.
Is there a way around this? Am I being a horrible noob?
You're using the same tempList each time, instead of making a new one.
You likely need to do:
myList.Add(tempList)
tempList = new List(Of String) ' Create a new List(Of T), don't reuse...