Solved

Search a listview for an item already in the list.

Posted on 2008-11-01
2
245 Views
Last Modified: 2012-06-22
Hi All,

I am using a KeyPress event to place the textbox.text into the listbox but only if it does not exist. It just keeps adding the same named item even though it is in the list, probably something simple. Here is my code:
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then

            Dim strg As String = txtAddUnit.Text

            Dim item As ListView.ListViewItemCollection = ltvUnits.Items

            Dim index As Integer

            If ltvUnits.Items(index).Text = strg Then

                Exit Sub

            Else

                ltvUnits.Items.Add(strg).BackColor = Color.LightGreen

                UnitTrackingPanel2()

                txtAddUnit.Text = String.Empty

            End If

        End If

Open in new window

0
Comment
Question by:Hawkvalley1
2 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 150 total points
ID: 22859847
try this...
    Private Sub txtAddUnit_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAddUnit.KeyPress

        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then

            If ltvUnits.FindItemWithText(txtAddUnit.Text) Is Nothing Then

                ltvUnits.Items.Add(txtAddUnit.Text).BackColor = Color.LightGreen

                UnitTrackingPanel2()

                txtAddUnit.Text = String.Empty

            End If

        End If

    End Sub

Open in new window

0
 
LVL 9

Author Closing Comment

by:Hawkvalley1
ID: 31512429
Thanks again...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
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…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

943 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

17 Experts available now in Live!

Get 1:1 Help Now