FILEDIALOG

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Not supported in Run BASIC

Description

Opens a Windows Common File-dialog. There are two dialog styles: OPEN a file and SAVE a file. The style that is used depends on whether the word "OPEN" or "SAVE" is included in the titleString parameter.

Syntax

  1. filedialog titleString, templateString, receiverVar$

Parameters

titleString-- Used to label the dialog window. If the title-bar has the word "save" in it, then the SAVE type dialog window is used, and the approval button will say "Save". If "open" is used in the title instead of "save", then the dialog will be an OPEN file type with a button which says "Open". Examples:
Open
Save
Save As...
templateString-- Uses standard Windows "*" wild-card character to filter the types of files that are displayed. Multiple file types can be displayed if separated by a semicolon. An empty string ("") will display all file types. Examples:
"*.*" ' displays all file types and names
"*.bas" ' displays all files of type "bas"
"*.txt;*.dat" ' displays all "text" and "dat" files
templateString can be prefixed with a folder name to allow returning to the same folder to open another file in the previous folder.
receiverVar$ -- In an Open file dialog, the name and full path of the selected file is placed in receiverVar$. If Cancel is pushed, this variable will contain an empty string. This parameter must be a string variable. A string literal will cause a compile error.

Hints

After selecting a file and pushing "Open" button, program execution will continue and the filename will be in the receiverVar$ variable. If instead, "Cancel" is clicked, the variable will be empty which could cause an error when the program continues. Be sure to trap this possibility in the program to avoid the error.
You can hide the "save" or "open" words by putting them in the title after chr$(0) like this:
filedialog "Please select a file"+chr$(0)+"open","*", ret$
Windows displays only the part of title before chr$(0).
If you start the title with chr$(0), so according to our previous hint, no title should be displayed at all, localized Windows will show the standard title (Open or Save As) in local language (eg. in Russian).
There is a way to preset file name AND use template string (checked on XP):
filedialog "Open","calc2.bas";chr$(0);"*.bas;*.c",receiverVar$ 
1) fills "file name" field with name I supply (here, "calc2.bas")
2) it hides all other files in file pane
3) it allow to choose another filter, like *.bas or *.c

Example

Open

'Open any file
    filedialog "Open", "*.*", fileName$

Save

'Save a text file
    fileName$ = "good.txt"
    filedialog "Save my file as...", "*.txt", fileName$

Cancel

' Trapping an empty variable on Cancel
    filedialog "Open text file", "*.txt", fileName$
    if fileName$ = "" then notice "No file selected"
    end

Useful Procedures

'* If curPath$ = "" then JustBASIC uses DefaultDir$ *

FUNCTION OpenPath$(BYREF curPath$, fileMask$)
  FILEDIALOG "Open a File", curPath$; Mask$, Rtn$
  if Rtn$ > "" then
    DO
      op = p + 1
      p = instr(Rtn$, "\", op)
    LOOP until p = 0
    curPath$ = left$(Rtn$, op - 1)
  end if
  OpenPath$ = Rtn$
END FUNCTION