populate list box with directory

I need to populate a list box with the contents of a directory. Then the user will click on one of the entries to select that file to process.

Also, I would like the user to be able to change the ‘default’ directory that is listed by clicking on the directory in a file requester box.

I believe that I should be using the folderitem category of commands / properties, but have not been able to deduce how. I have looked at the examples that come with xojo, but they only create an open dialog example, not not what I am trying to accomplish…

Any suggestions?



Have a look at this project I just created which, I believe, does essentially what you are looking for and can get you started.


The first example in the Language Reference for ListBox shows you how to put the names of the files on the Desktop into a ListBox.

Examples/Files/FileBrowser has a more complete example that let you navigate the entire folder hierarchy.

Thanks Roger / Paul… Roger, that is what i was looking for… Is there a way to save the directory that is selected? I have a database that stores the directory so that it does not need to be ‘changed too’ each time the program is run, and i want to read the database’s value to set that as the directory.

And one more question… Is there also a way to limit the files shone to a particular file type extension? I was looking at some examples that set file types, but have to find it again to see if it would work with your example.



I highly recommend that you read Chapter 1: Files, in the UserGuide-Framework.pdf that comes with Xojo. A quick look at it and I see that it answers all of your questions.

Well, I did 'RTFM" before I posted Emile, I just did not follow it very well… I mean, I can get to the directory, I can see the files, and I can select the files, I just did not ascertain how to 'save the directory as a default ’ directory so that the program started in that directory.

I do see how to 'get ’ the directory… my question involves only how to save and retrieve the file path between program runs so that I don’t have to drill down to the directory each time I run the program.

I will re-read the manual again…

You save the info to a plist file (preferences file) when you close the app, and load it when you start the app.

Harrie’s suggestion is probably correct but I haven’t read the docs so don’t know how confusing it is. I’ll refrain from commenting on Emile’s contribution.

As Markus said, you can write to a plist or even just a plain text file. Either way you’d probably want to store a folderitem reference. The following is ‘memory code’ - typed from memory here rather than in Xojo :slight_smile: It would need error trapping for Nil objects. I’ve left out the actual reading and writing code.

dim mydir as folderitem
dim s as string

mydir = SpecialFolder.Desktop.Child(“Some folder”)
s = EncodeBase64(mydir.GetSaveInfo(nil))

’ then save the string ‘s’ into a file of your choice (I’d use application support/your_name/myprefs.txt or similar)

To get it back, decode and read back the saved string

dim f as folderitem
dim s as string

'open and read from your prefs file then

f = GetFolderitem(DecodeBase64(s))

Alternatively you could even save the absolute path of you folder as a plain text string but that would generally be regarded as bad practise (ask anyone here :slight_smile: )

Once you have the folderitem you can assign it to a property and do what you wish with it. To display it just use the absolute path and assign it to a text field.


from the documentation:
This one have a default directory that let you set the… default directory.

Paul already talked about the examples.

At last, Gary can save the FolderItem of each object into ListBox’ RowTag for future use…

I don’t doubt the information’s there, but given the recent desire for a ‘friendly’ forum your comment could easily cause unnecessary offence. Why not just say what Harrie said? The message is the same but without the attitude. Alternatively, say nothing.

Ok, looking at the examples again, I remember why I did not find my solution, looking at this example:

Dim dlg as OpenDialog
Dim f as FolderItem
dlg=New OpenDialog
#If Not (TargetLinux) then
#Else //open Home directory on linux
dlg.Title=“Select a MIF file”
dlg.Filter=FileTypes1.mif //mif file type defined in FileTypes1 set
If f <> Nil then
//proceed normally
//User Cancelled
End if

My questions are about the initial directory (dlg.InitialDirerectory = ) in this and other examples it always is equal to a default directory, such as a home directory. I tried to put a file path in there, " //files/restored/" but get an error that I am putting in a string instead of a folder item…

So, my question, I guess, is how do I set the initial directory to a file path of my choosing that is NOT a special “home” directory?

I see the dlg.InitialDirectory.Directory ---- directory option (as shown in the example copied above), but did not find the documentation on how to use this option…

Ok, please ignore the last post (I could not figure out how to delete)… thank you Kim, i was able to use your example to work out what I wanted to accomplish.


as far as I understand your last-1 message, the answer is: you do not passed a valid FolderItem. .InitialDirectory is not limited (to a special location FolderItem); you can pass any valid FolderItem.


In the examples, Xojo lead us to use special folders probably beacause people have the tendency to save data anywhere in THEIR hard disk. So, passing SpecialFolder.Documents is the base of where you have to save user data.


Ok, not to be dense, but I have looked at the examples, and played with some of the commands, and still have not achieved the right solution.

Below is my code (commented out code was from prior attempts, and the FileType is not active yet… I keep seeing referenced in the examples, and I do know I found in the docs once, but have not found it again or figured out how to use them yet…). It takes me to the default directory, and I can select it (still can’t get it to select automatically), but it will not populate the list box.

In the bold section below, I have used dim u As Integer = dlg.InitialDirectory.Count to determine the number of files in the directory, and it returns an integer (the MsgBox line confirms this), but line [ if not dlg.item(i).Directory Then] gives me an out of bounds error… however, when this part was used in a global method (another attempt, another example tried) this par worked? Why does it not work in this instance?

Dim TransType As New FileType
TransType.Name = “text/trn”
TransType.MacType = “TRN”
TransType.MacCreator = “trn”
TransType.Extensions = “trn”

'selectedDir = SelectFolder

'SpecialFolder.Desktop.Child(“Some folder”)
Dim dlg as OpenDialog
Dim f, datadir as FolderItem
'datadir="//files/restored/ameritrade data"
dlg=New OpenDialog
dlg.ActionButtonCaption=“Action Button”
dlg.CancelButtonCaption=“Cancel Button”
dlg.Title=“Title bar text”
dlg.PromptText=“Prompt Text”

'selectedDir= New OpenDialog
#If Not (TargetLinux) then
#Else //open Home directory on linux
'dlg.InitialDirectory= datadir
'f =SpecialFolder.Home.Child(“Documents/Ameritrade”)
'dlg.InitialDirectory="//files/restored/ameritrade data/"
dlg.Title=“Select a file”
'dlg.Filter=FileType.TransType //mif file type defined in FileTypes1 set

[b]]If f <> Nil then
dim u As Integer = dlg.InitialDirectory.Count
MsgBox str(u)

for i As Integer = 1 to u
if not dlg.item(i).Directory Then
filFiles.AddRow dlg.Item(i).DisplayName
end if[/b]

'SelectedDir= f.PathTypeURL
'selectedDir = f.NativePath
//proceed normally
//User Cancelled
End if

You should probably be using SelectFolderDialog instead of OpenDialog, if your goal is to select a folder whose contents you want to display in a listbox.

That said, in your example, do not use dlg.InitialDirectory.Count or dlg.Item(). You should use “f”. That is the folderitem that the user selected. InitialDirectory may well be wrong because the user navigated to some other folder. “f” will reflect that, InitialDirectory will not.

OpenDialog lets the user select a file, not a folder. If you want the folder that the file resides in, use f.Parent.

I will not comment on the rest of the code.

Ok (I know the code is messy, I have been experimenting with many different methods)…

When I change the dlg as OpenDialog to Dim f As SelectFolderDialog then change f.intialDirectory=special… gives me an error that the item does not exist. if I put f in again as dimmed as a folderitem, then I get other errors…

I think you misunderstood.

dim dlg as SelectFolderDialog
dim f as folderitem
dlg = new SelectFolderDialog
dlg.InitialDirectory = SpecialFolder.Documents
f = dlg.ShowModal
dim u as integer = f.Count
for i as integer = 1 to u
  if not f.item(i).Directory then
     filFiles.AddRow f.item(i).DisplayName

Note that I am using “f”, which is returned from the dialog, not the dialog itself. Eg., f.Count, not dlg.InitialDirectory.Count, and f.item() instead of dlg.item().

Ok, I made the changes that you noted above (I did misunderstand… having a hard time with the folderitem concept… vb was simplier in this regard to change directories, paths, and retrieve files).

however, I now get a nilobject exception error for the line: dlg.initialdirectory = specialfolder.Documents (yes, that folder exists, and has files in it).

Sure dlg is <> Nil ?

dlg = new SelectFolderDialog
dlg.InitialDirectory = SpecialFolder.Documents