Click to See Complete Forum and Search --> : Common Dialog Handling a cancel


DLARLICK
July 5th, 2001, 03:01 PM
I am working with several of the common dialog forms. My question is regarding the cancel. I am surprised that the dialogs don't set an attribute that the form was cancelled and believe I am missing something easy.

I have written the following to catch the error code but I am unsure if this is the best or even appropriate way to handle this.

I believe the Microsoft example has the code checking the filename after the execution of the dialog to see if a filename is returned. Unfortunately this doesn't seem to work if you are providing a default file for the dialog.


private Function OpenCommonDialog(sFileName as string) as string
'Work around to determine if the cancel button was clicked on the
' common dialog. The dialog doesn't set an attribute. You must
' catch the exception. Thanks Microsoft! Hence the seperate function
' for this purpose.

on error GoTo ErrorHandler:
'Perform the file write here
'prompt for the file location.
dlgBrowseFile.Flags = cdlOFNOverwritePrompt & cdlOFNPathMustExist
dlgBrowseFile.InitDir = sFileName
dlgBrowseFile.Filter = "All Text (*.txt)|*.txt"
dlgBrowseFile.DefaultExt = "REV"
dlgBrowseFile.CancelError = true
dlgBrowseFile.ShowSave
sFileName = dlgBrowseFile.filename

End1:
OpenCommonDialog = sFileName
Exit Function

ErrorHandler:
sFileName = ""
resume End1:
End Function




Thanks EveryOne
All comments welcome...
(I'll do anything to avoid the goto statement!)

Iouri
July 5th, 2001, 03:58 PM
Use the CancelError property to trigger a trappable error if cancel is selected, i.e.


private Sub Command1_Click()
on error GoTo Cancelled
With CommonDialog1
.CancelError = true
.ShowSave
'Save Code Here
End With
MsgBox "File Saved.", vbInformation + vbOKOnly, "Saved."
Cancelled:
End Sub

===========
CommonDialog1.CancelError = true
CommonDialog1.ShowOpen
If Err.Number <> 0 then Exit Sub

Iouri Boutchkine
iouri@hotsheet.com

softweng
July 5th, 2001, 05:33 PM
Here is how I use the CommonDialog Control:

private Function GetPCFilePath() as string

on error GoTo ErrHandler

'//Reset Return Value to Default
GetPCFilePath = ""

'//set CancelError true
cdPC.CancelError = true

'//set Title
cdPC.DialogTitle = "Please Select Path And File Name Of Master PC List"

'//set flags
cdPC.Flags = cdlOFNHideReadOnly

'//set filters
cdPC.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

'//Specify default filter
cdPC.FilterIndex = 1

'//Display the Open dialog box
cdPC.ShowOpen

'//Mark Success
GetPCFilePath = cdPC.FileName

Exit Function

ErrHandler:
'//User pressed the Cancel button
If Err.Number = cdlCancel then
MsgBox "Operation Canceled.", vbExclamation + vbApplicationModal
else
MsgBox "An error Has Occurred." & vbCrLf & Err.Number & vbCrLf & Err.Description, vbExclamation + vbApplicationModal
End If

End Function




Just set the .CancelError = True (like Iouri said)

Then you can use the Constant cdlCancel and compare it with the error code returned, if there
was one. If you need me to explain any of it let me know.

Kris
Software Engineer
Phoenix,AZ

RussLeinweber
May 21st, 2005, 02:47 PM
I am not fond of Microsoft's ways but in this case their method is not too bad. Even so, they could've done better. If you check the FileName propery after the dialog has been canceled, it will only return the default file you have specified in the FileName property, without a full path. If it returns a full path, the dialog box was not canceled. If you have specified a full path for a default file, then you will have to use the error routines described by the other members in this thread.

Example:
If your Common Dialog is named cdlgMain and you have set the default file with the FileName property to DefaultFile.txt, then you can use this code:

if cdlgMain.FileName="DefaultFile.txt" then
'The common dialog was canceled
else
'The common dialog was not canceled.
'A file was selected
end if

I usually do not specify a full path in common dialogs just for this purpose.
I hope this information helps.

I am working with several of the common dialog forms. My question is regarding the cancel. I am surprised that the dialogs don't set an attribute that the form was cancelled and believe I am missing something easy.

I have written the following to catch the error code but I am unsure if this is the best or even appropriate way to handle this.

I believe the Microsoft example has the code checking the filename after the execution of the dialog to see if a filename is returned. Unfortunately this doesn't seem to work if you are providing a default file for the dialog.


private Function OpenCommonDialog(sFileName as string) as string
'Work around to determine if the cancel button was clicked on the
' common dialog. The dialog doesn't set an attribute. You must
' catch the exception. Thanks Microsoft! Hence the seperate function
' for this purpose.

on error GoTo ErrorHandler:
'Perform the file write here
'prompt for the file location.
dlgBrowseFile.Flags = cdlOFNOverwritePrompt & cdlOFNPathMustExist
dlgBrowseFile.InitDir = sFileName
dlgBrowseFile.Filter = "All Text (*.txt)|*.txt"
dlgBrowseFile.DefaultExt = "REV"
dlgBrowseFile.CancelError = true
dlgBrowseFile.ShowSave
sFileName = dlgBrowseFile.filename

End1:
OpenCommonDialog = sFileName
Exit Function

ErrorHandler:
sFileName = ""
resume End1:
End Function




Thanks EveryOne
All comments welcome...
(I'll do anything to avoid the goto statement!)