Deleting Items from a Listview through the Use of checkboxes - vb.net

My program takes a checkboxed item and depending on the serial number that is present on the item on the invoice, subtracts one from the amount list
I have the following in a listview on a details page which I added with the following code:
Item - Low Socks(pink)
Serial # - 34-75-860
Price - 5.89
Amount - 12
Except they are in columns instead of rows like above
Dim items As New ListViewItem
items = ListView1.Items.Add("Low Socks(Pink)")
items.SubItems.Add("34-75-860")
items.SubItems.Add("$5.89")
items.SubItems.Add("12")
items = ListView1.Items.Add("Low Socks(Black)")
items.SubItems.Add("34-75-900")
items.SubItems.Add("$5.89")
items.SubItems.Add("25")
items = ListView1.Items.Add("Low Socks(Red)")
items.SubItems.Add("34-75-756")
items.SubItems.Add("$5.89")
items.SubItems.Add("10")
items = ListView1.Items.Add("Low Socks(Orange)")
items.SubItems.Add("34-75-234")
items.SubItems.Add("$5.89")
items.SubItems.Add("34")
items = ListView1.Items.Add("Low Socks(Blue)")
items.SubItems.Add("34-75-598")
items.SubItems.Add("$5.89")
items.SubItems.Add("23")
End Sub
Under my invoice page I have checkboxes next to the items on the invoice. When the checkbox is clicked I want the amount to decrease by one. I will go in later and change it to the actual amount it needs to be depending on how many of that item they ordered... my coding for my checkbox is this:
Dim item As ListViewItem
Dim i As Integer
Dim count As Integer
'count the number of items in itemdetails2 listview
count = ItemDetails2.ListView1.Items.Count - 1
'loop to read each item in the list
For i = 1 To count
If i > count Then Exit For
item = ItemDetails2.ListView1.Items(i)
'compare the item to the serial number
If item.Checked = True Then
If (item.SubItems(0).Text = "34-75-860") Then
item.SubItems(2).Text -= 1
End If
i = i + 1
count = count - 1
End If
Next
ItemDetails2.Show()
End Sub
Right now it doesn't look like it does anything. I have tried changing my index's on my subitems to 1 and 3 instead of 0 and 2 but i figured because they are subitems that they need to be subitem index 0 and subitem index 2 since there is one item and three subitems to that one item. if that makes sense....
please help.

Without digging into your indexing stuff i can notice that your are treating strings as like as numeric
item.SubItems(2).Text -= 1
When you should try :
item.SubItems(2).Text -= CStr(CDec(item.SubItems(2).Text) - 1D)
I hope that helps...

Related

Transferring items from one listBox to another list

i have ListBox1 and ListBox2 and textbox
I want the following when setting a value in a TextBox. If the value in ListBox is1, then items are moved from the current value to the last items in not the first. to listbox2
Dim foo As String
Dim index As Integer = 0
Dim good As Integer = 0
foo = TextBox1.Text
ListBox2.Items.Clear()
For i = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).ToString = foo Then
index = i
End If
If i >= index Then
ListBox2.Items.Add(ListBox1.Items(good).ToString)
End If
Next
As Lars has noted in a comment, the index is 0 at the beginning so the condition i >= index wil be always true. So you need to intialize it with Int32.MaxValue.
You add always the first item, because good is always 0. You should use the loop variable i:
ListBox2.Items.Add(ListBox1.Items(i).ToString())
Here is a LINQ version which simplifies the whole code, you don't need more:
ListBox2.Items.Clear()
Dim allAfter = ListBox1.Items.Cast(Of Object).SkipWhile(Function(item) Not TextBox1.Text.Equals(item))
ListBox2.Items.AddRange(allAfter.ToArray())

I need to add all of the items in a ListBox into a "total" variable

Here is an example of the For loop that I'm trying to run where lstCost is my ListBox that is formed by selecting options from two other ListBoxes and calculating a total for those option to create an item total and totalCost is the variable where I'm trying to save the total of all list items.
Dim totalCost As Integer
For x As Integer = 0 To lstCost.Items.Count - 1
totalCost += Val(lstCost.Items.Item(x))
Next
txtTotalCost.Text = totalCost.ToString
For some reason, I end up with a 0 as my total in my txtTotalCost TextBox. Can anybody think of any reason why?
Dim totalCost As Double = 0
For x As Integer = 0 To lstCost.Items.Count - 1
totalCost += DirectCast(lstCost.Items.Item(x).ToString.trim,Double)
Next
txtTotalCost.Text = totalCost.ToString
depending on your values in listbox you can change it to Decimal if required

Adding text between listboxes items

I'm using vb.net form.
I have 8 listboxes (listbox1 , listbox2,listbox3..) each one contains many items and another empty listbox (listbox10). I'm trying to add first listboxes items into the listbox10
ex: ) listbox1 first items is 'A' , listbox2 first items is 'b' , listbox3 first items is 'c'...etc . Now listbox10 first item must be "Abc..."
The problem is i want to add text ( string or number) between each listbox
ex:)listbox1 first items is 'A' , listbox2 first items is 'b' , listbox3 first items is 'c'...etc . Now I want listbox10 first item to be "The letters ABC is now done"
The main idea is to combine all listboxes items and adding text between them to create a meaningful sentence
here is my code
Dim controls = New List(Of ListBox)() From _
{ListBox1, ListBox2, ListBox3, ListBox4, ListBox5, ListBox6, ListBox7, ListBox8}
Dim minCount = Controls.Min(Function(x) x.Items.Count)
For x = 1 To minCount - 1
ListBox10.Items.Add(String.Join(" ", controls.Select
(Function(lb) lb.Items(x).ToString)))
Next
End Sub
Where i must to add my (string)?
Since you want to put different text between the listbox items, you would have to drop the String.Join function and loop through the ListBoxes yourself:
Dim sb As New StringBuilder
For i As Integer = 0 To controls.Count - 1
For j As Integer = 1 To minCount - 1
sb.Append(controls(i).Items(j).ToString)
Next
If i < controls.Count - 1 Then
sb.Append(" " & i.ToString & " ")
End If
Next
ListBox10.Items.Add(sb.ToString)

Getting the index of an item inside listview vb.net

how do i get the index of an item inside the listview by looping?
for i = 0 to Listview1.items.count -1
??????????????????
next
so that i can get the index and validate it.
i know how to get the index using selectedindices.
i really wanna know how to get the index in a looping method
just want to clarify
i was working on a listview that have a hotkeys from keypress 0 to 9
when keypress 0 is pressed, item1 will be inserted inside the listview and if i pressed 0 again the quantity column will increment, i manage to do that. but the problem is when i press keypress 1 first (which has the item2) it will go inside the listview then if i press keypress 0 im having an error because of the incriminating of the quantity. i think finding the index and loop it will solve the problem.
this is how i manage to incriment the quantity of the item1 in column 4 or element(3)
Dim quantity As Integer = CInt(cartListView.Items(0).SubItems.Item(3).Text)
quantity = quantity + 1
cartListView.Items(0).SubItems.Item(3).Text = quantity.ToString
of course the index(0) gives me error when the item1 is in the index 1 of the listview
please be reminded that the item1 is static or fixed in the keypress 1. any solution or revision?
Solved:
since the item1 is static, the looping statement that vlad gave does it. it manage to search the item1 and return the index and inserted that index to the element 0 of this code
Dim textSearch = DTfsn.Rows(0)("item1").ToString 'static item for keypress 1
For i = 0 To cartListView.Items.Count - 1
If cartListView.Items(i).Text = textSearch Then
Dim quantity As Integer = CInt(cartListView.Items(i).SubItems.Item(3).Text)
quantity = quantity + 1
cartListView.Items(i).SubItems.Item(3).Text = quantity.ToString
End If
Next
now it can increase its quantity whereever the item1 is positioned in the listview.
starting from this comment
of course. i wanna know how to get the index of a searched item
I'll just guess
if you want to find the index of a specific item (in the example I'm searching for item d)
you can do something like this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim textSearch = TextBox1.Text 'text to look for
For i = 0 To ListView1.Items.Count - 1
If ListView1.Items(i).Text = textSearch Then
MessageBox.Show("text found at index " & i.ToString)
End If
Next
End Sub

Multi Select List Box

I have a list box on a form and it works fine for what I want to do.
I am wanting to edit items on the form, this means populating the listbox and then selecting the relevant items.
My listbox contains a list of item sizes, i want to select the sizes which belong to the item being edited.
PLease can someone give me some pointers.
I tried me.lstItemSizes.SetSelected(i,true) but this only works for a single item.
Any help wil be much appreciated.
My Code:
Private Sub SelectItemSizes(ByVal itemID As Integer)
Dim itemSizes As IList(Of ItemSize) = _sizeLogic.GetItemSizes(itemID)
Me.lstItemSizes.SelectionMode = SelectionMode.MultiExtended
If (itemSizes.Count > 0) Then
For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1
For x As Integer = 0 To itemSizes.Count - 1
If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
Me.lstItemSizes.SetSelected(i, True)
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Next
Next
End If
End Sub
Did you set the selectionmode to multi?
You need to specify that in order to allow multiple selections.
Then you can do:
Dim i as Integer=0
For i=0 To Me.listBox.SelectedItems.Count -1
'display the listbox value
next i
Here is a screen shot:
After you set the property on the listbox then call setselected based on the values you want selected.
me.lstItemSizes.SetSelected(3,true)
me.lstItemSizes.SetSelected(4,true)
me.lstItemSizes.SetSelected(9,true)
Here you can add 20 numbers and only select the even.
Dim i As Integer
'load the list with 20 numbers
For i = 0 To 20
Me.ListBox1.Items.Add(i)
Next
'now use setselected
'assume only even are selected
For i = 0 To 20
If i Mod 2 = 0 Then
Me.ListBox1.SetSelected(i, True)
End If
Next
3rd edit
Look at the way you are looping, lets assume I create a list of integers, my vb.net is rusty I mainly develop in C#. But assume you did this:
Dim l As New List(Of Integer)
l.Add(2)
l.Add(6)
l.Add(20)
You only have three items in your list, so first loop based on the items on your list, then within the items in your listbox, you have it vice versa. Look at this:
Dim i As Integer
Dim l As New List(Of Integer)
l.Add(2)
l.Add(6)
l.Add(20)
'load the list with 20 numbers
For i = 0 To 20
Me.ListBox1.Items.Add(i)
Next
Dim lCount As Integer = 0
For lCount = 0 To l.Count - 1
For i = 0 To 20
If i = l.Item(lCount) Then
Me.ListBox1.SetSelected(i, True)
Exit For
End If
Next
Next
In the code my l is a list of just 3 items: 2, 6, and 20.
I add these items to l which is just a list object.
So now I have to loop using these 3 numbers and compare with my listbox. You have it the opposite you are looping on your listbox and then taking into account the list object.
Notice in my for loop that once the item in my list is found I no longer need to loop so I exit for. This ensures I dont overdue the amount of looping required. Once the item is found get out and go back to the count of your list object count.
After running my code here is the result
You have to change the ListBox.SelectionMode property in order to enable multiple-selection.
The possible values are given by the SelectionMode enum, as follows:
None: No items can be selected
One: Only one item can be selected
MultiSimple: Multiple items can be selected
MultiExtended: Multiple items can be selected, and the user can use the Shift, Ctrl, and arrow keys to make selections
So, you simply need to add the following line to the code you already have:
' Change the selection mode (you could also use MultiExtended here)
lstItemSizes.SelectionMode = SelectionMode.MultiSimple;
' Select any items of your choice
lstItemSizes.SetSelected(1, True)
lstItemSizes.SetSelected(3, True)
lstItemSizes.SetSelected(8, True)
Alternatively, you can set the SelectionMode property at design time, instead of doing it with code.
According to MSDN, SetSelected() can be used to select multiple items. Simply repeat the call for each item that needs to be selected. This is the example they use:
' Select three items from the ListBox.
listBox1.SetSelected(1, True)
listBox1.SetSelected(3, True)
listBox1.SetSelected(5, True)
For reference, this is the MSDN article.
Because my code had the following loops:
For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1
For x As Integer = 0 To itemSizes.Count - 1
If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
Me.lstItemSizes.SetSelected(i, True)
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Next
Next
The first loop loops through the available sizes and the second loop is used to compare the item sizes.
Having the following code:
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Meant that even if item i became selected, it could also be deselected.
SOLUTION:
Remove Me.lstItemSizes.SetSelected(i, False) OR Include Exit For