Private Sub cmdEXIT_Click() '退出按钮
创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、成都做网站、宣州网络推广、成都小程序开发、宣州网络营销、宣州企业策划、宣州品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供宣州建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
Private Sub Form_Unload(Cancel As Integer) '窗体的关闭按钮
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "是否要确定退出 ?" ' 定义信息。
Style = vbYesNo + vbCritical + vbDefaultButton2 ' 定义按钮。
Title = "程序退出对话框" ' 定义标题。
Help = "DEMO.HLP" ' 定义帮助文件。
Ctxt = 1000 ' 定义标题
' 上下文。
' 显示信息。
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' 用户按下“是”。
MyString = "Yes" ' 完成某操作。
unload me '关闭程序
End If
End Sub
参考代码:
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
If MessageBox.Show("确定要关闭吗?", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
e.Cancel = True
End If
End Sub
运行效果:
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
这像是以前 VB 的写法,VB.net 里不是这样的,应该是:
Private Sub Form_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
MagBox("1111")
End Sub
注意,... Handles MyBase.Closed 是在同一行里的。
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
MsgBox("窗口即将关闭....")
End Sub
Else
e.Cancel = True
End If