Tutorial 035 Use of File Open Dialogue Box

Its useful to be able to easily access any file (eg a text file) for processing in a BB4W program.

The following short program illustrates this by printing the full "path name" of any file on your computer including the file-type suffix, eg .txt for text file.

In later tutorials we will make use of this technique to conveniently load chosen files of various sorts....but for the moment lets not cloud the issue with unnecessary complications and just print the filename we find, without using it further.

Here is an example of some output :

Double-click any file to see its <pathname>

(You can navigate anywhere on your computer before clicking)

File name found :

C:\Program Files\RedShift4\RedShift4.exe

Press <SPACE> to do another

The technique is described in Richard Russell's BB4W Helpfile under :

<E> Accessing the Windows API, <30> Using Dialogue Boxes, <1> File Open and File Save

The tricky bit is to make sure that in the DIM statement the ff% parameter is big enough, here 50 is plenty.

Listing :

      REM:File Opening
      REM:Richard Weston 30 June 2003
      MODE8
      :
      DIM of% 75, ff% 50, fn% 255
      !of% = 76
      of%!4 = @hwnd%
      of%!12 = ff%
      of%!28 = fn%
      of%!32 = 256
      of%!52 = 6
      $ff% = "All File Types"+CHR$0+"*.*"+CHR$0+CHR$0
      :
      PRINT"Double-click any file to see its <pathname>"
      COLOUR1
      PRINT'"(You can navigate anywhere on your computer before clicking)"
      :
      SYS "GetOpenFileName", of% TO result%
      IF result% THEN filename$ = FNnulterm$(fn%)
      :
      COLOUR15
      PRINT'"File name found :"
      PRINT'filename$
      COLOUR2
      PRINT'"Press <SPACE> to do another"
      G=GET
      RUN
      END
      :
      DEF FNnulterm$(P%)
      LOCAL A$
      WHILE ?P% <> 0
        A$ += CHR$?P%
        P% += 1
      ENDWHILE
      = A$


Annotated Listing :

      REM:File Opening
      REM:Richard Weston 30 June 2003
      MODE8
      :
      DIM of% 75, ff% 50, fn% 255 *** Just make sure ff% value is adequate - see RTR's HelpFile!
      !of% = 76   *** Don't worry about the next 7 lines!
      of%!4 = @hwnd%
      of%!12 = ff%
      of%!28 = fn%
      of%!32 = 256
      of%!52 = 6
      $ff% = "All File Types"+CHR$0+"*.*"+CHR$0+CHR$0 *** We'll be modifying this line in later programs....
      :
      PRINT"Double-click any file to see its <pathname>"
      COLOUR1
      PRINT'"(You can navigate anywhere on your computer before clicking)"
      :
      SYS "GetOpenFileName", of% TO result% *** This line brings up the Dialogue Box
      IF result% THEN filename$ = FNnulterm$(fn%) *** This function is described by RTR in the Helfile - just use it

*** All the above scary stuff is just to get hold of the chosen file's full pathname, here captured as filename$ ***
      :
      COLOUR15
      PRINT'"File name found :"
      PRINT'filename$
      COLOUR2
      PRINT'"Press <SPACE> to do another"
      G=GET
      RUN
      END
      :
      DEF FNnulterm$(P%) *** Don't worry about how  this defined function works- just include it for now.
*** Its reading the contents of  a memory location ***
*** Must be placed after the END statement like other PROC and FN DEFinitions ***

      LOCAL A$
      WHILE ?P% <> 0
        A$ += CHR$?P%
        P% += 1
      ENDWHILE
      = A$ *** This is the value which the function takes (in this case its a string corresponding to the pathname).


Next Tutorial

Richard Weston's Homepage