Take Away: Use setWorkingDir then trap for MenuChangingWork in menuAction.
KB101121
Suppose that you have code that relies on objects that are in the same directory as the form, and you want to automatically change your working directory to that directory when the form is opened. This section shows you how to accomplish this.
Demo Files: WORK1.FSL.
Step By Step
1. Change your working directory to Paradox's Samples directory and create a new form.
2. In the init method of the form, type lines 3-14. Line 4 declares f as a Form variable and dyn1 as a DynArray ready to accept string values. Line 8 is rather interesting. It uses attach() to attach f to the current form. When you attach to the current form, you do not have to include its title. Line 9 checks to see whether the current working directory is the same directory the form is in. If it is not, then line 10 uses getFilename() and splitFullFileName() to extract the form's path. Line 11 sets the working directory.
1: ;Form :: init 2: method init(var eventInfo Event) 3: var 4: f Form 5: dynDir DynArray[] String 6: endVar
7: ;Set working directory to this directory. 8: f.attach() 9: if not isFile(":WORK:ANSWERS.FSL") then 10: splitFullFileName(f.getFileName(), dynDir) 11: if not setWorkingDir(dynDir["Drive"] + dynDir["Path"]) then 12: errorShow() 13: endIf 14: endIf 15: endMethod
3. In the menuAction event of the form, type lines 8-10. Similar to the previous example, in step 3, line 9 checks for the constant MenuChangingWork. If it is detected, line 10 sets an error code to a nonzero value.
1: ;Form :: menuAction 2: method menuAction(var eventInfo MenuEvent) 3: if eventInfo.isPreFilter() then 4: ;// This code executes for each object on the form: 5: 6: else 7: ;// This code executes only for the form: 8: if eventInfo.id() = MenuChangingWork then 9: eventInfo.setErrorCode(1) 10: endIf 11: endIf 12: endMethod
4. Check the syntax and save the form as WORK1.FSL. Change your working directory to a different directory (such as C:\) and open the form. After the form opens, check the current working directory by selecting File | Working Directory.