vb.net窗体关闭的问题!
没事做了个记事本,但在窗体关闭的时候出了点问题.如下:按了窗体右上角的"X"关闭窗体时,会提示是否保存(是,否,取消),我是在closing(closed更不行)里面加载...
没事做了个记事本,但在窗体关闭的时候出了点问题.如下:
按了窗体右上角的"X"关闭窗体时,会提示是否保存(是,否,取消),我是在closing(closed更不行)里面加载的判断程序.按'取消'后是Exit sub,不过不行,还是退出程序(我估计这个事件执行时都已经释放窗体资源了).
请教高手,按了'关闭'按钮之后是应该在哪个事件里加载判断程序呢(就是比closing还要之前执行的事件)?才不能让窗体关闭了.或者有什么语句是跳出程序的呢? 展开
按了窗体右上角的"X"关闭窗体时,会提示是否保存(是,否,取消),我是在closing(closed更不行)里面加载的判断程序.按'取消'后是Exit sub,不过不行,还是退出程序(我估计这个事件执行时都已经释放窗体资源了).
请教高手,按了'关闭'按钮之后是应该在哪个事件里加载判断程序呢(就是比closing还要之前执行的事件)?才不能让窗体关闭了.或者有什么语句是跳出程序的呢? 展开
2个回答
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
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
End
Else
e.Cancel = True
End If
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询