Koyubi Choatky
Koyubi Choatky

Reputation: 63

Detecting Cancel button in CommonDialog control

In VB6, if I press the Cancel button on an Open File dialog box, my filename is still added into my listbox.

For example:

Private Sub btnImportImage_Click()
    DailogOpenFile.ShowOpen
    If Trim$(txtEmailAttachment.Text) = "" Then
        txtEmailAttachment.Text = DailogOpenFile.FileName
    Else
        txtEmailAttachment.Text = txtEmailAttachment.Text & ";" & DailogOpenFile.FileName
    End If

End Sub

Upvotes: 6

Views: 9556

Answers (1)

Bond
Bond

Reputation: 16311

It looks like you're using the CommonDialog control? If so, you need to set the CancelError property to True and then test for an error afterwards. For example:

Private Sub btnImportImage_Click()

    DailogOpenFile.CancelError = True

    On Error Resume Next
    DailogOpenFile.ShowOpen

    If Err.Number = &H7FF3 Then
        ' Cancel clicked
    Else

    End If

    ...

End Sub

Of course, you can also jump to an error handler:

Private Sub btnImportImage_Click()

    DailogOpenFile.CancelError = True

    On Error GoTo MyErrorHandler
    DailogOpenFile.ShowOpen

    ...

MyErrorHandler:
    ' Cancel was clicked or some other error occurred

End Sub

Upvotes: 6

Related Questions