Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Search Listbox

Posted on 2004-10-14
Medium Priority
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#/vb.net?
Question by:miyengar
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
LVL 53

Expert Comment

ID: 12313416

I have moved this to the vb.net 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 150 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

618 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