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

window with a list of files

Status
Not open for further replies.

mettodog

Vendor
Joined
Jul 11, 2000
Messages
94
Location
US
how owuld i have a file list box list only files with a certain extension? say for example i want to list only files with the .htm or .html extension. also, how could i make it so that when you double click on a file name in the list box, it would open up that file?
 
To list only certain file types, use the Pattern property of the FileListBox control.
To look for multiple file types, semicolon separate your list. So to list only .htm and .html files, set the Pattern property to:

*.htm;*.html

To open the file??? How do you want to open the file?? Do you mean Open filename For Input/Output/... or do you mean display the file in a browser window,...????

Anyway, here is how you can start:

Private Sub File1_DblClick()
Dim l As Long
For l = 0 To File1.ListCount - 1
If File1.Selected(i) = True Then

' Do your stuff to open the file.
' To get hold of the selected file use
' File1.Path to get the path of the file
' and File1.List(i) to get the name of the file.

Exit For
End If
Next l
End Sub


Simon
 
Note about last entry -
Replace the (i)'s with (l)

Simon
 
i plan to open it in a text box, text1 to be exact
 
See my post in thread222-41653 about reading a file. Then do Text1.text = f.ReadAll instead of the line that you will see there.

Simon
 
this is my code, it doesnt look like it would work at all (it doesnt) but what can i do to solve my problem?


Private Sub Filelist_DblClick()
Dim filepath As String
Dim frmListOpen As New frmOpenTemp
Dim l As Long
For l = 0 To FileList.ListCount - 1
If FileList.Selected(i) = True Then
frmListOpen.Show
filepath = FileList.Path + "\" + FileList.List(l)
frmListOpen.OpenText.DataSource = filepath
Exit For
End If
Next l
End Sub
 
I am assuming the following:
1. you have a form in your project called frmOpentemp
2. frmOpenTemp has a text box called OpenText
3. Opentext has its MultiLine property set to True
4. your main form has a FileListBox control called FileList

and then I would do the following:

1. Add a reference to Microsoft Scripting Runtime.
2. Add the code below to the main form.

Private Sub Filelist_DblClick()

Const ForReading = 1

Dim filepath As String
Dim frmListOpen As frmOpenTemp
Dim l As Long
Dim fso As FileSystemObject
Dim f As TextStream

For l = 0 To FileList.ListCount - 1
If FileList.Selected(l) = True Then
filepath = FileList.Path & "\" & FileList.List(l)
Exit For
End If
Next l

If Len(filepath & "") > 0 Then ' something is selected
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filepath)
Set frmListOpen = New frmOpenTemp
frmListOpen.OpenText.Text = f.ReadAll
frmListOpen.Show
Set frmListOpen = Nothing
Set f = Nothing
Set fso = Nothing
Else
frmListOpen.OpenText.Text = ""
End If

End Sub


Simon
 
Seems a bit complicated for loading a file in a text box. Why not:
[tt]
Private Sub FileList_DblClick()
f = FreeFile
Open File1.FileName For Binary As #f
G$ = String$(LOF(f), 0)
Get #f, 1, G$
Close #f
frmOpentemp.OpenText.Text = G$
End Sub
[/tt]

To each his own... personally, I would use a Rich Text control, in case the file was over 32kb in size.
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Alt255,
What about if the multiselect option is set to true on the filelistbox and more than 1 file is selected?? Will your code work or will it bomb on the open statement (not sure what File1.FileName will be if more than 1 file is selected)

I do agree with you that my code is fairly long winded, but if you look at it, there are only two differences to your:
1. I find the selected file differently;
2. I open the file differently

...........

Simon
 
Ah, didn't think about multi-select. Didn't occur to me that mettdog would want to load multiple files in a text box.

As far as the methods are concerned, it's as I said... to each his own. There are multiple solutions to any given problem. All have their strengths and weaknesses.

VCA.gif

Alt255@Vorpalcom.Intranets.com
 
I don't think mettdog would want to do this either, but ......

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top