读书人

求问一个简单的CommonDialog控件的有关

发布时间: 2012-02-04 15:43:09 作者: rapoo

求问一个简单的CommonDialog控件的问题。
我用CommonDialog控件对话保存一个文件。
CommonDialog1.ShowSave
CommonDialog1.Filter = "(*.dat)|*.dat"
strFileName = CommonDialog1.FileName
If Dir(strFileName) <> "" Then
If MsgBox("文件已经存在,是否覆盖?", vbInformation + vbOKCancel, "提示") = vbCancel Then
Exit Sub
End If
End If
为什么对话框出来时我点击“取消”按钮,没有退出该对话框,而是出来“文件已经存在,是否覆盖”再点击取消才退出。
这个是那个地方设置不对,请朋友们指正一下,谢谢!

[解决办法]
If Dir(strFileName) <> "" And strFileName <> "" Then '增加一个判断
[解决办法]

on error resume Err
CommonDialog1.ShowSave
CommonDialog1.Filter = "(*.dat)|*.dat"
strFileName = CommonDialog1.FileName
If Dir(strFileName) <> "" Then
If MsgBox("文件已经存在,是否覆盖?", vbInformation + vbOKCancel, "提示") = vbCancel Then
Err: Exit Sub
End If
End If

[解决办法]

VB code
Option ExplicitPrivate Sub Command1_Click()        Dim strFileName As String                On Error GoTo ErrTocjl                CommonDialog1.ShowSave        CommonDialog1.CancelError = True        CommonDialog1.Filter = "(*.dat)|*.dat"        strFileName = CommonDialog1.FileName        If strFileName <> "" Then           If Dir(strFileName) <> "" Then              If MsgBox("文件已经存在,是否覆盖?", vbInformation + vbOKCancel, "提示") = vbCancel Then                 Exit Sub              Else                 '覆盖的代码              End If           End If        End IfErrTocjl:End Sub 

读书人网 >VB

热点推荐