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

Selecting an item in a FileListBox

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
GB
I have a fileListBox that, unsurprisingly, has a list of files in. I also have a string with a file name in. What I'd like to do is look through the list of files and highlight(select) the one that matches the filename held in the string.

Any help? - super-duper,

elziko
 
elziko -

Try this: assume your FileListBox control is named File1...

Private Sub File1_Click()
If File1.FileName = txtstring (name to compare with) Then
.
. do what you want to do
.
End If

End Sub

This should work. John
 
Thanks, but surely Filname refers to the selected file? This means that the only file I could select is the one that is already selected NOT the one in the string.

I need to cycle through ALL the files in the list box.

Any further help?

Thanks

elziko
 
If I understand correctly, here is what you need:

Code:
Dim z As Integer
        For z = 0 To (File1.ListCount - 1)
            If File1.List(z) = FileName Then
                File1.Selected(z) = True
                Exit For
            End If
        Next
 
Hi,
Try this and modify if needbe. It uses one file listbox, one textbox, and one commandbutton. You could put the code in the txtbox change event provided the filename isn't being entered by a user. <g>

Private Sub cmdFindFile_Click()
Dim Ndx As Integer
Dim Found As Boolean
Found = False
For Ndx = 1 To File1.ListCount
If Text1.Text = File1.List(Ndx) Then
Found = True
MsgBox &quot;File found&quot;, , File1.List(Ndx)
End If
Next Ndx
If Not Found Then MsgBox &quot;File not found&quot;, , Text1.Text
End Sub
 
Hi again,
I realized that I forgot the select part...here's the proper code for it. I even tested it. <g>

Private Sub cmdFindFile_Click()
Dim Ndx As Integer

For Ndx = 1 To File1.ListCount
If Text1.Text = File1.List(Ndx) Then
File1.Selected(Ndx) = True
Exit For
End If
Next Ndx
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top