Solved

Search Listbox

Posted on 2004-10-14
5
368 Views
Last Modified: 2012-06-27
Hi,
   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?
0
Comment
Question by:miyengar
5 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
miyengar,

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

COBOLdinosaur
Page Editor
0
 
LVL 3

Expert Comment

by:GrumbleBot
Comment Utility
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.

0
 
LVL 5

Expert Comment

by:KarcOrigin
Comment Utility
Try this if you are in VB.NET

    Private Sub AddInList(ByVal pItem As String)
        If (ListBox1.Items.Contains(pItem) = False) Then
            ListBox1.Items.Add(pItem)
        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.

Cheers!
0
 

Author Comment

by:miyengar
Comment Utility
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
0
 
LVL 3

Accepted Solution

by:
GrumbleBot earned 50 total points
Comment Utility
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);
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

15 Experts available now in Live!

Get 1:1 Help Now