Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

Listbox add items and clear all items

Hi there,
This question is a two parts question.
First, how can i remove all the items of a listbox?
I try listbox1.clear() but is not an option for aspx
Second, if i want to add a item to my listbox how can i check that an item with the same name is already there. How cam i do this?
Im working on a web app on VS 2005.
  • 4
  • 3
1 Solution
COHFLAuthor Commented:
for the remove should i do it like this:
Dim item As ListItem
                For Each item In Me.lstCaseContents.Items
or there is another effective way?
Fernando SotoCommented:
Removes all List Items  from the ListBox.

Fernando SotoCommented:
To see if the ListBox already has a ListItem of the same value try this.


Where item is of type ListItem that you want to check before adding to the ListBox.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

COHFLAuthor Commented:
Here is my problem it will work find if my initial value is a listboxitem. The item i'm checking to see if exist is a string

so how can convert it to string if possible?
Dim source as string = "Something"
if lstbox.items.contains(source) then
'do something
end if

if i put that code i get an error saying value of type 'String' cannot be converted to 'System.web.ui.webcontrols.listitem'.
any ideas?
Fernando SotoCommented:
What version of the .Net Framework?
Are you using Visual Studio 2008?
COHFLAuthor Commented:
VS 2005
Fernando SotoCommented:
This should do what you need.
' String to find
Dim findThis = "Text String"

' Switch to see if it was found
Dim Found As Boolean = False
' Iterate through the ListItem collection
' To check if it is in the list or not
For Each item In lstCaseContents.Items
    ' Test to see if this is what you are looking for
    If item.Text = findThis Then
        ' It was found so set switch and exit loop
        Found = True
        Exit For
    End If

' If it was not found add it to the list
If Not Found Then
    item = New Web.UI.WebControls.ListItem
    item.Text = "String To Find"
    item.Value = "Its Value"
End If

Open in new window


Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now