|
-
Sep 28th, 2006, 12:06 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Using Common Dialog box
Hai Friends
I am working with common dialog box. How can i find out the button pressed by the user( ok or Cancel)
-
Sep 28th, 2006, 12:19 AM
#2
Lively Member
Re: Using Common Dialog box
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
-
Sep 28th, 2006, 01:02 AM
#3
Re: Using Common Dialog box
 Originally Posted by okosv
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:
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:
VB 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
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 28th, 2006, 05:12 AM
#4
Lively Member
Re: Using Common Dialog box
you can set .FileName property to empty string before run method ShowOpen or other
-
Sep 28th, 2006, 06:33 AM
#5
Re: Using Common Dialog box
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
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
-
Sep 28th, 2006, 07:24 AM
#6
Thread Starter
Addicted Member
Re: Using Common Dialog box
thank you friends
but how can i handle other errors in that routine.Is there any specific error number for that error?
-
Sep 28th, 2006, 08:40 AM
#7
Re: Using Common Dialog box
 Originally Posted by GMKK
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:
..checks if the user pressed cancel while using .CancelError.
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 28th, 2006, 02:15 PM
#8
Re: Using Common Dialog box
 Originally Posted by Keithuk
VB Code:
ErrHandler:
If Err = cdlCancel Then
'user pressed cancel
Else
MsgBox Err.Description
End If
End Sub
The ErrHandler will show you other errors. If you don't press cancel and there is still an error MsgBox Err.Description
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
-
Sep 29th, 2006, 06:33 AM
#9
Thread Starter
Addicted Member
Re: Using Common Dialog box
thank you friends.it works fine.
how can i mark this post as resolved?
-
Sep 29th, 2006, 06:34 AM
#10
Re: Using Common Dialog box
Pull down the Thread Tools menu and click the Mark Thread Resolved menu item.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|