How to delete same items from multiple list box? - vb.net

I am facing a problem while deleting the items from multiselect list boxes. I will try my best to explain my front end and the logic. Please let me know the correct way to get the output.
Well I need to add new subjects into a list box. I can select multiple items once added into it. I have 3 list boxes first listbox is for holding the major subjects. The second one is for holding the optional subjects. I have two text boxes that feeds the data into the list box, the items on the text box will be added when the button or enter key is pressed.
The third listbox is disabled. It takes the items from the majorsub and optsubs listbox. This means if I add 10 major subjects and 5 optional subjects then the 3rd list will hold this 15subjects. Now I can easily multi select from one list and delete it. Deleting individually is not a problem.
My concern here is - once I select the items from this two list and click delete button it has to search the same items from the third list and remove it from this list as well.
How can I do that? Please help me in this.
Add items in list 1
Remove items from list 1 and 3
This is for inserting and removing the major subs
Public Sub btnMajSubAdd_Click() Handles btnMajSubAdd.Click
Try
'CODE TO ADD MAJOR SUBJECTS IN TO THE LIST AND THE ALL SUBJECTS LIST
If btnMajSubAdd.Text = "+" Then
If txtMajSubs.Text <> "" Then
lbMajorSubs.Items.Add(Trim(txtMajSubs.Text))
lbAllSubs.Items.Add(Trim(txtMajSubs.Text))
txtMajSubs.Text = ""
txtMajSubs.Focus()
End If
Else
For n As Integer = 0 To lbMajorSubs.SelectedItems.Count - 1
' REMOVE THE CURRENT SELECTED ITEM FROM ITEMS
For i As Integer = 0 To lbAllSubs.Items.Count - 1
If lbAllSubs.Items(i).ToString = lbOptSubs.SelectedItems(n).ToString Then
lbAllSubs.Items.Remove(lbAllSubs.Items(i))
lbMajorSubs.Items.Remove(lbMajorSubs.SelectedItems(n))
i = i - 1
n = n - 1
End If
Next i
Next n
btnMajSubAdd.Text = "+"
txtMajSubs.Focus()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

The recommended way to do this is with the Model-View-Controller pattern. Instead of adding an item directly to the list box, add it to a collection, then refresh the list box with items in the collection.
In this particular case, maintain two collections, to which you add major subjects and minor subjects, respectively. On adding a new subject, you add it to the appropriate collection, then immediately refresh the list box.
Upon removing an item from the list box, remove the corresponding item from the collection, and refresh the list box again.

Related

Access VBA reselect items in List Box

I'm working on a DB where the users have to select items from multiple list boxes, at the end of the all of this, the form closes and opens a new form. All of that works great.
My issue is that when the user goes back to the original form, I want it to load in with their list box items already highlighted. Sort of a "here is where you left off" kind of thing.
I have code that works, but it's somewhat limited:
If InStr(me.Backup_Chain, "Apple") > 0 then
Me.List_Type.Selected(0) = true
End if
If InStr(me.Backup_Chain, "Grapes") > 0 then
Me.List_Type.Selected(1) = true
End if
"List_Type" is just a textbox that I populate with the users previous selection. So if they selected Apples and Grapes it reads: 'Apples','Grapes'
This works pretty well, but it's not dynamic. So if I add "Bananas" to the list, Bananas because item number 1 in the list, and thus screws up all of my other references.
So, my question is 2 parts:
Is there an easier way or reselecting the list box items?
If not, is there a way to make the Selected(#) dynamic? i.e. to tell that "Grapes" is in location 1 vs 2.
Thanks everyone!
Edit: In addition to this, I have a run a query to filter one of the list boxes down to a subgroup of clients (based on the other list box selections). Which means I need to refresh the page. So I can't get by with just hiding the form.

How to get each item from a listbox and store it to multiple textboxes in VB.NET

If you click this link to see this image, I have a listbox containing various numerical items. I like to know how I can get each item from the list and store it to the textboxes.
Since each textbox is named TextBox1, TextBox2, TextBox3, etc, I tried using this code,
For i = 1 To 15
Me.Controls(String.Format("TextBox{0}", i)).Text = item.Value
Next
But it only gets the final item from the textbox and puts them all in the first 15 Textboxes. Click to view the output.
What I would like is a loop code that will get the first item from the listbox and store it in textbox1, get the second item from the listbox and store it in textbox2, and so on.
Any help is appreciated. Thank you
As per jmc's suggestion you should also pull a different item from the list:
For i = 1 To 15
Me.Controls(String.Format("TextBox{0}", i)).Text = LISTBOXNAME.Items(i - 1).Value
^^^^^^^^^^^^^^^^^^^^^^^^
Next
Remember that arrays are indexed from 0
-- none of this is a great way to do these things, incidentally. Your textboxes should be well named, you should parse your text into proper class objects with named properties etc.. but if all youre after is a quick hack now that will be hard to maintain in the future (as having a single form with X textboxes on named textbox1, 2, ... 37 wwould be) then it'll suffice

How to delete multiple selections in a listbox?

So I have a userform that has a:
listbox called "lstentries"
radio button called "Delete".
Also, the "Start" is the name of the cell that is the top left corner of the table of rows.
I already set the code so it can select multiple entries in the listbox. However, when I select multiple entries it only deletes the first row that it comes to. So i tried making a while loop so it keeps deleting the selected ones, but that's not working either. I get the "Run-time error '1004' - Method 'Range' of object'_Global' failed"
Hopefully someone can help me out. Here is the snippet of the code where it deletes the rows. Thanks in advance.
If optDelete.Value = True Then
Dim delete As Range
Do While True
Set delete = Range("start").Offset(lstEntries.ListIndex, 0)
delete.EntireRow.delete Shift:=xlUp
Loop
End If
In the list box you can run through the list of items. Since you are allowing for multiple items to be selected and deleted, you will have to adjust the index as items are removed.
The list box is 0-indexed which means it starts at 0 and not 1.
' set the to go through all items in the list
For listIndexCount = 0 To lstEntries.ListCount
' check to make sure the index count is not greater or equal to the total number of items
' this is needed because the ListCount will change if items are deleted
If listIndexCount >= lstEntries.ListCount Then
Exit Sub
End If
' check to see if the item is selected
If lstEntries.Selected(listIndexCount) Then
' remove item
lstEntries.RemoveItem (listIndexCount)
' subtract 1 to account for removed item
listIndexCount = listIndexCount - 1
End If
Next
Another option in cases like this is to iterate through in reverse order. When starting at the bottom of the list you do not have to worry about adjusting the index since the items further up are not affected by removing an item below them.

vb.net listbox- Remove ALL items that DON'T contain specific text

I'm working with a listbox in vb.net and am trying to remove all items from the listbox that don't contain specific text at the click of a button. Here's my code:
Dim i As Integer
For i = 0 To ListBoxPrePublish.Items.Count - 1
If InStr(ListBoxPrePublish.Items(i), "-8-") > 0 = False Then
ListBoxPrePublish.Items.RemoveAt(i)
Exit For
End If
Next
This only removes 1 item at a time though. How can I tweak this to remove all items that don't contain "-8-" at once?
EDIT: in case anyone asks, the listbox items list is growing rather large so I'm adding a sort feature so users can widdle down their options if they want to. That's why I'm not filtering anything before adding to the listbox
Here is the complete code for backward loop I mentioned in the comments - it should work:
For i as Integer = ListBoxPrePublish.Items.Count - 1 To 0 Step -1
If Not ListBoxPrePublish.Items(i).Contains("-8-") Then
ListBoxPrePublish.Items.RemoveAt(i)
End If
Next
No, I do not know of any RemoveRange type functionality. And be advised that you will need to loop through the listbox Items collection backwards as you remove items or you will get index exceptions, because once you remove something it will mess up the index values of all the remaining items in the iterator.

VB.NET Listview Textchanged Search

I've tried searching on stackoverflow and implementing code that I found but to no avail. As the text changes I want to use the current text in the textbox to filter through the listview items (so the items that are not closed to being matched get removed) and it only leaves me with whatever is contained in the columns.
Here's an example of what I mean:
Search: "Georges"
|1|Anderson Silva|Anderson silva is the champion in the ...|
|2|Georges St-Pierre| Georges is the champion in the ...|
|3|Georges Sotoropolis| Georges Sotoropolis is a fighter in the lightweight division|
With this search, only rows 2 and three would be returned. The first row would be omitted and not displayed. Once I erase the terms, then it would get displayed.
Here is the code that I currently have:
Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
lwArticles.BeginUpdate()
If tbSearch.Text.Trim().Length = 0 Then
'Clear listview
lwArticles.Clear()
'If nothing is in the textbox make all items appear
For Each item In originalListItems
lwArticles.Items.Add(item)
Next
Else
'Clear listview
lwArticles.Clear()
'Go through each item in the original list and only add the ones which contain the search text
For Each item In originalListItems
If item.Text.Contains(tbSearch.Text) Then
lwArticles.Items.Add(item)
End If
Next
End If
lwArticles.EndUpdate()
End Sub
It seems to be working but I cannot see the listview items once I enter something in the tbSearch. The scrollbar gets smaller / larger depending if there are more / less items due to the search being executed. My problem seems that they are not visible
Thank you!
Listview.Clear wipes out the items, columns, and groups. It appears you only want to wipe out the items, so call lwArticles.Items.Clear instead of lwArticles.Clear
I would suggest another approach - first create a DataTable based on your original items. Then create a DataView and assign that as DataSource of your ListView. Now you can change DataView.RowFilter, and your list will update automatically, so only items matching the filter will show up. Using this approach you don't need to recreate everything from scratch every time, and your TextChanged becomes very simple and maintainable - it just changes RowFilter, if a corresponding DataView has already been created.
Here's the final answer of my question:
Private originalListItems As New List(Of ListViewItem) 'this gets populated on form load
Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
lwArticles.BeginUpdate()
If tbSearch.Text.Trim().Length = 0 Then
'Clear listview
lwArticles.Items.Clear()
'If nothing is in the textbox make all items appear
For Each item In originalListItems
lwArticles.Items.Add(item)
Next
Else
'Clear listview
lwArticles.Items.Clear()
'Go through each item in the original list and only add the ones which contain the search text
For Each item In originalListItems
If item.Text.Contains(tbSearch.Text) Then
lwArticles.Items.Add(item)
End If
Next
End If
lwArticles.EndUpdate()
End Sub
Credits to Dan-o for explaining that lwArticles.Clear() will clear everything. He notified me that I needed lwArticles.Items.Clear() to clear only the items in the listview.