Populating PopUpMenu with Folder Contents with Hierarchy

Hello Everyone,

Complete noobie here, so please forgive my ignorance.

I’m looking to populate a PopUpMenu with the contents of a folder.
I’d like to list the files in that folder, but also have a hierarchy showing the folders and the files inside those folders.

I’ve looked at the example file named “FileBrowser” which other than specifying which folder to show, pretty much behaves as I’d like. The problem is that it is using ListBox as opposed to PopUpMenu. The latter does not have the same event handlers and therefore cannot be duplicated in it.

I found this Thread, and I would have posted the question there, but it is unfortunately closed:
https://forum.xojo.com/t/populate-popupmenu-with-folder-contents/45390/13

There I found code by @Tim_Parnell which I’ve successfully used to populate my PopUpMenu, but it doesn’t work with Hierarchy:

dim fTarget as FolderItem = GetFolderItem("..")

// 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
  
  PD_ExistingConfig.AddRow(fThis.DisplayName)
  
next

But at the end, it seems the original poster @Richard_Summers was able to make it work as I want it (I believe). Unfortunately he did not post his final code. I tried to duplicate it but kept getting bugs.

I didn’t understand the comment by @Sascha_S “Try to add the RowTag after the AddRow Event, because you can’t add a RowTag to a Row before it even exists”
And perhaps that is what wasn’t working.

Any help would be greatly appreciated it.

Thank you much and stay safe. :slight_smile:

I am unsure of the real question, so…

a. How many entries do you plan to add in a PopupMenu ?

b. Do you want to set hierarchy in a PopupMenu ?
(Row and Sub-Rows ?)

Hi Emile,

Thanks for your reply. I’m sorry if I wasn’t clear. I would like the PopupMenu to display a File Explorer list with Rows and Sub-Rows.

Something that looks like this:

And you know PopupMenu can do that ?

You want to do that with what Xojo version ?

I don’t think a popup menu can do it, but it can be done with an ordinary menu or with a contextual menu. Something like a FE or Finder list can be generated using recursion.

But @Luis_Sinibaldi what you show in your image looks more like a listbox.

That thread is not about hierachy, but flat PopupMenu.

@TimStreater what is a FE ?

Yes, what I show was the Example file named “FileExplorer”.

If a Popup Menu is not capable of doing this, but an ordinary menu can, how do I set that up?

What I’m trying to create is something that looks like a Popup Menu than when clicked, can look like the contents of a Listbox that is showing a file explorer. Then when an item gets double clicked, the Listbox turns back into the Popup Menu image, and the file get’s loaded.

You can do hierarchical menus, I think.
But not with built-in Xojo Popupmenu.
You would need to build a custom menu with MenuItems (or on macOS via plugin with NSMenuItemMBS) and then pop that as context menu in right position.

The PopupMenu control cannot do this. Here’s how you can recurse through files and add them to a regular MenuItem:

Private Function CreateFolderMenu(f As FolderItem) as MenuItem
  Var base As New MenuItem(f.Name, f)
  For Each child As FolderItem In f.Children
    If child.IsFolder Then
      base.AddMenu(CreateFolderMenu(child))
    Else
      Var m As New MenuItem(child.Name, child)
      base.AddMenu(m)
    End If
  Next
  
  Return base
End Function

You can call it like this:

Var startFolder As FolderItem = SpecialFolder.Documents
FolderMenu = CreateFolderMenu(startFolder) // Property FolderMenu As MenuItem

And show the menu like this:

Var selectedFile As MenuItem = FolderMenu.Popup

If selectedFile <> Nil Then
  Label1.Text = FolderItem(selectedFile.Tag).NativePath
End If

Sample project download

@Luis_Sinibaldi
You may want to read the codument (lnk below) from the Microsoft servers that list and explain the different User Interface objects. Then, it will be easier to choose the one you need from (if this exists in Xojo) or buy it from a third party:

Thank you. I will take a look at this. I really appreciate all your time and efforts.

Thank you so much Emile. I will read through that also. Thanks for your help. Much appreciated.

"Luis_Sinibaldi I will try to dig out some old code.

Try this:

// 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
    
    // PREVENT AN ERROR IF FOLDER CONTAINS NO FILES
    if fThis = nil then continue
    
    // POPULATE THE ACTUAL POPUP MENU
    HumanResourcesPopupMenu.AddRow(fThis.DisplayName)
  next
  
End

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

// OPEN SELECTED FILE
If Me.ListIndex >= 0 Then
  Dim FileToOpen As FolderItem
  
  Dim f As New FolderItem
  FileToOpen = ThePopupMenu.RowTag(ThePopupMenu.ListIndex)
  
  If FileToOpen <> Nil And FileToOpen.Exists Then
    FileToOpen.Launch
  End If
End If

Hope this helps

from screenshot i would say use a listview in extra window. listview can show a hirachie.

listview:
Did you meant a ListBox ?

If so, the op stated earlier:

The problem is that it is using ListBox as opposed to PopUpMenu.

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