Jump to content

frmCommonDialog - Cancelling


AC2Designs

Recommended Posts

If a file gets selected and is in the Filename textbox, it does not matter if you choose "OK" or "Cancel" the name still gets based to my code.

 

it does not matter if I click "OK" or "Cancel" what matters is whether there is anything in the filename box or not.

 

Is there a way around this?

Or do I need to read the click event somehow to test whether "OK" or "Cancel" was clicked?

How would I do this?

 

Thanks for your help,

 

Kevin

Link to comment
Share on other sites

It will be useful to post an excerpt from your code for debugging.

 

However the code below works correctly on my workstation:

 

CommonDialog1.FileName = "C:\MyTestFile.txt"
CommonDialog1.Filter = "Text Files (*.TXT) |*.txt" & "|All Files (*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.DialogTitle = "Select a file "

CommonDialog1.ShowOpen

MsgBox CommonDialog1.FileName

 

Regards,

Link to comment
Share on other sites

Try with this:

Sub OpenFile()
   On Error GoTo errHan
   ' Your code
   '
   '
   '
   Exit
errHan:
   If Err.Number = 32755 Then    'Cancel
       Unload Me
   ElseIf Err.Number = 75 Then
       MsgBox "File " & fName & " is open"
       Unload Me
   End If
End Sub

Link to comment
Share on other sites

I do not think I was clear on my original post.

My code works fine, and I do not get any errors.

The problem is when prompted for a Filename, there is no way to cancel out.

 

ex.

When I click "Cancel" in my commomdialog, if there is a Filename in the FileName textbox, the Filename still gets passed to

commondialog1.filename

 

I guess what I need, is a way to determine which button I click, "OK" or "Cancel"

 

Is that possible?

 

Kevin

Link to comment
Share on other sites

From your post and signature I presume that you are using VBA (VSTA isn’t supported under AutoCAD 2008). For this reason my proposed example is based on VBA and probably wasn't clear enough in my first post.

 

So, what component are you using for Open box? Should be "Microsoft Common Dialog Control 6.0".

 

On a new form add two buttons and a CommonDialog control and paste the code below.

 

Private Sub CommandButton1_Click()
   Dim SelectedFile As String
   CommonDialog1.FileName = "C:\MyTestFile.txt"
   CommonDialog1.ShowOpen
   [color=red]SelectedFile = CommonDialog1.FileName
[/color]    MsgBox SelectedFile
End Sub

Private Sub CommandButton2_Click()
   Dim SelectedFile As String
   CommonDialog1.FileName = "C:\MyTestFile.txt"
   [color=red]SelectedFile = CommonDialog1.FileName[/color]
   CommonDialog1.ShowOpen
   MsgBox SelectedFile
End Sub

 

As you can see it depends where in code you are getting the file name; the first approach is the right one (I don’t mean that it is the only one) and should solve your issue.

 

Regards,

Link to comment
Share on other sites

My code works fine, and I do not get any errors.

 

Regarding @goran’s suggestion, usage of error handler can be handy even when you don’t have a code error (mistake) – sometime may deliberate cause an error just to test it and act accordingly; it is a quite common programming practice (i.e. try / except coding).

Only one comment, on CommonDialog’s boxes the error isn’t raised by default for Cancel usage, so need to explicitly force it by using:

 

CommonDialog1.CancelError = True

 

Regards,

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...