Search Listbox

Posted on 2004-10-14
Last Modified: 2012-06-27
   I wanted to add an item to a listbox.  Before adding I want to see whether the listbox has the item if it has then it should not add to the listbox.  What is the code in c#/
Question by:miyengar
LVL 53

Expert Comment

ID: 12313416

I have moved this to the topic where I thing you will have a better chance of getting a solution.

Page Editor

Expert Comment

ID: 12313652
You can do this in C#

bool b = listBox1.Items.Contains((string) "TestString");
if the string exists then it will return true else it would return false.

If you want to know the location of the string if it exists you could do this
int n  = listBox1.Items.IndexOf((string) "TestString");
This would return the index of the string in the listbox. it it doesn't exist then it will return -1.

for VB you would not need the cast (string) portion.

Hope this helps.


Expert Comment

ID: 12316101
Try this if you are in VB.NET

    Private Sub AddInList(ByVal pItem As String)
        If (ListBox1.Items.Contains(pItem) = False) Then
        End If
    End Sub

Call this function with the passed value you want to add in your listbox. It will add the item in the listbox if it is not exists in the listbox.


Author Comment

ID: 12319220
Tried all the above methods gives me the following error.
My code is
bool b = lstlinenumber.Items.Contains((string)"txtlinenumber.Text");
--The best overloaded method match for 'System.Web.UI.WebControls.ListItemCollection.Contains(System.Web.UI.WebControls.ListItem)' has some invalid arguments

Accepted Solution

GrumbleBot earned 50 total points
ID: 12321642
bool b = lstlinenumber.Items.Contains((string)"txtlinenumber.Text");

You would not want the quotes in your example, because the .Text is already a string value.

Try this

bool b = lstlinenumber.Items.Contains((string) txtlinenumber.Text);

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now