Question Is it possible to open a file by number (say 4th in a list) rather than by name?

dHeather

Active member
Joined
Jun 18, 2012
Messages
27
Programming Experience
Beginner
Hello,

I have a form with a list of numbered labels on it. When a user hovers over a label a picture box updates with a thumbnail of target file. When they click the label it opens the file. At the moment I am reading the file names using the following code:

[XCODE] Private Sub FillLabelNames()
Dim di As New IO.DirectoryInfo("D:\" & FolderString)
Dim dir1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
Dim Counter As Integer = 1
Dim FileName As String
For Each dra In dir1
FileName = dra.ToString
myLabels(Counter - 1).Visible =
True
myLabels(Counter - 1).Text = FileName
Counter = Counter + 1
Next
If Counter < 22 Then
Dim i As Integer
For i = Counter To 21
myLabels(Counter - 1).Visible =
False
Counter = Counter + 1
Next
End If
End Sub
[/XCODE]

This obviously updates the labels with the file names which I then read when clicked. What I would rather do though is use numbered labels (or buttons) which when clicked open the file number that corresponds to the number clicked.

I could obviously store the filenames in an array, but that seems redundant as they are already sitting in the folder I want to read.

So back to my question, is it possible to just click a button and open say the 4th file in a folder without previously storing it's filename?

Thanks
 
There probably is a way using the API in a fancy way but I can't see any reason why you would want to not least because there is no guarantee that what is 4th in the folder when you initially read it is still 4th in the folder when you want to open it. You could of course actually name the files 1.bmp, 2.bmp etc. and cut off/add the extension as the program progresses.
 
Back
Top