Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Cancel button in CommonDialog control

Tags:

vb6

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
like image 387
Koyubi Choatky Avatar asked Jul 29 '15 09:07

Koyubi Choatky


1 Answers

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
like image 58
Bond Avatar answered Nov 01 '22 02:11

Bond