Hey everyone,
I have a listBox that contains number of items & I have a textbox,Command button.
What I want is that once I press on the command button,the item which is written in the textbox is now highlighted in the listbox
I use VB6
Please help !

  1. You'll need to use the list Property of the list box control.
  2. You'll need to use some kind of loop to search through the values of the list.
  3. You'll use your code to match the value of the text box to the value of the item in the List box.

A few pointers:

  1. Find the total of the list count first by using one of the properties of the List Box
  2. Use the List property with its Index value to return the value of that index in the list.
  3. Use the selected property of the list control to with the same index value to select the item in the list.
  4. Use exit to exit out of your loop when you've found the answer.

Here's a few lines of code to get you started:

Private Sub cmdListHighLight_Click()
    Dim strSearch As String
    Dim intCounter As Integer, intListTotal As Integer
    '
    strSearch = txtInput
    intListTotal = lstResults.ListCount - 1
    
    For intCounter = 0 To intListTotal
        ' Enter your code here.
        ' You don't have to use a for loop.
    Next
    
End Sub

Thank you for your help
but this is my code :

Private Sub Command1_Click()
Dim i As Integer
For i = 0 To List1.ListCount - 1
List1.ListIndex = i
If List1.Text = txtsearch.Text Then
MsgBox " your item has been founded" & i, vbInformation + vbOKOnly, "founded"
Exit Sub
End If
Next i
MsgBox " Not founded", vbExclamation + vbOKOnly, "not founded"
End Sub

& i dunnw why it does not work !

I made it work with this edit :

Private Sub Command1_Click()
Dim i As Integer
For i = 0 To List1.ListCount - 1
List1.ListIndex = i
If List1.List(i) = Text1.Text Then
MsgBox " your item has been founded" & i, vbInformation + vbOKOnly, "founded"
Exit Sub
End If
Next i
MsgBox " Not founded", vbExclamation + vbOKOnly, "not founded"
End Sub

But the problem now is that i have 2 similar items in the list or even three
say for example that i have two words both are (book) ,what he made is that he selected only one of the 2 items
i want to make it highlight both the 2 similar items
do you have any idea how i can do such a thing ?
& Thank you again :)

You have a MultiSelect property on the list box that should handle what you want to do.

There are 3 properties. You'll have to choose the appropriate value.

Hint: Only one will work for your desired outcome. And you'll have to change the logic on the if / end if. If you find one and then exit the sub, then you will not find the other.

Private Sub Command1_Click()
     Dim i As Integer
     For i = 0 To List1.ListCount - 1
          List1.ListIndex = i
          If List1.List(i) = Text1.Text Then
               MsgBox "Your item has been found." & i, vbInformation + vbOKOnly, "Found"
               Exit Sub
          End If
     Next i
     MsgBox "Not found", vbExclamation + vbOKOnly, "Not found"
End Sub

hi friend how about using the listview in vb6! thank you

How about make your own thread?

This article has been dead for over six months. Start a new discussion instead.