VB.NET How to properly close a windows application Form
VB.NET
How to properly close a windows application Form
VB.NET How to properly close a windows application Form |
Requirements
Visual Basic .Net (any version) , VB2010 and up recommend
.NET FrameWork 4.0 and above recommended
.NET FrameWork 4.0 and above recommended
How to properly close a windows application Form in VB.NET ?
You may close a Form in VB.NET Application [WinForm] by :KeyPress event
Before you test this code you need to set the Form Property [KeyPreview] True, from Form Properties Window.
The code below will Read Keyboard button [Escape] when clicked and Executes Close()
Private Sub MainFrm_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = Chr(Keys.Escape) Then Close() End IfEnd Sub
Then in the FormClosing() event, we will place a code to display a message [MsgBox] with [Yes, No, Cancel]. If the user clicked [Yes] then it will Close the Form and Ends the application.
Private Sub MainFrm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim AreyouSure As String = MsgBox("Exit application", MsgBoxStyle.YesNoCancel + MsgBoxStyle.Information, "Exit") If AreyouSure = MsgBoxResult.Yes Then End 'End Application ElseIf AreyouSure = MsgBoxResult.No Then e.Cancel = True 'Don't Close the form Exit Sub Else e.Cancel = True Exit Sub End If
End Sub
Click [X] button on the Form
Clicking on [X] button will also triggers the above code, resulting a message box for the user to choose [Yes, No, Cancel].
Note :
If you wish to disable the [X] button function in a VB.Net WinForm, in FormClosing() event, use this code :Private Sub MainFrm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
e.Cancel=TrueEnd Sub
link to this article : https://adonetaccess2003.blogspot.com/2019/10/vbnet-close-form.html
♥ Here are some online Visual Basic lessons and courses :
- Visual Basic .Net snippets collection
- Visual Basic .Net - How to check for the internet connection
- Visual Basic .Net - POP3 and Receiving E-mails
- Visual Basic .Net - Generate Random Combinations
- Visual Basic .Net - Play sounds on Button Click or Mouse Hover
- Visual Basic .Net - Progressbar control
- Visual Basic .Net Solution - The application failed to initialize
- Visual Basic .Net - Working with Database full example
- Visual Basic .Net - There is already an open DataReader associated
- Visual Basic .Net - SyBase Advantage Database [Add, Edit, Search, Delete and DataGridView]
- Visual Basic .Net - Fade Image
- Visual Basic .Net - Free POS System Project Source Code