Problem: Method for user to select a file
There are 1800 files from which the user can choice (all in the same directory)
Could I use a ListBox to list all the files?
Or is 1800 files to many to be listed in a ListBox?
How would I programmatically load the ListBox. The files in the
directory could change from time to time so the names can't be hard coded.
If listBox is not the right approach what would you suggest?
1800 file names can easily be handled in a ListBox.
Why not use FF_OpenFileDialog or Jose's AfxOpenFileDialog to select the file. Using one of those functions takes care of all of the hard work for you.
Paul, Thanks for the suggestion... I had forgotten about those FF functions.
Paul,
I am a bit confused by the syntax. Could you explain it?
Let's say I have a bunch of files in a directory called "C:\MyDir"
and I only want to display "jpg" image files.
And I want the user to be able to select one file.
Is the file name selected returned in the nResult ?
How do I know what file was selected?
What should the other variables be?
nResult = FF_OpenFileDialog( hWndForm, sCaptionName, sFileSpec, sInitDir, sFilter, sDefExtetion, nFlags, nCenterFlag)
what would
sCaptionName = "Lexi Images"
sFileSpec =
sInitDir = "C:\MyDir"
sFilter =
sDefExtention = "jpg"
nFlags =
nCenterFlag =
This is working fine for me:
Display OpenFile HWNDFORM,,,"Agregar Documentos en Imágenes", "C:\", "*.BMP;*.JPG;*.TIF;*.PNG;*.GIF;*.EMF"+Chr$(0)+"*.BMP;*.JPG;*.TIF;*.PNG;*.GIF;*.EMF"+Chr$(0),"","",%OFN_READONLY Or _
%ofn_allowmultiselect+ &H10000000 _
Or %ofn_filemustexist Or _
%ofn_nodereferencelinks Or _
%ofn_pathmustexist Or _
%ofn_shareaware Or _
%OFN_ENABLESIZING Or _
%OFN_CREATEPROMPT Or _
%ofn_explorer To ARCHIVOZ$,CUANTOS_FILES&
'For multiple selection of files
For i& = 1 To ParseCount (ARCHIVOZ$,Chr$(0))
arcx$ = arcx$ + Parse$(ARCHIVOZ$,Chr$(0),i&)+$CrLf
Next i&
For ABRE&=1 To CUANTOS_FILES&
ARCHIVOX$=Parse$(arcx$,$CrLf,1)+"\"+Parse$(arcx$,$CrLf,ABRE&+1)
If cuantos_files&=1 Then ARCHIVOX$=ARCHIVOZ$ 'for select only one file
If Len(Dir$(ARCHIVOX$))=0 Then Exit Function
MSGBOX ARCHIVOX$
NEXT ABRE&
And using FF_OPENFILEDIALOG:
Local nResult As Long ' determines whether the OpenFiles dialog was cancelled
Local sFilename As String ' holds the returned list of selected file(s)
Local sFilter As String ' holds the filter list
Local nFlags As Long ' flags that describe the OpenFiles dialog behavior
Local fCount As Long ' number of filenames retrieved
Local sFile As String ' name of file retrieved after parsing sFilename
Local sPath As String ' path for sFile
Local temp As String
sFilter = "File of Image (*.jpg)|*.jpg"
nFlags = %OFN_EXPLORER Or %OFN_ENABLESIZING Or _
%OFN_FILEMUSTEXIST Or %OFN_NODEREFERENCELINKS Or %OFN_HIDEREADONLY
temp = CurDir$
nResult = FF_OpenFileDialog( hWndForm, "Open Image", sFilename, "", sFilter, "jpg", nFlags, %True)
ChDir temp
And using AfxOpenFileDialog...
#COMPILE EXE
#DIM ALL
%UNICODE = 1
' // Include files for external files
#INCLUDE ONCE "CWindow.inc" ' // CWindow class
#INCLUDE ONCE "commdlg.inc" ' // Common dialogs
' ========================================================================================
' SDK open file dialog.
' ========================================================================================
SUB SdkOpenFileDialog (BYVAL hwnd AS DWORD)
LOCAL i AS LONG
LOCAL nCount AS LONG
LOCAL dwStyle AS DWORD
LOCAL bstrInitialDir AS WSTRING
LOCAL bstrFileSpec AS WSTRING
LOCAL bstrDefExtension AS WSTRING
LOCAL bstrFilter AS WSTRING
LOCAL bstrPath AS WSTRING
LOCAL bstrFile AS WSTRING
bstrInitialDir = CURDIR$
bstrFileSpec = "*.BAS;*.INC"
bstrDefExtension = "BAS"
bstrFilter = "PB Code Files (*.BAS)|*.BAS|"
bstrFilter += "PB Include Files (*.INC)|*.INC|"
bstrFilter += "PB Template Files (*.PBTPL)|*.PBTPL|"
bstrFilter += "All Files (*.*)|*.*"
dwStyle = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST OR %OFN_ALLOWMULTISELECT
IF AfxOpenFileDialog(hwnd, "", bstrFileSpec, bstrInitialDir, bstrFilter, bstrDefExtension, dwStyle) THEN
bstrFileSpec = RTRIM$(bstrFileSpec, CHR$(0))
nCount = PARSECOUNT(bstrFileSpec, CHR$(0))
IF nCount = 1 THEN
' // Do whatever you need with the file
MSGBOX bstrFileSpec
ELSE
bstrPath = PARSE$(bstrFileSpec, CHR$(0), 1)
IF RIGHT$(bstrPath, 1) <> "\" THEN bstrPath = bstrPath & "\"
FOR i = 2 TO nCount
bstrFile = PARSE$(bstrFileSpec, CHR$(0), i)
IF LEN(bstrFile) THEN
' // Do whatever you need with the file
MSGBOX bstrPath & bstrFile
END IF
NEXT
END IF
END IF
END SUB
' ========================================================================================
' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG
' // Set process DPI aware
IF AfxGetWindowsVersion => 6 THEN SetProcessDPIAware
' // Create an instance of the class
LOCAL pWindow AS IWindow
pWindow = CLASS "CWindow"
IF ISNOTHING(pWindow) THEN EXIT FUNCTION
' // Create the main window
pWindow.CreateWindow(%NULL, "CWindow with Open File Dialog", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
' // Set the client size
pWindow.SetClientSize 500, 320
' // Center the window
pWindow.CenterWindow
' // Add buttons
pWindow.AddButton(pWindow.hwnd, %IDOK, "&Start", 0, 0, 75, 23)
pWindow.AddButton(pWindow.hwnd, %IDCANCEL, "&Close", 0, 0, 75, 23)
' // Default message pump (you can replace it with your own)
pWindow.DoEvents(nCmdShow)
END FUNCTION
' ========================================================================================
' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
STATIC hInstance AS DWORD ' // Instance handle
STATIC lpc AS CREATESTRUCT PTR ' // Pointer to the creation parameters
STATIC pWindow AS IWindow ' // Reference to the IWindow interface
SELECT CASE uMsg
CASE %WM_CREATE
' // Pointer to the creation parameters
lpc = lParam
' // Instance handle
hInstance = @lpc.hInstance
' // Get a reference to the IWindow interface from the CREATESTRUCT structure
pWindow = CWindow_GetObjectFromCreateStruct(lParam)
EXIT FUNCTION
CASE %WM_COMMAND
SELECT CASE LO(WORD, wParam)
CASE %IDOK
SdkOpenFileDialog(hwnd)
EXIT FUNCTION
CASE %IDCANCEL
' // If the Escape key has been pressed...
IF HI(WORD, wParam) = %BN_CLICKED THEN
' // ... close the application by sending a WM_CLOSE message
SendMessage hwnd, %WM_CLOSE, 0, 0
EXIT FUNCTION
END IF
END SELECT
CASE %WM_SIZE
' // If the window isn't minimized, resize it
IF wParam <> %SIZE_MINIMIZED THEN
' // Resize the sample button
pWindow.MoveWindow GetDlgItem(hwnd, %IDOK), pWindow.ClientWidth - 195, pWindow.ClientHeight - 35, 75, 23, %TRUE
pWindow.MoveWindow GetDlgItem(hwnd, %IDCANCEL), pWindow.ClientWidth - 95, pWindow.ClientHeight - 35, 75, 23, %TRUE
END IF
CASE %WM_DESTROY
' // End the application
PostQuitMessage 0
EXIT FUNCTION
END SELECT
' // Pass unprocessed messages to Windows
FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)
END FUNCTION
' ========================================================================================
And using IAfxFileDialog...
#COMPILE EXE
#DIM ALL
%UNICODE = 1
' // Include files for external files
#INCLUDE ONCE "CWindow.inc" ' // CWindow class
#INCLUDE ONCE "CAfxFileDialog.inc" ' // Open File Dialog class
' ========================================================================================
' Open file dialog (class).
' ========================================================================================
SUB OpenFileDialogClass (BYVAL hwnd AS DWORD)
LOCAL pofd AS IAfxFileDialog
pofd = CLASS "CAfxFileDialog"
IF ISNOTHING(pofd) THEN EXIT SUB
pofd.DefaultFolder = CURDIR$
pofd.FileName = "*.BAS;*.INC"
pofd.DefaultExtension = "BAS"
pofd.Filter = CHR$("PB Code Files (*.BAS)", 0, "*.BAS", 0) & _
CHR$("PB Include Files (*.INC)", 0, "*.INC", 0) & _
CHR$("PB Template Files (*.PBTPL)", 0, "*.PBTPL", 0) & _
CHR$("All Files (*.*)", 0, "*.*", 0)
pofd.Options = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST OR %OFN_ALLOWMULTISELECT
IF pofd.ShowOpenDialog THEN
LOCAL pFiles AS IPowerCollection
LOCAL vFile AS VARIANT
pFiles = pofd.Files
? "Selected path: " & pofd.SelectedPath
FOR EACH vFile IN pFiles
? VARIANT$$(vFile)
NEXT
END IF
END SUB
' ========================================================================================
' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG
' // Set process DPI aware
IF AfxGetWindowsVersion => 6 THEN SetProcessDPIAware
' // Create an instance of the class
LOCAL pWindow AS IWindow
pWindow = CLASS "CWindow"
IF ISNOTHING(pWindow) THEN EXIT FUNCTION
' // Create the main window
' // Note: CW_USEDEFAULT is used as the default value When passing 0's as the width and height
pWindow.CreateWindow(%NULL, "CWindow with Open File Dialog", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
' // Set the client size
pWindow.SetClientSize 500, 320
' // Center the window
pWindow.CenterWindow
' // Add buttons
pWindow.AddButton(pWindow.hwnd, %IDOK, "&Start", 0, 0, 75, 23)
pWindow.AddButton(pWindow.hwnd, %IDCANCEL, "&Close", 0, 0, 75, 23)
' // Default message pump (you can replace it with your own)
pWindow.DoEvents(nCmdShow)
END FUNCTION
' ========================================================================================
' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
STATIC hInstance AS DWORD ' // Instance handle
STATIC lpc AS CREATESTRUCT PTR ' // Pointer to the creation parameters
STATIC pWindow AS IWindow ' // Reference to the IWindow interface
SELECT CASE uMsg
CASE %WM_CREATE
' // Pointer to the creation parameters
lpc = lParam
' // Instance handle
hInstance = @lpc.hInstance
' // Get a reference to the IWindow interface from the CREATESTRUCT structure
pWindow = CWindow_GetObjectFromCreateStruct(lParam)
EXIT FUNCTION
CASE %WM_COMMAND
SELECT CASE LO(WORD, wParam)
CASE %IDOK
OpenFileDialogClass(hwnd)
EXIT FUNCTION
CASE %IDCANCEL
' // If the Escape key has been pressed...
IF HI(WORD, wParam) = %BN_CLICKED THEN
' // ... close the application by sending a WM_CLOSE message
SendMessage hwnd, %WM_CLOSE, 0, 0
EXIT FUNCTION
END IF
END SELECT
CASE %WM_SIZE
' // If the window isn't minimized, resize it
IF wParam <> %SIZE_MINIMIZED THEN
' // Resize the sample button
pWindow.MoveWindow GetDlgItem(hwnd, %IDOK), pWindow.ClientWidth - 195, pWindow.ClientHeight - 35, 75, 23, %TRUE
pWindow.MoveWindow GetDlgItem(hwnd, %IDCANCEL), pWindow.ClientWidth - 95, pWindow.ClientHeight - 35, 75, 23, %TRUE
END IF
CASE %WM_DESTROY
' // End the application
PostQuitMessage 0
EXIT FUNCTION
END SELECT
' // Pass unprocessed messages to Windows
FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)
END FUNCTION
' ========================================================================================