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.
Thanks,
COHFL
COHFLAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

COHFLAuthor Commented:
for the remove should i do it like this:
Dim item As ListItem
                For Each item In Me.lstCaseContents.Items
                    Me.lstCaseContents.Items.Remove(item)
                Next
or there is another effective way?
0
Fernando SotoRetiredCommented:
Removes all List Items  from the ListBox.

ListBox1.Items.Clear()
0
Fernando SotoRetiredCommented:
To see if the ListBox already has a ListItem of the same value try this.

ListBox1.Items.Contains(item)

Where item is of type ListItem that you want to check before adding to the ListBox.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

COHFLAuthor Commented:
FernandoSoto,
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"
lstbox.items.add(source)
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?
0
Fernando SotoRetiredCommented:
What version of the .Net Framework?
Are you using Visual Studio 2008?
0
COHFLAuthor Commented:
VS 2005
0
Fernando SotoRetiredCommented:
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
Next

' 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"
    lstCaseContents.Items.Add(item)
End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.