Hai Friends
I am working with common dialog box. How can i find out the button pressed by the user( ok or Cancel)
Printable View
Hai Friends
I am working with common dialog box. How can i find out the button pressed by the user( ok or Cancel)
If user pressed OK button .FileName property contents the file's path, if Cancel button, it contents empty string.
VB Code:
With CommonDialog1 .Show If .FileName="" Then 'Cancel button Else 'OK button End If End With
This is actually incorrect if the user opens more than 1 file. For example:Quote:
Originally Posted by okosv
1) User opens Open File Dialog
2) User Presses Cancel
3) .Filename is empty.
4) User opens Open File Dialog
5) User Selects file
6) .Filename now contains filename
7) User opens Open File Dialog
8) User Presses Cancel
9) .Filename still contains the filename of the previous file.
To overcome this, the .CancelError property is useful:
chemVB Code:
Sub OpenFile() On Error GoTo Err With CommonDialog .CancelError = True .ShowOpen LoadFile .FileName 'user pressed OK End With Exit Sub Err: 'user pressed cancel MsgBox "Cancel was pressed" End Sub
you can set .FileName property to empty string before run method ShowOpen or other
VB Code:
Sub OpenFile() On Error GoTo ErrHandler With CommonDialog .CancelError = True .ShowOpen If .FileName <> "" Then 'do something with the file End If End With Exit Sub ErrHandler: If Err = cdlCancel Then 'user pressed cancel Else MsgBox Err.Description End If End Sub
thank you friends
but how can i handle other errors in that routine.Is there any specific error number for that error?
Keithuk's example shows you how to do this. Using:Quote:
Originally Posted by GMKK
..checks if the user pressed cancel while using .CancelError.VB Code:
If Err = cdlCancel Then
chem
The ErrHandler will show you other errors. If you don't press cancel and there is still an error MsgBox Err.Description ;)Quote:
Originally Posted by Keithuk
thank you friends.it works fine.
how can i mark this post as resolved?
Pull down the Thread Tools menu and click the Mark Thread Resolved menu item.