?
Solved

Listbox add items and clear all items

Posted on 2010-01-12
7
Medium Priority
?
333 Views
Last Modified: 2012-05-08
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
0
Comment
Question by:COHFL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

by:COHFL
ID: 26295437
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 26295759
Removes all List Items  from the ListBox.

ListBox1.Items.Clear()
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 26295836
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:COHFL
ID: 26296572
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 26296723
What version of the .Net Framework?
Are you using Visual Studio 2008?
0
 

Author Comment

by:COHFL
ID: 26296755
VS 2005
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 1000 total points
ID: 26297533
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

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question