|
|
Title | Make a Common Dialog start in its last location |
Description | This example shows how to make a Common Dialog control start in its last location in Visual Basic 6. After the user selects a file, the program extracts the file's path and sets the dialog's InitDir property to that path. |
Keywords | Common Cialog, dialog, open file, save file |
Categories | Files and Directories, Controls |
|
|
After opening a file, pull the path out of the dialog's FileName property. Set its InitDir property to this path.
|
|
Private Sub cmdOpenFile_Click()
Dim file_name As String
dlgFile.CancelError = True
dlgFile.Flags = _
cdlOFNFileMustExist Or _
cdlOFNHideReadOnly Or _
cdlOFNExplorer Or _
cdlOFNLongNames
On Error Resume Next
dlgFile.ShowOpen
If Err.Number = cdlCancel Then
' The user canceled.
Exit Sub
ElseIf Err.Number <> 0 Then
' Unknown error.
MsgBox "Error " & Format$(Err.Number) & _
" selecting file." & vbCrLf & _
Err.Description
Exit Sub
End If
On Error GoTo 0
' Do something with the file.
file_name = dlgFile.FileName
dlgFile.InitDir = Left$(file_name, _
Len(file_name) - _
Len(dlgFile.FileTitle) - 1)
txtFiles.Text = file_name & vbCrLf & txtFiles.Text
End Sub
Private Sub Form_Load()
' Start in the current directory.
dlgFile.InitDir = App.Path
End Sub
|
|
You could also save this path in the Registry or some other place so the program could begin in that directory the next time it ran.
|
|
|
|
|
|