Removing items from a listbox in VB?

Solved
nico8002 Posted messages 85 Status Membre -  
 sky -
Hello everyone,

I have a Select that displays the results in a listbox and I would like to be able to remove items from this listbox.

I would like to click on a name in my list and then click on a "remove" button.

Do you have any code ideas??

Thank you in advance.

Configuration: Windows XP / Internet Explorer 6.0

13 réponses

nico8002 Posted messages 85 Status Membre 1
 
Oh, but actually I don't want to remove it from my database, I just want to remove it from my list.
Do you have any idea how to do that?
Because I'm starting with VB, I'm a bit in the dark :s
1
AnthonyG
 
On your delete button, you can remove the record from your database with an SQL query like:
"DELETE * FROM YourTable WHERE YourTable.Name = ""& Me.ListBox.Text "";"
0
sky
 
too stupid
0
chrij Posted messages 124 Status Membre 49
 
Hello,
you need to get the index of the items to be removed (index i for example).

Then, you can do:
ListBox1.Items.RemoveAt(i)
0
nico8002 Posted messages 85 Status Membre 1
 
I did this

Dim ItemObject(9) As System.Object
Dim i As Integer
For i = 0 To 9
ItemObject(i) = "Item" & i
Next i
LB_ListeMachines.Items.RemoveAt(i)

But when I run my application and click the button, it highlights in yellow the line I highlighted.
0
AnthonyG
 
I'm not very imaginative this morning, but you could create a second table similar to the first in which you would make a copy of your source table. To remove elements, you would delete them as I suggested above.
It's a very dirty solution, I agree, and you must certainly be able to find better.
You could also consider storing those you want to keep in another table, instead of deleting what you no longer want, but that's hardly better.
What use do you envision for your cleaned-up list, exactly?
0
nico8002 Posted messages 85 Status Membre 1
 
Well, actually I have a database with a list of machines
and I will need to send a file to certain machines
so I display the machines (present in my database) in a listbox
and I would like to remove from the listbox to send my file only to the ones I want to send it to.
0
nico8002 Posted messages 85 Status Membre 1
 
Chrij, I did this

Dim ItemObject(9) As System.Object
Dim i As Integer
For i = 0 To 9
ItemObject(i) = "Item" & i
Next i
LB_ListeMachines.Items.RemoveAt(i)

but when I run my application and click the button, it highlights the line I highlighted in yellow.
0
chrij Posted messages 124 Status Membre 49
 
What error does it give you?

With what you just wrote, you will remove the object whose index is 9 from your list (the last i = 9).

According to what you wrote at the beginning, you want to remove what you select from the list. If that's the case, you can do:
For Each item In ListBox1.SelectedItems ListBox1.Items.Remove(item) Next
0
nico8002 Posted messages 85 Status Membre 1
 
I put what you just gave me:

For Each item In LB_ListeMachines.SelectedItems
LB_ListeMachines.Items.Remove(item)
Next

and it always highlights in yellow the line you underlined and tells me this:

Cannot modify the item collection when the DataSource property is set.
0
chrij Posted messages 124 Status Membre 49
 
try to close your DataSource before deleting:

ObjectDataSet.Clear()
0
nico8002 Posted messages 85 Status Membre 1
 
Dim ObjetDataSet As DataSet
ObjetDataSet.Clear()
For Each item In LB_ListeMachines.SelectedItems
LB_ListeMachines.Items.Remove(item)
Next

I did this, but it highlights in green what I underlined and tells me:
"The variable 'ObjetDataSet' is used before it has been assigned a value. A null reference exception may occur at runtime."
0
chrij Posted messages 124 Status Membre 49
 
Normal for the error.
You shouldn't create the dataset object.
You must have somewhere a "Data something" that is open (that's what the error said "Cannot modify the element collection when the DataSource property is set". You need to close it to modify the elements of the Listbox.
0
nico8002 Posted messages 85 Status Membre 1
 
Actually, it's not working,

Dim DT As New DataTable
DT.Clear()
For Each item In LB_ListeMachines.SelectedItems
LB_ListeMachines.Items.Remove(item)
Next

So I did what you told me for my datasource, but it still highlights the same thing and gives me the same error.
0
chrij Posted messages 124 Status Membre 49
 
I just did a quick test that seems to work:
Dim liste As ListBox.SelectedIndexCollection liste = ListBox1.SelectedIndices For Each item In liste ListBox1.Items.RemoveAt(item) Next


Normally, it only deletes the selected items.
0
nico8002 Posted messages 85 Status Membre 1
 
well it always takes away my entire list
0
nico8002 Posted messages 85 Status Membre 1
 
Dim liste As ListBox.SelectedIndexCollection
liste = LB_ListeMachines.SelectedIndices
For Each item In liste
LB_ListeMachines.Items.RemoveAt(item)
Next

I put that…
0
chrij Posted messages 124 Status Membre 49
 
Did you put it in a function?
Don't you have anything else that selects the elements from your list?

When I did my test, I selected the elements with the mouse, then I deleted the selected item.

Do you have only one element selected at a time?
0
nico8002 Posted messages 85 Status Membre 1
 
I have a "show" button to put all the names from my database into the list box.
Then, in my database, each machine has a sector; there are 2 in my case.
So I have a checkbox that shows the machines from one of the 2 sectors and another one that does the opposite.
So nothing selects my items.
0
nico8002 Posted messages 85 Status Membre 1
 
Do I need to put code in the function of my listbox?
0
lermite222 Posted messages 9042 Status Contributeur 1 199
 
Hello,
Apparently you are in VB.Net, I do not know the syntax but in VB6 I would write...
 For i = ListBox1.Count - 1 To 0 Step -1 If ListBox1.Selected(i) Then ListBox1.RemoveItem (i) i = i - 1 End If Next i

Your turn to transcribe in .net
A=

--
Experience teaches more surely than advice. (André Gide)
If you bump into a pot and it sounds hollow, it's not necessarily the pot that is empty. ;-)(Confucius)
0
nico8002 Posted messages 85 Status Membre 1
 
OK
I'm going to try to transcribe all of that
Thank you
0
lermite222 Posted messages 9042 Status Contributeur 1 199
 
In re-reading your main post, you could directly remove the line in the click event of the listBox
 ListBox1.RemoveItem (ListBox1.ListIndex)
0
chrij Posted messages 124 Status Membre 49
 
Well, the good thing is that we're working with the same version of vb.
The downside is that I don't have any issues.

Can you show me the function that fills your listbox?
0
nico8002 Posted messages 85 Status Membre 1
 
```vb Private Sub B_Afficher_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_Afficher.Click

Dim DT As New DataTable
DT.Clear()
Dim oleDbConnex As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=C:\...\BDD.mdb")
Dim oleCmd As New OleDb.OleDbCommand("SELECT NomMachine FROM Machine", oleDbConnex)

Try
Dim TA As New OleDb.OleDbDataAdapter(oleCmd)
TA.Fill(DT)
With LB_ListeMachines
.ValueMember = "NomMachine"
.DisplayMember = "NomMachine"
.DataSource = DT
End With
Catch ex As Exception
MessageBox.Show("Unable to establish a connection to the database!! " & ex.Message)
End Try

For i = 0 To LB_ListeMachines.Items.Count - 1
LB_ListeMachines.SetSelected(i, False)
Next

End Sub ```
0
lermite222 Posted messages 9042 Status Contributeur 1 199
 
Since it's fine... I don't see what the way of filling the listbox has to do with the question??
And since you have no problem, try translating the function I showed above into .net. :D
0
nico8002 Posted messages 85 Status Membre 1
 
Well, I did it, but it also deletes my entire list :/

Here is how I translated it :

For i = LB_ListeMachines.Items.Count - 1 To 0 Step -1
If LB_ListeMachines.SelectedItems(i) Then
LB_ListeMachines.Items.RemoveAt(i)
i = i - 1
End If
Next i
0
chrij Posted messages 124 Status Membre 49
 
Here is the Hermite function 222 in VB2008:
For i = ListBox1.Items.Count - 1 To 0 Step -1 If ListBox1.GetSelected(i) Then ListBox1.Items.RemoveAt(i) i = i - 1 End If Next i 
0
nico8002 Posted messages 85 Status Membre 1
 
I put them, it always deletes everything :'(
0
chrij Posted messages 124 Status Membre 49
 
I found a link that offers a solution to fill a listbox from a database.
https://codes-sources.commentcamarche.net/

Basically, you would need to put:
 TA.Fill(DT) oleDbConnex.Close() for i=0 to DT.Rows.Count -1 LB_ListeMachines.Items.Add(DT.Rows(i)("NomColonne").toString) Next 


I don't have a database on hand to test...
0
nico8002 Posted messages 85 Status Membre 1
 
Well, I filled my listbox
it's good, everything is displayed when I click.
0
chrij Posted messages 124 Status Membre 49
 
yes but if it indicates
ListBox1.Items.Count = 0 that means there is an issue.

I have a hard time understanding why..
0
nico8002 Posted messages 85 Status Membre 1
 
Yeah I admit it's weird
maybe it's because of DT.clear? since it's placed in the remove button function and just before the deletion?
0
chrij Posted messages 124 Status Membre 49
 
try removing it and see if you have a list with a slightly larger count ;-)
0
nico8002 Posted messages 85 Status Membre 1
 
Yeah, but if I remove it, it gives me the previous error, it highlights the line in yellow and tells me: "It is not possible to modify the element collection when the DataSource property is set."
0
chrij Posted messages 124 Status Membre 49
 
Hi,
I've looked into it a bit since yesterday.
The problem is that you link your data source to your list: you can't modify your list directly (risk of changing the database) and you also can't close your connection (that empties the list).

We need to find a way to fill your listbox differently so that it uses the data without being tied to the database.
The solution I posted earlier might be a way to achieve this.
0
nico8002 Posted messages 85 Status Membre 1
 
ah ok I see what you mean
which solution are you talking about?
0
chrij Posted messages 124 Status Membre 49
 
TA.Fill(DT) oleDbConnex.Close() for i=0 to DT.Rows.Count -1 LB_ListeMachines.Items.Add(DT.Rows(i)("NomColonne").toString) Next 

Logically (but is computing a question of logic?? ;-)), this retrieves the data and adds it to the items of the listbox (without binding it)
It's up to you to tell me if it makes a difference.
0
nico8002 Posted messages 85 Status Membre 1
 
This code, I need to put it in my fill button, right?
0
chrij Posted messages 124 Status Membre 49
 
yes
0
nico8002 Posted messages 85 Status Membre 1
 
ok ok
well, I'm going to try this ;)
0
nico8002 Posted messages 85 Status Membre 1
 
Here is the code for the next person who will be in the same situation as me Lol

The code for the function that fills the listbox

Private Sub CKB_ChoixToutesMachines_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
DT.Clear()
Dim oleDbConnex As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=C:\..\BDD.mdb")
Dim oleCmd As New OleDb.OleDbCommand("SELECT NomMachine FROM Machine ORDER BY NomMachine", oleDbConnex)
Try
Dim TA As New OleDb.OleDbDataAdapter(oleCmd)
LB_ListeMachines.Items.Clear()
TA.Fill(DT)
oleDbConnex.Close()
For i = 0 To DT.Rows.Count - 1
LB_ListeMachines.Items.Add(DT.Rows(i)("NomMachine").ToString)
Next
Catch ex As Exception
MessageBox.Show("Unable to establish a connection to the database!!" & ex.Message)
End Try
For i = 0 To LB_ListeMachines.Items.Count - 1
LB_ListeMachines.SetSelected(i, False)
Next
End Sub

The code for the function to remove an item from the listbox

Private Sub B_Retirer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_Retirer.Click
DT.Clear()
For i = LB_ListeMachines.Items.Count - 1 To 0 Step -1
If LB_ListeMachines.GetSelected(i) Then
LB_ListeMachines.Items.RemoveAt(i)
i = i - 1
End If
Next i
End Sub
0