vb.net窗体关闭的问题!

没事做了个记事本,但在窗体关闭的时候出了点问题.如下:按了窗体右上角的"X"关闭窗体时,会提示是否保存(是,否,取消),我是在closing(closed更不行)里面加载... 没事做了个记事本,但在窗体关闭的时候出了点问题.如下:
按了窗体右上角的"X"关闭窗体时,会提示是否保存(是,否,取消),我是在closing(closed更不行)里面加载的判断程序.按'取消'后是Exit sub,不过不行,还是退出程序(我估计这个事件执行时都已经释放窗体资源了).
请教高手,按了'关闭'按钮之后是应该在哪个事件里加载判断程序呢(就是比closing还要之前执行的事件)?才不能让窗体关闭了.或者有什么语句是跳出程序的呢?
展开
 我来答
匿名用户
2013-07-12
展开全部
Private Sub frmNotepad_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Call subexit()
End
End Sub

Sub subexit()
Dim frmNew As frmNotepad
frmNew = ActiveForm
If frmNew.Text = "未定标题 - 记事本" Then
If frmNew.rtb.Text = "" Then

Else
Dim result As New MsgBoxResult 'result提示对话框yes,no,cancel
result = MsgBox("未定搏尘标题 文件的文字已经改变。"蚂游 & Chr(10) & Chr(10) & "想保存文件基物禅吗?", MsgBoxStyle.YesNoCancel + MsgBoxStyle.Exclamation, "记事本")
If result = MsgBoxResult.Yes Then 'result.Yes表示保存,清空内容,打开新页面
Dim fileSave As New SaveFileDialog
Dim re As New DialogResult 're提示对话框OK,cancel
fileSave.FileName = "*.txt"
fileSave.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*"
re = fileSave.ShowDialog()
If re = DialogResult.OK Then 're.OK表示成功保存,清空内容,打开新页面

filename = fileSave.FileName
Dim fstream As FileStream
Dim sw As StreamWriter
Try
'frmNew.Text = filename.Substring(filename.LastIndexOf("\") + 1) & "- 记事本"
fstream = New FileStream(filename, FileMode.Create, FileAccess.ReadWrite)
sw = New StreamWriter(fstream, System.Text.Encoding.Default)
sw.BaseStream.Seek(0, SeekOrigin.End)
sw.Write(rtb.Text)
sw.Flush()
Catch ex As Exception
MsgBox("保存文件失败")
Finally
sw.Close()
End Try
ElseIf re = DialogResult.Cancel Then 're.cancel表示不保存,不改变任何结果

End If

ElseIf result = MsgBoxResult.No Then 'result.no表示不保存,清空内容
rtb.Text = ""
End If

End If
Else

If rtb.Text.Compare(rtb.Text, compareStr) <> 0 Then
Dim result As New MsgBoxResult
result = MsgBox(filename + " 文件的文字已经改变。" & Chr(10) & Chr(10) & "想保存文件吗?", MsgBoxStyle.YesNoCancel + MsgBoxStyle.Exclamation, "记事本")
If result = MsgBoxResult.Yes Then

Dim fstream As FileStream
Dim sw As StreamWriter
Try
'frmNew.Text = filename.Substring(filename.LastIndexOf("\") + 1) & "- 记事本"
fstream = New FileStream(filename, FileMode.Create, FileAccess.ReadWrite)
sw = New StreamWriter(fstream, System.Text.Encoding.Default)
sw.BaseStream.Seek(0, SeekOrigin.End)
sw.Write(rtb.Text)
sw.Flush()
Catch ex As Exception
MsgBox("保存文件失败")
Finally
sw.Close()
End Try

ElseIf result = MsgBoxResult.No Then
rtb.Text = ""

End If

End If

End If

End Sub
匿名用户
2013-07-12
展开全部
If MsgBox("是否要族橘退出消穗此系统?拿迅", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "系统消息") = MsgBoxResult.Yes Then
End
Else
e.Cancel = True
End If
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式