List all files in a folder in a List Box

For questions about using Classic.

Post » Fri Sep 17, 2010 1:45 pm

I'm currently working on a level editor for a game, which is pretty much done. It stores all objects into an array then saves the array out to a file. The loading is also working fine, however the user has to type the name of the level into a box to be able to open it, which is just annoying. So i was wondering if there's a way to get all files in a folder to be listed in a List Box? This way the user can select their level from a list.

All the level files are stored in AppPath/maps

Thanks
B
7
S
2
G
4
Posts: 164
Reputation: 2,418

Post » Fri Sep 17, 2010 3:56 pm

The "File" objects would work well for this. It has an event to loop through each file in a directory, then there's a condition to get the name each file as it loops through. Let me know if you need anymore help. :)
Moderator
B
99
S
37
G
19
Posts: 1,584
Reputation: 17,842

Post » Fri Sep 17, 2010 9:20 pm

For a very simple list of files, I have used this:

[code:2bxjb9by]-> ListBox: Add line File.FileList("F:\Downloads")[/code:2bxjb9by]

Just drop in a listbox and file object, and add that action somewhere. That path is just my download directory, which has hundreds of files. It seems to handle separating each file into a new line just fine.

You can handle selections or double-clicks easily with something like this (which only copies the filename to a text object):

[code:2bxjb9by]+ ListBox: Selection changed
-> Text: Set text to ListBox.LineText(ListBox.SelectedLine)

or...

+ ListBox: Double clicked
-> Text: Set text to ListBox.LineText(ListBox.SelectedLine)
[/code:2bxjb9by]
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Sat Sep 18, 2010 12:13 am

Thanks to both of you! Its working perfectly :D
B
7
S
2
G
4
Posts: 164
Reputation: 2,418

Post » Sat Sep 18, 2010 3:56 pm

how would you go about displaying only a certain type of file?

like a particular file extension, is that possible?
B
15
S
2
G
3
Posts: 50
Reputation: 1,863

Post » Sat Sep 18, 2010 10:47 pm

[quote="L5j":tghtci61]how would you go about displaying only a certain type of file?

like a particular file extension, is that possible?[/quote:tghtci61]

[code:tghtci61]+ Your condition...
-> ListBox: Add line File.FileListFiltered("F:\Downloads", "*.cap")[/code:tghtci61]

This shows me all of the .cap files in my download folder. Check out this wiki page for more that the file object can do:

http://sourceforge.net/apps/mediawiki/c ... ile_Object

It does have the elements necessary for more complex file and/or directory listing. Also, the Common Dialog Object has some uses in this area, if you don't mind a windows-style requester, though it's not quite as flexible as the file object.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 6 guests