Hello everyone,
I don’t understand where the error is!
In a loop to list all the files in a directory and put each filename in an array variable, well I have an error! I put you the code below
Thanks for your help
Var dlg As New SelectFolderDialog
dlg.ActionButtonCaption = “Select”
dlg.Title = “Title Property”
dlg.PromptText = “Prompt Text”
dlg.InitialFolder = SpecialFolder.Documents
f = dlg.ShowModal
If f <> Nil Then
’ Use the folderitem here
Label1.Text=f.shellpath
Else
’ User cancelled
End If
Var pic() As String
Var nb As Integer
For Each file As FolderItem In f.Children
nb = nb+1
pic(nb)=file.Name
Next
Thanks, but in fact I misspoke!
The pic variable does contain all the file names, regardless of their extension (I’ll do a filter for jpg files later).
But the final goal is to display the images in this directory in a canvas, how can I do that?
That’s why I wanted to retrieve all the file names in a variable and use it later.
Sorry, I’m a beginner…
Var f As FolderItem
Var pic() As String
Var dlg As New SelectFolderDialog
dlg.ActionButtonCaption = "Select"
dlg.Title = "Title Property"
dlg.PromptText = "Prompt Text"
dlg.InitialFolder = SpecialFolder.Documents
f = dlg.ShowModal
If f = Nil Or Not f.Exists Then Return
Label1.Text=f.shellpath
For Each file As FolderItem In f.Children
If Not file.IsFolder Then
If file.Name.Right(4) = ".png" Then
// Because your previous nb=nb+1 did not increase the pic array magically
// and because the size of the array is unknow upo to this point
// you can increase it 'on thefly' using the following:
pic.Add(file.Name)
// this will add a new element on each cycle
// If you later need to know the 'size' of the array,
// you can use pic.Count f.e.
End If
End If
Next
The line If file.Name.Right(4) = ".png" Then adds only files ending with .png to the array. You can add more file types with lines like this If file.Name.Right(4) = ".ini" Or file.Name.Right(5) = ".jpeg" Then.
There are smarter solutions for this, but i think for a beginner, this is the way to start.
Var f As FolderItem
Var pic() As Picture '<<<<<<<<<<<<<<<<<<<<<<<<
Var dlg As New SelectFolderDialog
dlg.ActionButtonCaption = "Select"
dlg.Title = "Title Property"
dlg.PromptText = "Prompt Text"
dlg.InitialFolder = SpecialFolder.Documents
f = dlg.ShowModal
If f = Nil Or Not f.Exists Then Return
Label1.Text=f.shellpath
For Each file As FolderItem In f.Children
If Not file.IsFolder Then
If file.Name.Right(4) = ".png" Then
Try '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pic.add(Picture.Open(file)) '<<<<<<<< Mind the 'file' instead of just 'f' here!
Catch e As IOException '<<<<<<<<<<<<<<<
' unable to open picture '<<<<<<<<<<<
End Try '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
End If
End If
Next
I’ve marked the changes with '<<<<'s
Hope it helps. I have to leave for now.
Place one at the start, and when it reaches that, you can step through. You can add/remoce reakpoints during that process. Or add a ‘break’ statement right at the end (no breakpoints), and then it will run and drop into the debugger at the break statement. Then you can examine variables to verify they have the expected values.