?
Solved

Search Listbox

Posted on 2004-10-14
5
Medium Priority
?
374 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
[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
5 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 12313416
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
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.

0
 
LVL 5

Expert Comment

by:KarcOrigin
ID: 12316101
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
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
0
 
LVL 3

Accepted Solution

by:
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);
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
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 Month9 days, 13 hours left to enroll

762 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