NigelRocks
asked on
Removing a ListBox Item
I have a listbox with 10 items and I have to remove two items at runtime after the list has been populated. I think I'll have to get the listindex out for each one based on the display string and then remove based on the listindex. Is there a better way?
ASKER
The syntax you provided gives me an error:
chkLstRole.Remove("ActiveM ember")
Doing it like this doesn't give an error, yet doesn't work.
chklstRole.Items.Remove("A ctiveMembe r")
chkLstRole.Remove("ActiveM
Doing it like this doesn't give an error, yet doesn't work.
chklstRole.Items.Remove("A
oh sorry, you need to use Items.Remove
Furthermore, is "ActiveMember" displayed in the ListBox?
Also are you adding strings to the listbox or class instances?
Furthermore, is "ActiveMember" displayed in the ListBox?
Also are you adding strings to the listbox or class instances?
ASKER
Mmmm...still not workin' for me. I've checked and I don't see where the listBox is getting repopulated. Here's my code:
Dim listIndex As Integer
listIndex = chkLstRole.FindStringExact ("ActiveMe mber")
chkLstRole.Items.Remove(li stIndex)
This still does nothing.
Dim listIndex As Integer
listIndex = chkLstRole.FindStringExact
chkLstRole.Items.Remove(li
This still does nothing.
How are you adding items to the ListBox?
ASKER
By calling a method that returns a data table.
That makes no sense, can you show the code? Are you binding to the datatable?
ASKER
Here's the inital line that loads the listbox:
oLookupEntity.FillLookupCo ntrol("ROL E", Me.chkLstRole, "")
In the attached code, the first FillLookupControl is the method that is used in our business layer. The second "FillLookupControl" is in our data access layer.
oLookupEntity.FillLookupCo
In the attached code, the first FillLookupControl is the method that is used in our business layer. The second "FillLookupControl" is in our data access layer.
Public Overridable Sub FillLookupControl(ByVal QueryNameParam As String, ByRef oCombo As System.Windows.Forms.ListBox, ByVal BlankDescription As String, Optional ByVal UseCentralDB As Boolean = False)
Try
Dim dal As New LookupDALC
Dim dt As DataTable = dal.FillLookupControl(QueryNameParam, BlankDescription, UseCentralDB)
oCombo.DisplayMember = "Description"
oCombo.ValueMember = "ID"
For Each row As DataRow In dt.Rows
oCombo.Items.Add(New Item(row("Description"), row("ID")))
Next
'TODO The selectedIndex = 0 is causing a problem
If dt.Rows.Count > 0 Then
oCombo.SelectedIndex = 0
End If
Catch ex As Exception
Throw ex
End Try
End Sub
Public Function FillLookupControl(ByVal QueryNameParam As String, ByVal BlankDescription As String, Optional ByVal UseCentralDB As Boolean = False) As DataTable
Dim sql As String = "SpDAL_GetLookupValues"
Dim dt As New DataTable
Try
If UseCentralDB Then
MyBase.Connect(SQLWrapper.AppDataBases.CCSCentral)
Else
MyBase.Connect(SQLWrapper.AppDataBases.ApplicationDB)
End If
MyBase.AddParameters("@LookupTypeDesc", QueryNameParam, SqlDbType.VarChar)
MyBase.AddParameters("@BlankDescription", BlankDescription, SqlDbType.VarChar)
dt = GetDataTable(sql, CommandType.StoredProcedure)
Catch ex As Exception
Throw ex
End Try
Return dt
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I see, so therefore you would need to remove the object if you don't want to use the index.
So first way is the way I mentioned before. Just do:
oCombo.Items.Remove(oCombo .SelectedI tem)
The code above removes the selected item.
Another way is to loop through the listbox and convert each selected item to your item class something like this:
For x As Integer = 0 To oCombo.Items.Count -1
Dim myItem As Item = Ctype(oCombo.Items(x), Item)
'Check here however you check your Item class if it's the right Item
'If it's the right item then remove like this:
oCombo.Items.Remove(oCombo .Items(x))
Exit For
Next
Also you can loop through the datatable
For Each row As DataRow In dt.Rows
If row("Description") = "ActiveMember" Then _
oCombo.Items.Remove(row)
Next
So first way is the way I mentioned before. Just do:
oCombo.Items.Remove(oCombo
The code above removes the selected item.
Another way is to loop through the listbox and convert each selected item to your item class something like this:
For x As Integer = 0 To oCombo.Items.Count -1
Dim myItem As Item = Ctype(oCombo.Items(x), Item)
'Check here however you check your Item class if it's the right Item
'If it's the right item then remove like this:
oCombo.Items.Remove(oCombo
Exit For
Next
Also you can loop through the datatable
For Each row As DataRow In dt.Rows
If row("Description") = "ActiveMember" Then _
oCombo.Items.Remove(row)
Next
I thought you were looking for alternatives to removing by index?
listBox1.Remove("ValueOfIt
also if you don't know the value and want to remove the selected value then you do this:
listBox1.Remove(listBox1.S