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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listbox Find and replace

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
IE
Hi

How can i search a listbox for text in textbox1 and replace it with what is in textbox2

Thanks
 
Code:
textbox1.Text.Replace(OldWord, textbox2.Text)

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I may not have been clear on what i am trying to achive.

This code will serach the listbox for text enterd in textbox1
Code:
Dim listLength As Integer = (ListBox1.Items.Count - 1)
        
            For i = 0 To listLength
               
                listString = ListBox1.Items.Item(i)
          
                If InStr(listString.ToLower, TextBox1.Text.ToLower) Then
                
'replace the found item with what has been entered in textbox2
                End If
            Next

I am trying to replace the found item with the text that has been entered in textbox2

Thanks
 
Sorry, I missed the whole list box thing (half asleep lately). You are almost there.
Code:
Dim listLength As Integer = (ListBox1.Items.Count - 1)
        
For i = 0 To listLength
               
      listString = ListBox1.Items.Item(i)
      Dim strFound As Integer
          
      If InStr(listString.ToLower, TextBox1.Text.ToLower) Then
            [RED]listbox.items.item(i) = TextBox2.Text[/RED]
      End If
Next



-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I don't know why I assumed the line only had one word. Replace red with:
Code:
listbox1.items.item(i) = liststring.tolower.replace(textbox1.text.tolower, textbox2.text)

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Also with that line you don't need to do the InStr first unless you are doing more than just replacing.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks for the help.

I decided to extend the code to automate the process by using REGEX and using two list boxes. One list box (listbox1) will hold the information to be searched and the second list box (listbox2) will hold the information containing the search items.

What I am trying to do is, when the code finds the item in list box1 from list box2, it will replace the listbox1 item with the next item in the listbox2

Example:
Litbox1:
Testing1
Testing2
Testing3
Testing4

Listbox2:
Testing1
Failed to finish
Testing3
Test Passed

Final listbox1:
Failed to finish
Testing2
Test Passed
Testing4


My below code works to a point, what happens, is that it finds the item it is looking for and will replace the first found item with the next item from listbox2 but when it finds the next item, it also replaces that item with the sane item from listbox2

(Confused, I am)

Here is my code:

Code:
Dim i As Integer
        For i = 0 To ListBox2.Items.Count - 1
            RO1 = (ListBox2.Items(i).ToString)

            Dim iq As Integer
            For iq = 0 To ListBox1.Items.Count - 1
                Dim RO As String = (ListBox1.Items(iq).ToString)

                If Regex.IsMatch(RO, RO1) Then

                        ListBox1.Items.Item(iq) = (RO.Replace(RO, ListBox2.Items.Item(+1)

                End If
            Next
        Next

Where am I going wrong
 
First let me say you want to make 100% sure that ListBox2 cannot be changed while you are doing this because it is likely to cause a bunch of errors. Second if that is all the data you have you are going to get an error when it checks the Testing4 in ListBox1. ListBox2 must always contain one more data than ListBox1 or you have to do a check and not process the final line (Testing4) in ListBox1. For that very reason the situation seems really strange that you would want to offset like that.

Also what you are doing and what you said you need to do are two different things. It would really be best to layout exactly what you are trying to do with as much code as possible because we could end up back and forth with this often. From what you said (rather than your code posted) you would want to delete everything except this piece of code with the change.
Code:
Dim iq As Integer
For iq = 0 To ListBox1.Items.Count - 1
    Dim RO As String = (ListBox1.Items(iq).ToString)

    If Regex.IsMatch(RO, RO1) Then

        ListBox1.Items.Item(iq) = RO.Replace(RO, ListBox2.Items.Item([RED]iq + 1[/RED]))

    End If
Next

Another thing by that code you are working with only one word or an exact line for line so really you should just do this:
Code:
Dim iq As Integer
For iq = 0 To ListBox1.Items.Count - 1
    Dim RO As String = (ListBox1.Items(iq).ToString)

    If Regex.IsMatch(RO, RO1) Then

        ListBox1.Items.Item(iq) = ListBox2.Items.Item([RED]iq + 1[/RED])

    End If
Next
There is no reason to do a replace if you are going to replace a whole word/line.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks for the suggestions. When i run the code it does replace the found items but it also removes items.

I also discovered that the regex is causing a lot of issues, so i think i will have to drop the regex and look at a different method of searching

As what i previously posted, i am trying to search listbox 1 for items from listbox2 and if found then replace that item in listbox i with the next item from listbox2. If the code I posted does not reflect what I am trying to achieve then I apologies but that is why I have posted it, for help

This is an example of what is in the listboxes

Listbox1.items:

tag1=Stereo>=##[goto]for one
CAN=STOP>=##[goto]back
stp=pause>=##[goto]add
tag2=finish>=##[goto]Hello

Listbix2.items:

tag1=Stereo>=##[goto]for one
tag1=Stereo/task>=##[goto]for one
CAN=STOP>=##[goto]back
CAN=STOP/PAUSE>=##[goto]back
stp=pause>=##[goto]add
stp=pause>=##[goto]minus
tag2=finish>=##[goto]Hello
tag2=finish>=##[goto]Welcome

As you can see, listbox2 and listbox1 contains matching items plus listbox2 contains the items as what it should be changed to (the next item).
 
If you visibly have to represent them (like you seem to) then you might think of a ListView for the change (ListBox2) Rather than a ListBox. You can then set the text of the ListView to be what is looked for and the sub text to what it should be changed to. This would help keep it straight better and help prevent possible errors.

If you are looking for two identical strings you can just use an equal(=) symbol for the check. I've never seen Regex so I have clue of it's over all function, but an equal should be all you need.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top