Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare Selected Listbox item to items in ListView

Status
Not open for further replies.

Shift838

IS-IT--Management
Jan 27, 2003
987
US
Need a little help here. I must have a brainfart going on or something.

I have a Listview and a Listbox. The listview contains 3 columns (Filename, extension and size). I select a item in the list box and click add and it will add it to the list box.

I want to be able to remove it from the list box if I have added the wrong file. So I want to take the text from the listbox selected item and compare it to the text of the items by looping through all the items in the Listview.

No matter what I have tried, it never sees both items text values as the same, even if they are. I have tested to display the text via a message box and it looks right. code below. I am sure it is something with the code, but I cannot figure it out.
Code:
Dim ctext, rtext As String
        Dim icount As Integer
        rtext = ListBox1.SelectedItem

        For icount = 0 To ListView1.Items.Count - 1

            ctext = ListView1.Items(icount).Text
            If ctext = rtext Then
                'display 'found message
                MsgBox("found")
            Else
                'display Not found message
                MsgBox("not found")
            End If

        Next
 
You said your [tt]ctext = rtext [/tt]

What do you get in the Immediate Windo if you do:
[tt]
Debug.Print "ctext is *" & ctext & "* and rtext is *" & rtext & "*"[/tt]

* is to make sure there are no spaces before or after the text.

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
There are no trailing spaces. I thought the same thing yesterday and checked. The strings are identical when it loops through.
 
it seems if I add the item in the collection listbox control on the form it finds the item with the below code. but if I add the item to the listbox from the listview control and try to remove the item with the same code, it does not find it.

Anyone know why?

Code:
Dim lbitem, lvitem As String

        lbitem = ListBox1.SelectedItem.ToString

        For i = 0 To ListView1.Items.Count - 1
            lvitem = ListView1.Items(i).Text
            If lvitem = lbitem Then
                MsgBox("found")
            Else
                MsgBox("not found")
            End If
        Next

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top