Popup menu from filenames?

Hi everyone, I am trying to populate a popup menu from a list of filenames.
Can anyone point me in the right direction, as I haven’t used Xojo for a few years.

Here is what I have so far:

Inserted into the window open event:

// POPULATE THE POPUP MENU

// DEFINE THE TARGET FOLDER
dim fTarget as FolderItem = GetFolderItem(“Applications/File Repository/Business/Xojo Extras/Xojo Scripts”)

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

I also have this in the popup change event:

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

Using the latest Xojo on an M1 MacBook Pro.

Any help appreciated.

Thank you all in advance.

Have a look at Folder Contents in a Menu – Xojo Programming Blog

Thanks Wayne - I just tried that but Im still having trouble with the pathname.
Instead of SpecialFolder.Documents, I need my pathname as listed above, but it fails to compile.

I think this needs to be:

Thank you to both of you - I now have the blog entry version working.
Much appreciated, both of you!

Wayne - I just realised, I still need to launch whichever file is clicked on in the drop down list.
The blog entry doesn’t explain this. Any pointers on how to do that?

Thanks.

Hi Richard,

You would have something like this in the change event handler.

Sub Change() Handles Change
  If me.RowTagAt(me.SelectedRowIndex) <> Nil Then
    Var f As FolderItem = me.RowTagAt(me.SelectedRowIndex)
    f.Open
  End If
End Sub

There is no control to add your change event handler to?? Only 1 button, and there is no option for Change?

I’m referring to the blog entry you pointed me to earlier.
Confused :frowning:

In that case you’d add a method.

Public Function Pressed(Sender As MenuItem) As Boolean
  If Sender.Tag <> Nil Then
    Var f As FolderItem = Sender.Tag
    f.Open
  End If
End Function

And add

AddHandler FolderMenu.Action, AddressOf Pressed

to the last line of the pushbutton1 Action Event Handler.

OK, that is now throwing errors, so I have started from scratch.
I now have a brand new project. I only have 1 control (PopupMenu1).

In the window’s Open event, I have this code:

// POPULATE POPUPMENU1

// DEFINE THE TARGET FOLDER
dim fTarget as FolderItem = SpecialFolder.Desktop

// 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)
    
    // PREVENT AN ERROR IF FOLDER CONTAINS NO FILES
    if fThis = nil then continue
    
    // ASSIGN FILE PATH TO ROWTAG
    //THIS LINE THROWS AN OUT OF BOUNDS ERROR ???????
    MainWindow.PopupMenu1.RowTag(i) = fThis
    
    // POPULATE THE ACTUAL POPUP MENU
    MainWindow.PopupMenu1.AddRow(fThis.DisplayName)
    
  next
  
End

In the PopupMenu1 change event, I have this code:

// OPEN SELECTED FILE
If Me.ListIndex >=0 Then
  
  If me.RowTagAt(me.SelectedRowIndex) <> Nil Then
    Var f As FolderItem = me.RowTagAt(me.SelectedRowIndex)
    f.Open
  End If
  
End If

When I run the code I get an out of bounds exception at this line:

 // ASSIGN FILE PATH TO ROWTAG
    //THIS LINE THROWS AN OUT OF BOUNDS ERROR ???????
    MainWindow.PopupMenu1.RowTag(i) = fThis

If I comment that line out - the popup menu populates correctly, but nothing happens when I click on a file from the list :frowning:

Can anyone assist. I simply need a popup menu to be populated with files in a folder, and then have the selected file open in its default app.

Thank you.

Should be

// COUNT FILES IN FOLDER
  dim iCount as Integer = fTarget.Count
  
  // LOOP THROUGH FOLDER ITEMS
  for i as Integer = 0 to (iCount-1)
    dim fThis as FolderItem = fTarget.Item(i)

I’d also not be inclined to keep Dim-ming variables inside a loop, that’s extra overhead. Personally I declare all variables at the top of the method, then I don’t later have to go hunting around for their definition.

Actually it’s also better to redo the loop as:


lim =  fTarget.Count - 1

for i = lim downto 0

so you’re not recalculating the loop’s limits at each iteration.

What is the value of i when you get the OOB exception? Also, when you assign to the rowtag, that row doesn’t exist yet.

Thanks Tim - I placed the RowTag line AFTER the population line, and it all now works as expected :slight_smile:

Thanks for the help.