Removing items from a listbox in VB?
Solved
nico8002
Posted messages
85
Status
Membre
-
sky -
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
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
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
Do you have any idea how to do that?
Because I'm starting with VB, I'm a bit in the dark :s
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?
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?
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.
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.
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.
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.
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:
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
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."
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."
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.
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.
I just did a quick test that seems to work:
Normally, it only deletes the selected items.
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.
Hello,
Apparently you are in VB.Net, I do not know the syntax but in VB6 I would write...
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)
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)
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?
The downside is that I don't have any issues.
Can you show me the function that fills your listbox?
```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 ```
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 ```
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:
I don't have a database on hand to test...
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...
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.
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.
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.
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
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