ObjectListView loop through items and delete selected - vb.net

I am trying to count selected items and delete selected items with a loop by using an ObjectListView (found on sourceforge). What I have is not working
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If lstObjectMain.Items.Count > 0 Then
If lstObjectMain.SelectedItems.Count > 0 Then
Debug.Print(lstObjectMain.SelectedItems.Count)
lstObjectMain.SelectedItem.Remove()
End If
End If
End Sub

Try this:
If lstObjectMain.SelectedObjects IsNot Nothing Then
For Each a In lstObjectMain.SelectedObjects
lstObjectMain.RemoveObject(a)
Next
End If
Keep in mind that this is only removing items from the view.
It is better to modify underlying model, and then hit again lstObjectMain.SetObjects()

Related

Matching cards by using .tag

In this game I am creating, I have a set of "cards" as pictureboxes set to a random lot of images. When the game starts, the images in the pictureboxes are hidden and the user has to guess which image was in each card. I am having trouble with finding if the user's guess matches what was in the actual card.
In the code below, I am using a listbox to score the names of images in which the user can guess from.
https://imgur.com/a/xCg8X
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ClickedCard Is Nothing Then 'Make sure that a card has been clicked, otherwise the below code will fail.
MsgBox("You must select a card.")
Return 'Do not continue execution of this code.
End If
btnSubmit.Visible = True
ClickedCard.Image = imglist1.Images(ListBox1.SelectedIndex)
If ClickedCard.Tag = ListBox1.SelectedIndex Then
roundscore += 1
If roundscore = Cards.Count Then
MsgBox("All right")
End If
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Static AlreadySelected As New List(Of Integer)
If AlreadySelected.Contains(ListBox1.SelectedIndex) Then
MessageBox.Show("Already select once")
Exit Sub
End If
AlreadySelected.Add(ListBox1.SelectedIndex)
'Your other code here
End Sub
The static list will persist between calls to this sub. You will have to clear this list when you go to a new round. I hope this helps with the problem you mentioned in your comments. :-)

What's wrong with this iterating over a collection code?

I want to pass the items from ListBox1 to ListBox2, and delete them from LisBox1. It throws a null exception at "lb2.Items.Add(item)" but can't find out why. It works fine with just one item though
I tried doing a "for each item in lb1.items... lb2.items.add(item) + lb1.items.remove(item)" but it wouldn't work for you can't modify a list while iterating over it or an exception will be thrown. Also tried other different approaches but couldn't make it work
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If lb1.Items.Count > 0 Then
Dim itemsAPasar((lb1.Items.Count - 1)) As Object
For Each item In lb1.Items
itemsAPasar(UBound(itemsAPasar)) = item
Next
For Each item In itemsAPasar
lb2.Items.Add(item)
Next
For Each item In itemsAPasar
lb1.Items.Remove(item)
Next
End If
End Sub
It looks like you could simplify your approach a bit...
Dim itemsToMove = lb1.Items.ToList()
For Each item in itemsToMove
lb1.Items.Remove(item)
lb2.Items.Add(item)
Next

vb: How to select multiple items in a Listbox at once?

I have a Listbox with a list of numbers from 1 to 10.
Now I want to program to select those numbers greater than 5. But I also want to trigger the SelectedIndexChanged event only once.
I know I can add multiple items into Listbox at once by using addrange() method.
But it seems there isn't a similar solution for select multiple item at once ?
How can i do this ?
youre question is a litle unclear but ...
First you need to set your Listbox SelectionMode to MultySimple.
Then you use ListBox1.SelectedItems.Count < 2 So it trigger the SelectedIndexChanged event only once at the start of the selecting.
Of course you can edit the code to fit your needs and let it trigger whenever you want.
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedItems.Count < 2 Then
MsgBox("one")
End If
End Sub
And to select everything above 5 you need to make a list of integers.
Then use a for each loop to get the items in the list of integers with the values you want.
Then use the list of integers in a for loop to select the items in the listbox.
Dim l As New List(Of Integer)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Item As Integer In ListBox1.Items
If Item > 5 Then
l.Add(ListBox1.FindString(Item))
End If
Next
For SetItem As Integer = 0 To l.Count - 1
For i = 0 To ListBox1.Items.Count - 1
If i = l.Item(SetItem) Then
ListBox1.SetSelected(i, True)
Exit For
End If
Next
Next
End Sub

How to use IF AND statement with events

I want to do something if both a Listbox Item is selected and a Button is clicked. What I'm thinking is like this but it clearly isn't correct.
If ListBox1.SelectedIndex >= 0 And btnConvert_click Then
(This is under the ListBox1_SelectedIndexChanged Private Sub)
You would need to do something like this
Private sub btnConvert_click (sender as Object, e as EventArgs) Handles btnConvert.Click
If Combo.SelectedIndex > -1 Then ' SelectedIndex= -1 --> nothing selected
' Do your code
End If
End Sub
And in your case you wouldn't use Combo_Selected. This is if you want select the item and click the button. Remember, first item has index "0".

Deleting a record from the datagridview

In VB2012, I have a created a student management software with SQL2012 as the back-end. The below code is used for deleting a selected record from the datagridview when DELETE button is clicked:
Private Sub Delete_Click(sender As Object, e As EventArgs) Handles Delete.Click
Dim DelStudent As StudentDataSet1._masterRow
DelStudent= CType(CType(StudentmasterBindingSource.Current, DataRowView).Row, StudentDataSet1.Student_masterRow)
DelStudent.Delete()
Me.Validate()
Me.StudentmasterBindingSource.EndEdit()
Me.Student_masterTableAdapter.Update(StudentDataSet1.Student_master)
End Sub
But instead of the selected row being deleted, the first row gets deleted. Please help...
Selected row is not necessary current, from the perspective of binding source. You need to be using DataGridView.SelectedRows. Each is a DataGridViewRow, and you should be able to retrieve DataGridViewRow.DataBoundItem, cast it to DataRow, and use Delete on that.
the code below is also helpful
Private Sub btnRemoveItem_Click(sender As Object, e As EventArgs) Handles btnRemoveItem.Click
If datagridviewname.SelectedRows.Count > 0 Then
For i As Integer = datagridviewname.SelectedRows.Count - 1 To 0 Step -1
datagridviewname.Rows.RemoveAt(datagridviewname.SelectedRows(i).Index)
Next
Else
MessageBox.Show("Select Row to remove")
End If
End Sub