VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Open common dialog box that lets the user specify the drive, directory, and the name of a file or s

by Byte Masters TEAM (4 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Tue 21st February 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Open common dialog box that lets the user specify the drive, directory, and the name of a file or set of files to open.

Rate Open common dialog box that lets the user specify the drive, directory, and the name of a file or s




Button
Listbox

This is  a simple project that shows how to reuse existing resources of windows
Like Common dialogs and use them via simple API calls instead of 
Adding the CommonDialog control "Comdlg32.ocx"


--------------------------------------------------------------
IN a Form
--------------------------------------------------------------

Option Explicit

'The only used API function
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

'Structure will be passed to API function
Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

'Options of Common dialog 
Const OFN_READONLY = &H1
Const OFN_OVERWRITEPROMPT = &H2
Const OFN_HIDEREADONLY = &H4
Const OFN_NOCHANGEDIR = &H8
Const OFN_SHOWHELP = &H10
Const OFN_ENABLEHOOK = &H20
Const OFN_ENABLETEMPLATE = &H40
Const OFN_ENABLETEMPLATEHANDLE = &H80
Const OFN_NOVALIDATE = &H100
Const OFN_ALLOWMULTISELECT = &H200
Const OFN_EXTENSIONDIFFERENT = &H400
Const OFN_PATHMUSTEXIST = &H800
Const OFN_FILEMUSTEXIST = &H1000
Const OFN_CREATEPROMPT = &H2000
Const OFN_SHAREAWARE = &H4000
Const OFN_NOREADONLYRETURN = &H8000
Const OFN_NOTESTFILECREATE = &H10000
Const OFN_NONETWORKBUTTON = &H20000
Const OFN_NOLONGNAMES = &H40000                      '  force no long names for 4.x modules
Const OFN_EXPLORER = &H80000                         '  new look commdlg
Const OFN_NODEREFERENCELINKS = &H100000
Const OFN_LONGNAMES = &H200000                       '  force long names for 3.x modules



Private Sub Command1_Click()

'Clear the listbox items
List1.Clear


'Create a structure and fill it
    Dim OFName As OPENFILENAME
    OFName.lStructSize = Len(OFName)
    'Set the parent window
    OFName.hwndOwner = Me.hWnd
    'Set the application's instance
    OFName.hInstance = App.hInstance
    'Select a filter
    OFName.lpstrFilter = "MP3 Files (*.mp3)" + Chr$(0) + "*.mp3" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
    'create a buffer for the file
    OFName.lpstrFile = Space$(10239)
    'set the maximum length of a returned file
    OFName.nMaxFile = 10240
    'Create a buffer for the file title
    OFName.lpstrFileTitle = Space$(10239)
    'Set the maximum length of a returned file title
    OFName.nMaxFileTitle = 10240
    'Set the initial directory
    'OFName.lpstrInitialDir = ""
    'Set the title
    OFName.lpstrTitle = "Open File - KPD-Team 1998"
    'Flags or Style of common dialog
    OFName.flags = OFN_HIDEREADONLY Or OFN_ALLOWMULTISELECT Or OFN_EXPLORER
    
    

    'Show the 'Open File'-dialog
    'If the user specifies a filename and clicks the OK button, the return value is nonzero

    If GetOpenFileName(OFName) Then
    
        'Get the dirty Filename
        'The function will return a filename like this

'With single selection :
'The filename property is the only selected file

'With multi selection
'The filename contains The directory of the selected files
'and followed by all selecte files names separated by a CHR(0) 


        Dim FN() As String
        FN = Split(OFName.lpstrFile, Chr(0))
        
        'For cleaning filename from nulls
        Dim i As Long
        Dim G() As String
        
        
        If UBound(FN) > 2 Then 'Multi selection
        
'FN(0) is the directory of all seleted files
            'Fix Root path
            If Right$(FN(0), 1) <> "\" Then
                FN(0) = FN(0) & "\"
            End If
        
            'Remove 3 items from clean array because the FN array has 2 empty items at the end
'and the first item which is the directory of the files
'The array G will contain fully qualified file names !

            ReDim G(UBound(FN) - 3) As String
            'Start filling from index 1 to [end-2]
            For i = 1 To UBound(FN) - 2
         'Add Directory + File name
                G(i - 1) = FN(0) & FN(i)
        
            Next i
        
        Else 'Single filename
        
            ReDim G(0) As String
            G(0) = FN(0)
        
        End If
        


        'Fill listBox
        For i = 0 To UBound(G) ' - 1
        
        List1.AddItem G(i)
        
        Next i
    
    

    End If
    
    

End Sub

Download this snippet    Add to My Saved Code

Open common dialog box that lets the user specify the drive, directory, and the name of a file or s Comments

No comments have been posted about Open common dialog box that lets the user specify the drive, directory, and the name of a file or s. Why not be the first to post a comment about Open common dialog box that lets the user specify the drive, directory, and the name of a file or s.

Post your comment

Subject:
Message:
0/1000 characters