Results 1 to 10 of 10

Thread: [RESOLVED] Using Common Dialog box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    136

    Resolved [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)

  2. #2
    Lively Member okosv's Avatar
    Join Date
    Sep 2006
    Posts
    95

    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:
    1. With CommonDialog1
    2.     .Show
    3.     If .FileName="" Then
    4.     'Cancel button
    5.     Else
    6.     'OK button
    7.     End If
    8. End With

  3. #3
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Using Common Dialog box

    Quote 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:
    1. With CommonDialog1
    2.     .Show
    3.     If .FileName="" Then
    4.     'Cancel button
    5.     Else
    6.     'OK button
    7.     End If
    8. 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:
    1. Sub OpenFile()
    2. On Error GoTo Err
    3. With CommonDialog
    4. .CancelError = True
    5. .ShowOpen
    6. LoadFile .FileName 'user pressed OK
    7. End With
    8. Exit Sub
    9. Err:
    10. 'user pressed cancel
    11. MsgBox "Cancel was pressed"
    12. End Sub
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  4. #4
    Lively Member okosv's Avatar
    Join Date
    Sep 2006
    Posts
    95

    Re: Using Common Dialog box

    you can set .FileName property to empty string before run method ShowOpen or other

  5. #5
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Using Common Dialog box

    VB Code:
    1. Sub OpenFile()
    2. On Error GoTo ErrHandler
    3.  
    4. With CommonDialog
    5.       .CancelError = True
    6.       .ShowOpen
    7.       If .FileName <> "" Then
    8.           'do something with the file
    9.        End If
    10. End With
    11. Exit Sub
    12. ErrHandler:
    13. If Err = cdlCancel Then
    14. 'user pressed cancel
    15. Else
    16. MsgBox Err.Description
    17. End If
    18. 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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    136

    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?

  7. #7
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Using Common Dialog box

    Quote 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:
    VB Code:
    1. If Err = cdlCancel Then
    ..checks if the user pressed cancel while using .CancelError.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  8. #8
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Using Common Dialog box

    Quote Originally Posted by Keithuk
    VB Code:
    1. ErrHandler:
    2. If Err = cdlCancel Then
    3. 'user pressed cancel
    4. Else
    5. MsgBox Err.Description
    6. End If
    7. 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.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    136

    Re: Using Common Dialog box

    thank you friends.it works fine.
    how can i mark this post as resolved?

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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
  •  



Click Here to Expand Forum to Full Width