Modify code to get listview string quantity by looking for some string if it present or not - vb.net-2010

I am using this code to get the quantity of strings from list view1 and save to another list view2 for now this code will transfer all strings that are in same column but I want to transfer all strings with condition if "some string" are available in same row of that string do transfer to list view2
Dim items As IEnumerable(Of Tuple(Of String, Integer)) = ListViewJobDetails.Items.Cast(Of ListViewItem).GroupBy(Function(i) i.SubItems(1).Text).Select(Function(i) Tuple.Create(i.Key, i.Count))
For Each itm In items
MyListView.Items.Add(itm.Item1).SubItems.Add(itm.Item2.ToString())
Next

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 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.

Get ListBox and TextBox Matching Values VB.NET

I have a listbox1 containing some values
value 1
value 2
value 3
value 4
value 5
and TextBox1 containing some lines
Value 3
Value 5
Value 10
Value 14
I want to get values matching in listbox1 and TextBox1
like
Value 3
Value 5
and perform some action code if values matches and loop until last value match
I have used this code but its not giving accurate output.
Dim compare As String
Dim comparear() As String
Dim list As String
Dim listar() As String
compare = TextBox1.Text
comparear = compare.Split(vbNewLine)
list = TextBox2.Text
listar = list.Split(vbNewLine)
For i = 0 To comparear.Length - 1
For p = 0 To listar.Length - 1
If listar(p).Contains(comparear(i)) Then
txt_match.Text = txt_match.Text & listar(p) & vbNewLine
Else
End If
Next
Next
You can write
Dim result = listBox1.Items.OfType(Of String).Intersect(textBox1.Lines)
result is of type IEnumerable(Of String). I.e., you can use it in For Each or add .ToList or .ToArray to get a collection.
Note that the match is case sensitive. If you want to ignore the case, you can write
Dim result = listBox1.Items.OfType(Of String) _
.Intersect(textBox1.Lines, StringComparer.OrdinalIgnoreCase)
Since the ListBox Items are returned as Objects, I used listBox1.Items.OfType(Of String) to convert them to Strings.
As I can see in the code example that you have added, you are comparing the lines of 2 TextBoxes and joning the result into a 3rd one, You can do it with these 2 code lines
Dim result = textBox1.Lines.Intersect(textBox2.Lines, StringComparer.OrdinalIgnoreCase)
txt_match.Text = String.Join(vbNewLine, result)
If you wanted to compare the items of 2 ListBoxes
Dim r = listBox1.Items.OfType(Of String) _
.Intersect(listBox2.Items.OfType(Of String), StringComparer.OrdinalIgnoreCase)
Intersect works with any two enumerations or collections of the same type
A.Intersect(B)
returns a IEnumerable(Of T) where both A and B are themselves IEnumerable(Of T). So it does not matter whether you are using the lines of a TextBox, the Items of a ListBox casted to T or arrays or List(Of T) or anything else.
Your code does not work because of 2 different errors:
You have Option Strict Off. This hides an error showing you that you are calling the wrong overload of Split. VB tries to convert vbNewLine (which is a String) to a Char, and calls Split with the 1st char found in vbNewLine. Use Option Strict On in your project and you will get a compile error. Write this instead
comparear = compare.Split(New String() {vbNewLine}, StringSplitOptions.RemoveEmptyEntries)
listar = list.Split(New String() {vbNewLine}, StringSplitOptions.RemoveEmptyEntries)
Your match is case sensitive. Instead compare like this
If listar(p).ToLowerInvariant = comparear(i).ToLowerInvariant Then

Iterating through a multidimensional array to find the element matching that of a single dimensional array

I have an 3 array's (Drink, Food, Desert), as well as a multidimensional array named prices that stores prices for 8 items on the menu. I have a sub procedure that deals with transferring the item from the array in the listbox to the textboxes below, but am having trouble with price considering it is multidimensional
You would be better thinking of your menu and each item as objects for example, a menu contains a list of menu items and a menu item is an object that has properties such as a name, a type(Drink/Main Course/Dessert/Side),a description and a price.
So you might be better creating a Menu object that contains a list of all the menu items to be stored in it.
So first of all you want to define what your MenuItems are .. somthing like the code below. You will also want to define what sort of item it is. That is done by the Enum..End Enum block.
Friend Class FoodMenuItem
Enum ItemType
Drink
MainCourse
Dessert
Side
End Enum
Public Property Name As String
Public Property Price As Decimal
Public Property Catagory As ItemType
Public Property Description As String
Public Sub New(newName As String, newPrice As Decimal, newCatagory As ItemType, newDescription As String)
Name = newName
Price = newPrice
Catagory = newCatagory
Description = newDescription
End Sub
End Class
Next you want to create a menu which is simply a list of menu items
Dim FoodItems As New List(Of FoodMenuItem)
To add a food item to the list you need to create it and add it to your list
Dim itemtoAdd1 As New FoodMenuItem("Pasta", 4.95D, FoodMenuItem.ItemType.MainCourse, "Delicious pasta with parmesan cheese")
Dim itemtoadd2 As New FoodMenuItem("Beer", 3D, FoodMenuItem.ItemType.Drink, "Cool and refreshing")
Dim itemtoadd3 As New FoodMenuItem("Red Wine", 3.3D, FoodMenuItem.ItemType.Drink, "Fruity")
Dim itemtoadd4 As New FoodMenuItem("White Wine", 3.5D, FoodMenuItem.ItemType.Drink, "Dry")
Dim itemtoadd5 As New FoodMenuItem("Salad", 4.5D, FoodMenuItem.ItemType.MainCourse, "Crisp Salad with iceberg lettuce, tomatoes and beetroot")
Dim itemtoadd6 As New FoodMenuItem("Chocolate Fudge Cake", 4.25D, FoodMenuItem.ItemType.Dessert, "Indulgent fudge cake with fresh whipped cream")
Dim itemtoadd7 As New FoodMenuItem("Ice Cream", 4.5D, FoodMenuItem.ItemType.Dessert, "In a choice of flavours with the topping of your choice")
FoodItems.Add(itemtoAdd1)
FoodItems.Add(itemtoadd2)
FoodItems.Add(itemtoadd3)
FoodItems.Add(itemtoadd4)
FoodItems.Add(itemtoadd5)
FoodItems.Add(itemtoadd6)
FoodItems.Add(itemtoadd7)
So at some point you want to have these items in their appropriate listboxes. You could use this sub ..
Private Sub UpdateList(listBoxToUpdate As ListBox, category As FoodMenuItem.ItemType)
listBoxToUpdate.Items.Clear()
listBoxToUpdate.Items.AddRange((From item As FoodMenuItem In FoodItems Where item.Category = category Select item).ToArray)
listBoxToUpdate.DisplayMember = "Name"
End Sub
And use it like this, assuming listbox names of ListBoxDrinks,ListBoxMainCourse,ListBoxDessert
UpdateList(ListBoxDrinks, FoodMenuItem.ItemType.Drink)
UpdateList(ListBoxMainCourse, FoodMenuItem.ItemType.MainCourse)
UpdateList(ListBoxDessert, FoodMenuItem.ItemType.Dessert)
When you click an item in say ListBoxDrinks you would get the item and place its name in one textbox and its price in another textbox like this ..
Private Sub ListBoxDrinks_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBoxDrinks.SelectedIndexChanged
Dim selectedDrink As FoodMenuItem = CType(ListBoxDrinks.SelectedItem, FoodMenuItem)
TextBoxItemName.Text = selectedDrink.Name
TextBoxItemPrice.Text = selectedDrink.Price.ToString("C")
End Sub
That should do it. By the way ToString("C") in the last line will format the text to your local currency.

Search combo box dropdown list based on user input

I have a combo box with list items :
ABC
BAC
DEE
How can I have the list suggest based on input. e.g If user types A then it should show:
ABC
BAC
because they all contain the string character A.
Dim AList As List(Of String) = New List(Of String)
' Iterate through all the items in the combobox.
For Each item As Object In ComboBox1.Items
' If the current item contains A then add to the list.
If item.ToString.Contains("A") Then
AList.Add(item.ToString)
End If
Next
Or you can do it via LINQ, which is basically the same :
Dim AList As List(Of String) = New List(Of String)
Dim query = From item In ComboBox1.Items
Where item.ToString.Contains("A")
Select item
For Each item in query
AList.Add(item.ToString)
Next