When coding, We Need To prompt users before they exit the system for a more user-friendly design, such:
The implementation methods are divided into two categories: Form Events and control events, which are shown as follows:
I. formclosing event (several methods are described below)
A.
Private sub frmpractise_formclosing (byval sender as object, byval e as system. Windows. Forms. formclosingeventargs) handles me. formclosing if MessageBox. Show ("are you sure you want to exit? "," Exit confirmation ", messageboxbuttons. yesno, messageboxicon. Question) = windows. Forms. dialogresult. No then E. Cancel = true end if end sub
B.
Private sub practise_formclosing (byval sender as object, byval e as system. Windows. Forms. formclosingeventargs) handles me. formclosing if msgbox ("are you sure you want to exit? ", Messageboxbuttons. okcancel," Exit confirmation ") = windows. Forms. dialogresult. Cancel then E. Cancel = true end if end sub
C.
Private sub practise_formclosing (byval sender as object, byval e as system. Windows. Forms. formclosingeventargs) handles me. formclosing dim P as integer p = msgbox ("Do you really want to exit the system? ", Msgboxstyle. okcancel," prompt ") if p = 2 then E. Cancel = true end if end sub
D. (simplest)
Private sub practise_formclosing (byval sender as object, byval e as system. Windows. Forms. formclosingeventargs) handles me. formclosing if msgbox ("are you sure you want to exit the program? ", Msgboxstyle. okcancel," Exit prompt ") = msgboxresult. Cancel then E. Cancel = true end sub
The above is just the usage when you click the close button in the window. If you click exit on the Form Control directly, you can use the following method to achieve it:
Ii. button_click event
Private sub btnquit_click (byval sender as system. Object, byval e as system. eventargs) handles btnquit. Click if msgbox ("do you want to exit the system? ", Msgboxstyle. yesno + msgboxstyle. Question," prompt ") = msgboxresult. yes then application. Exit () end if end sub