Hi everyone! Our next program to create is on how to shutdown, restart and logoff our computer in Visual Basic 6.
Before we start our tutorial let me remind you that you should save all your work to make sure you don’t loss your files.
Let us proceed now to the step by step procedure of creating the program.
1. Create a new project in your vb6 program and select Standard EXE.
2. By default there is only one Form (Form1). Rename the Form1 into ShutdownFrm.
3. We are going to design the form, open ShutdownFrm. We need four command buttons, rename the first button into cmdShutdown with the caption Shutdown, next button into cmdRestart with caption Restart, rename the next button into cmdLogoff with the caption Logoff and the last one into cmdAbort with the caption Abort. Save the visual basic project and the form as well in the same folder.
This is the result of the form:
You may change the design of the form and buttons based on your preferences.
4. The next process is to add the commands or codes to each button.
5. Double click Shutdown button and add the lines of code:
If vbYes = MsgBox("Are you really sure you want to shutdown the computer?", vbQuestion + vbYesNo, "System will shutdown in 100 sec") Then
Shell ("shutdown -s -t 100")
End If
Code Explanation: the code will ask you first if you really want to shutdown the unit, if yes then the timer for the shutdown will initialize. You have 100 seconds to abort or cancel the system shutdown.
Friendly Advice: Save all your work to prevent loss of data and files.
6. Double click the Restart button and add the following lines of code:
If vbYes = MsgBox("Are you really sure you want to restart the computer?", vbQuestion + vbYesNo, "System will restart in 100 sec") Then
Shell ("shutdown -r -t 100")
End If
Code Explanation: the code will ask you first if you really want to restart the unit, if yes then the timer for the restart will initialize. You have 100 seconds to abort or cancel the restart operation.
Friendly Advice: Let me remind you again to SAVE all your work to prevent loss of data and files.
7. Double click the Logoff button and add the code below:
If vbYes = MsgBox("Are you really sure you want to logoff the computer?", vbQuestion + vbYesNo, "System will logoff automatically without time countdown") Then
Shell ("shutdown -l")
End If
Code Explanation: the code will ask you first if you really want to logoff the computer, if yes then the computer will logoff automatically without time countdown. This operation can’t be aborted.
Friendly Advice: Let me remind you again to SAVE all your work to prevent loss of data and files.
8. This is the most important of all the code on how to cancel or abort the restart and shutdown operation. Double click the Abort button and paste the code below.
Shell ("shutdown -a")
9. Before you test project, please SAVE all your work.
This project will work only on Windows operating system. This project has been tested on Windows XP and Windows 7.