Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
目前成都創(chuàng)新互聯(lián)公司已為上千多家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)站空間、成都網(wǎng)站托管、企業(yè)網(wǎng)站設計、網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。我們擁有完善的網(wǎng)絡基礎設施服務,能夠為企業(yè)或個人提供主機域名、網(wǎng)站空間、企業(yè)郵局、網(wǎng)站加速、數(shù)據(jù)庫、云主機等網(wǎng)絡基礎服務。
MsgBox("窗口即將關閉....")
End Sub
Else
e.Cancel = True
End If
VB.NET關閉當前窗體me.close()
其它窗體用:窗體名.close(),例如:form2.close()
利用Process類遍歷當前所有進程,利用MainWindowTitle屬性來獲取指定窗口標題的進程ID,創(chuàng)建一個線程池或者線程,來循環(huán)檢測這個ID的進程是否已退出,在循環(huán)線程中設置Thread.Sleep(50)中斷來減少系統(tǒng)開銷,這個方法僅適用于主窗口,也就是窗體關閉,程序就退出了,如果針對的是子窗口,就用第二種方法。
利用系統(tǒng)API,循環(huán)遍歷所有窗口,當指定標題不存在時就是已經(jīng)退出了。
首先這個應該放在 FormClosing 事件中。
其次,因為你 Else 之后沒有忽略窗體關閉的請求。
應該再加一句 e.Cancel = True,表示告訴系統(tǒng),我取消關閉窗體這個操作了。
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("未定標題 文件的文字已經(jīng)改變。" 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 + " 文件的文字已經(jīng)改變。" 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