Populate popupmenu with folder contents

Hi,
I am trying to populate a popupmenu with the contents of a folder (which will be files only, no subfolders).

I also need the user to be able to double click on the items in the list, and have them open in the default application.
I have been away from Xojo for so long, I have forgotten almost everything :frowning:

This is for a Windows 10 app.

Does anyone know of any examples of this, which I can then study in order to re-learn from.

All I have so far is:

[code]Dim f as folderitem
f = GetFolderItem(“C:\Users\Richard\Desktop\Miscellaneous”)

For i As Integer = 0 To f.Count -1
TestPopupMenu.AddRow(f.Item(i).Name)
Next[/code]

And even this doesn’t populate the popupmenu.

Struggling big time - cant believe I have forgotten everything :frowning:

Thank you all in advance.

FolderItem.Count and FolderItem.Item are 1-based, so that could be causing you an issue with a directory with one item in it.

Try this:

dim fTarget as FolderItem = SelectFolder

// user cancelled
if fTarget = nil then return

// FolderItem.Count is very intense and should not be used in a for loop.
dim iCount as Integer = fTarget.Count

for i as Integer = 1 to iCount
  dim fThis as FolderItem = fTarget.Item(i)

  // Folderitem error, let's not get a NOE
  if fThis = nil then continue

  TestPopupMenu.AddRow(fThis.DisplayName)

next

{Code written in post editor disclaimer}

Also, in the time that you’ve been gone the way you access files on disk has gotten stricter. Now is the best time to start pretending GetFolderItem doesn’t exist if you’re learning again!

If you want to avoid potential Alias troubles(Link files?), use .TrueChild (instead of Child).

You may also want to exclude to display an invisible file, you can check for the file name first character if it is a dot (.) and don’t display it.

Check the “\Example Projects\Files\FileBrowser.xojo_binary_project example” ?

Did I forgot something ?

Thanks Tim and Emile.

Tim, if I need to forget about GetFolderItem as you recommended, does this mean it is becoming deprecated? as it is currently still in the language reference?

Also, how do I avoid making the usser select a folder, and instead hardcode a specific folder?
Basically, how do I hardcode the following path, without using GetFolderItem?

f = GetFolderItem(“C:\Users\Richard\Desktop\Miscellaneous”)

I cant see anything appropriate in the clasic language reference which would replace it :frowning:

Thanks, and nice to hear from you again :slight_smile:

How and WHY would one do this in a PopUpMenu?

The user wants to be able to select a file from a popup menu, and then click on the file name to open it up.

But the moment the user selected a File from the PopUpMenu the file could be opened, or am i missing something? Just trying to help :slight_smile:

Apologies, probably the way I explained it :slight_smile:
I need the file to open when the user selects the file from the popup menu, not when they double click it. Sorry, been a very long day :slight_smile:

Here is what I have so far:

This code is in a method to populate the popupmenu:

[code]// POPULATE HUMAN RESOURCES POPUP MENU
// DEFINE THE TARGET FOLDER
dim fTarget as FolderItem = GetFolderItem(“C:\Users\Richard\Desktop\Test\HumanResources”)

// PREVENT ERROR IF FOLDER HAS BEEN DELETED
If fTarget <> Nil And fTarget.Exists and fTarget.Directory Then

// COUNT FILES IN FOLDER
dim iCount as Integer = fTarget.Count

// LOOP THROUGH FOLDER ITEMS
for i as Integer = 1 to iCount
dim fThis as FolderItem = fTarget.Item(i)

// ASSIGN FILE PATH TO ROWTAG
RowTag(i) = fThis.GetSaveInfo(GetFolderItem(""))

// PREVENT AN ERROR IF FOLDER CONTAINS NO FILES
if fThis = nil then continue

// POPULATE THE ACTUAL POPUP MENU
HumanResourcesPopupMenu.AddRow(fThis.DisplayName)

next

End[/code]

However, I get an error stating that the item RowTag does not exist, but popultes the popupmenu fine if I remove that line of code.

In the popupmenu’s change event I have the following code which I need to launch the selected item in the list:

[code]// OPEN SELECTED FILE
If Me.ListIndex >= 0 Then
Dim FileToOpen As FolderItem

Dim f As New FolderItem
FileToOpen = f.GetRelative(RowTag(Me.ListIndex))

If FileToOpen <> Nil And FileToOpen.Exists Then
FileToOpen.Launch
End If
End If[/code]

This also states that the item RowTag does not exist :frowning:
Can anyone assist, as my head is now confused.

Thank you all in advance.

Try to add the RowTag after the AddRow Event, because you can’t add a RowTag to a Row before it even exists :slight_smile:

RowTags can be objects, so don’t force a string in there when you really need the FolderItem.

RowTag(i) = fThis

FileToOpen = ThePopupMenu.RowTag(ThePopupMenu.ListIndex)

Ok, thanks for the input Tim and Sascha.
All works perfectly now - THANK YOU BOTH!!!

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.