vb里面的对话框控件,打开“另存为”对话框后,单击取消选择不保存后还是会在目录保存文件

2024-12-18 06:59:35
推荐回答(3个)
回答1:

这个COM的对话框控件取消就是这样的机制,要判断用户点的是“取消”,就要设置控件的CancelError属性为True才可以接收到用户是否点了取消(这样真是增加了程序员的代码工作量),对了你的程序修改成这样就可以了:
On Error Resume Next

Dialog.Flags = &H4
Dialog.DefaultExt = ".txt"
Dialog.FilterIndex = 1
Dialog.CancelError = False
Dialog.ShowSave

If Err.Number = 0 Then
Open Dialog.FileName For Output As #1
Print #1, Text_List.Text
Close #1
End If 答案补充 有上的有误,下面的正确:
On Error Resume Next
Dialog.Flags = &H4
Dialog.DefaultExt = ".txt"
Dialog.FilterIndex = 1
Dialog.CancelError = True
Dialog.ShowSave

If Err.Number = 0 Then
Open Dialog.FileName For Output As #1
Print #1, Text_List.Text
Close #1
End If

回答2:

Private Sub Command1_Click()
On Error GoTo ErrProc:
Dialog.Flags = &H4
Dialog.Filter = "*.txt"
Dialog.FilterIndex = 1
Dialog.CancelError = True
Dialog.ShowSave
Open "C:\Documents and Settings\11111\桌面\11.txt" For Output As #1
Print #1, Text1
Close #1
ErrProc:
End Sub 答案补充 Private Sub Command1_Click()
On Error GoTo ErrProc:
Dialog.Flags = &H4
Dialog.Filter = "*.txt"
Dialog.CancelError = True
Dialog.ShowSave
Open Dialog.FileName & ".txt" For Output As #1
Print #1, Text1
Close #1
ErrProc:
End Sub

回答3:

把CancelError设置为True当点取消时会产生一个错误 在程序开头加上On Error GoTo Wrong:
在程序最后写上Wrong:就可以了