Vb6 检测CommonDialog控件中的取消按钮

Vb6 检测CommonDialog控件中的取消按钮,vb6,Vb6,在VB6中,如果我按下打开文件对话框上的取消按钮,我的文件名仍会添加到我的列表框中 例如: Private Sub btnImportImage_Click() DailogOpenFile.ShowOpen If Trim$(txtEmailAttachment.Text) = "" Then txtEmailAttachment.Text = DailogOpenFile.FileName Else txtEmailAttachment

在VB6中,如果我按下
打开文件
对话框上的
取消
按钮,我的文件名仍会添加到我的列表框中

例如:

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
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

看起来您正在使用
CommonDialog
控件?如果是这样,您需要将
CancelError
属性设置为
True
,然后测试错误。例如:

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
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
当然,您也可以跳转到错误处理程序:

Private Sub btnImportImage_Click()

    DailogOpenFile.CancelError = True

    On Error GoTo MyErrorHandler
    DailogOpenFile.ShowOpen

    ...

MyErrorHandler:
    ' Cancel was clicked or some other error occurred

End Sub

谢谢你的建议@债券:D