OpenDialog for specific files

I’ve been reading through the documentation and cannot seem to find the answer to my question, which is “how do i launch an OpenFileDialog with only specific files being shown”? Our business uses a CaseNumber system and each Case has multiple PDF documents associated with the CaseNumber.

I want to launch an OpenFileDialog box and see only those files for a particular case (e.g., 1890_wks.pdf / 1890_inst.pdf / 1890_coc.pdf etc).

I can launch the OpenFileDialog and see all the files in the specified directory/folder, but I cannot seem to figure out how to limit the files being displayed to only those files/documents for a particular CaseNumber.

The code I’ve been using that gets me a list of all files in the directory:

Var opendialog As New OpenFileDialog
Var fFolder As FolderItem
fFolder = New FolderItem(app.strCaseNum + “.*”)
fFolder = opendialog.ShowModal

Any suggestions would be greatly appreciated.

Do you already have a filetype? Then this code should be enough:

dim theDialog as new OpenFileDialog
theDialog.PromptText = kSelectView
theDialog.Filter = FileTypes.DB
dim theFolderitem as FolderItem = theDialog.showModal

maybe you could select a path and then list the allowed files only into a listview.

see SelectFolderDialog

As Beatrix suggests, define a file type to act as the filter. However, I have had issues with global FileTypes defined in the navigator. I have the most success when defining it in code immediately before using it.

var ft as new FileType
ft.Name = "PDF"
ft.Extensions = ".pdf"

var od as new OpenFileDialog
od.Filter = ft

var fSelect as FolderItem = od.ShowModal
if fSelect = nil then return // User cancelled

uses a CaseNumber

somehow he will see 1890_*.pdf

Oh that requires declares or plugins.

Pinging @Christian_Schmitz I’m quite sure MBS can do this (at least for macOS), do you have an example handy?

I noticed an error in my code - specifically, the fFolder = NewFolderItem line should read as follows:

fFolder = NewFolderItem(app.strCaseNum + “_.*.pdf”) because each saved file has the following format: CaseNumber_docType.pdf (e.g., 1807_wks.pdf / 1807_coc.pdf)

Again, I screwed-up, the line should read:

fFolder = NewFolderItem(app.strCaseNum + “_*.pdf”)

fFolder = New FolderItem(app.strCaseNum + “_*.pdf”)
fFolder = opendialog.ShowModal

that make no sense because the first line is unused.
and the constructor get a path, not a file with joker.

1 Like

One assumes you mean:

fFolder = New FolderItem(app.strCaseNum + “_*.pdf”)

Note: when posting code, please:

  1. enter the code in your post
  2. select it all with the mouse
  3. click the </> button.

Sure, you can check OpenDialogMBS class for cross platform file dialog with lots of options.

Or MacOS specific with NSOpenPanelMBS class and WinFileDialogMBS for Windows.

OK, now that we have my code errors straightened out, I still am not clear about how (or if) I can accomplish what I’m looking for. When my app.strCaseNum variable = ‘1807’ I want to get the OpenFileDialog box to display something like:

1807_wks.pdf
1807_coc.pdf
1807_inst.pdf

I’m trying to achieve the same listing as “ls 1807_*.pdf” would be from the command line.
Is this possible in XOJO ??

No, you have to filter out non-relevant results using declares or plugins. The best Xojo can do out of the box is filter to “all PDFs”, but not “only things matching this name pattern”.

1 Like

Tim, thanks for the concise response. I will look into the plugin and declaration options.

Tim, I assume that if I changed my directory/folder structure so that each Case had it’s own directory/folder that I could point a FolderItem at that case-specific directory and simply list its complete contents (which would be case-specific).

Am I correct in this assumption?

Yes, that would work. I think it’s a great simple solution.

if you have a base directory maybe you can save the path in a configuration.
from this path list all files with the help of FolderItem and memory it into a array.
if you change the case number use a DesktopListBox to list all files with this number.
you could store the original FolderItem object in RowTag.

collecting all related files into a Case folder is also good but if you need to move the files they may have same name.

This isn’t cross-platform, but on macOS I’m using Greg O’s SOS-MacOSSaveOpenDialog project.

In this repo there is an event called NSSavePanel_ShouldEnableItem that can be used by both the Open & Save dialogs. I did a little experiment to see if I could filter a list of files by checking if the file name contains a specific string value (text or numbers) and a specific extension (*json in this case). It appears to work.

Private Function NSSavePanel_ShouldEnableItem(obj as NSSavePanelGTO, f as FolderItem) As Boolean
  // In an Open Panel, this event fires for every item in the panel
  // In a Save Panel, this event fires only for directories
  // It does not appear that you can enable items that are disabled by the filter
		  
  // you must return true for the passed item to be enabled
  If f.Directory And Not f.Visible Then
    Return False

  ElseIf f.Name.Contains("03") And f.Extension = "json" Then
    // custom filtering based name containing specific string value
    // and specific extension
    Return True
  End If
		  
  Return False // to not enable any other kind of file
End Function

If you only need macOS, this could work for you. But if you need Windows too, then I think MBS will be the way to go, or a Windows Declares equivalent.