Link to home
Start Free TrialLog in
Avatar of NigelRocks
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?
Avatar of Omego2K
Omego2K
Flag of United States of America image

If it's just strings then you can do this:

listBox1.Remove("ValueOfItem");

also if you don't know the value and want to remove the selected value then you do this:
listBox1.Remove(listBox1.SelectedItem);
Avatar of NigelRocks
NigelRocks

ASKER

The syntax you provided gives me an error:

chkLstRole.Remove("ActiveMember")
 
Doing it like this doesn't give an error, yet doesn't work.
chklstRole.Items.Remove("ActiveMember")
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?
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("ActiveMember")
chkLstRole.Items.Remove(listIndex)

 
This still does nothing.
How are you adding items to the ListBox?
By calling a method that returns a data table.
That makes no sense, can you show the code? Are you binding to the datatable?
Here's the inital line that loads the listbox:

oLookupEntity.FillLookupControl("ROLE", 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.
 
 
 
 

        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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.SelectedItem)

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
I thought you were looking for alternatives to removing by index?