I wrote this program a while back that would allow me to set a delay, say 3 hours, and when the delay had expired, my PC would turn off. The purpose of it was so I didn't have to leave my PC on all night whilst downloading games. I recently tinkered with it to allow me to have my PC shutdown at a specific time. It works like a treat, but I was hoping on a few pointers to help make the code more concise / efficient / cleaner.
Public Class frmMain
'Declaration of Variables
Public Seconds As Integer 'Store Seconds as Integer
Public Minutes As Integer 'Store Minutes as Integer
Public Hours As Integer 'Store Hours as Integer
Public CurHour As Integer 'Store the Current Hour as Integer
Public CurMinute As Integer 'Store the Current Minute as Integer
Public DelayOrTime As Integer 'Varibale used to distinguish between Shutdown on Delay or Shutdown on certain time
'Used for displaying delay time
Public show_hrs As String 'Store hours as String
Public show_mins As String 'Store minutes as String
Public show_secs As String 'Store seconds as String
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Reset dropdowns
cbxHours.SelectedIndex = 0
cbxMinutes.SelectedIndex = 0
cbxSetHour.SelectedIndex = 0
cbxSetMin.SelectedIndex = 0
DelayOrTime = 0
End Sub
Private Sub btnDelay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSDDelay.Click
'Convert Dropdown values to Integers
Minutes = CInt(cbxMinutes.Text)
Hours = CInt(cbxHours.Text)
Seconds = 0
DelayOrTime = 1
'Error check for Restart / Shutdown Radio buttons - make sure one is checked
If radRestart_d.Checked = False And radShutdown_d.Checked = False Then
MsgBox("Please select either 'Shutdown' or 'Restart'", MsgBoxStyle.Exclamation, "Select Option")
'Error check for Countdown / delay time values
ElseIf Minutes = 0 And Hours = 0 Then
MsgBox("Please enter a valid delay time", MsgBoxStyle.Exclamation, "Enter Delay")
'Start Countdown Procedure
Else
Dim reply As MsgBoxResult 'Variable that stores reply value of message box
reply = MsgBox("You are about to start the timer..." & vbNewLine & "Do You Want To Proceed?", MsgBoxStyle.YesNo, "Are You Sure?")
If reply = MsgBoxResult.Yes Then
tmrDelayCount.Enabled = True 'Enable Timer
tbcTabs.Enabled = False 'Disable UI
btnCancel.Enabled = True 'Enable Cancel Button
'Display Confirm Message
MsgBox("Your computer will shutdown in approximately: " & Hours & " Hours and " & Minutes & " Minutes " & vbNewLine & vbNewLine &
"To stop the countdown, simply press the 'Cancel' button", MsgBoxStyle.OkOnly, "Countdown")
Me.WindowState = FormWindowState.Minimized
End If
End If
End Sub
Private Sub btnSDTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSDTime.Click
Hours = CInt(cbxSetHour.Text)
Minutes = CInt(cbxSetMin.Text)
DelayOrTime = 2
'Error check for Restart / Shutdown Radio buttons - make sure one is checked
If radRestart_t.Checked = False And radShutdown_t.Checked = False Then
MsgBox("Please select either 'Shutdown' or 'Restart'", MsgBoxStyle.Exclamation, "Select Option")
'Start Countdown Procedure
Else
Dim reply As MsgBoxResult 'Variable that stores reply value of message box
reply = MsgBox("You are about to start the procedure" & vbNewLine & "Do You Want To Proceed?", MsgBoxStyle.YesNo, "Are You Sure?")
If reply = MsgBoxResult.Yes Then
tmrCheckTime.Enabled = True 'Enable Timer
'Disable UI and enable cancel
tbcTabs.Enabled = False
btnCancel.Enabled = True
'Display Confirm Message
MsgBox("Your computer will shutdown at approximately: " & Hours & " : " & Minutes & " " & vbNewLine & vbNewLine &
"To stop the countdown, simply press the 'Cancel' button", MsgBoxStyle.OkOnly, "Countdown")
Me.WindowState = FormWindowState.Minimized
End If
End If
End Sub
'Countdown Timer Subroutine
Private Sub tmrDelayCount_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDelayCount.Tick
'Display the countdown
lblCountdown.Text = show_hrs & " : " & show_mins & " : " & show_secs 'Display Countdown
'Decrement Seconds by 1
Seconds -= 1
'If the seconds is less than 0 reset to 59 and decrement the minutes
If Seconds < 0 Then
Seconds = 59
Minutes -= 1
End If
'If the minutes is less than 0 reset to 59 and decrement the hours
If Minutes < 0 Then
Minutes = 59
Hours -= 1
End If
show_hrs = CStr(Hours)
show_mins = CStr(Minutes)
show_secs = CStr(Seconds)
'Display a '0' infront of the string when less than 10 eg '06'
If Seconds < 10 Then
show_secs = "0" + show_secs
End If
If Minutes < 10 Then
show_mins = "0" + show_mins
End If
If Hours < 10 Then
show_hrs = "0" + show_hrs
End If
'When Countdown reaches 00:00:00
If Hours = 0 And Minutes = 0 And Seconds = 0 Then
Call Shutdown_Restart()
End If
End Sub
Private Sub tmrCheckTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrCheckTime.Tick
CurHour = Hour(Now)
CurMinute = Minute(Now)
If CurHour = Hours And CurMinute = Minutes Then
Call Shutdown_Restart()
End If
End Sub
Private Sub Shutdown_Restart()
'If the user is shutting down on timer
If DelayOrTime = 1 Then
'Shutdown PC
If radShutdown_d.Checked Then
System.Diagnostics.Process.Start("Shutdown", "/s")
tmrDelayCount.Enabled = False
'Restart PC
ElseIf radRestart_d.Checked Then
System.Diagnostics.Process.Start("Shutdown", "/r")
tmrDelayCount.Enabled = False
End If
'If the user is shutting down on certain time
ElseIf DelayOrTime = 2 Then
'Shutdown PC
If radShutdown_t.Checked Then
System.Diagnostics.Process.Start("Shutdown", "/s")
tmrCheckTime.Enabled = False
'Restart PC
ElseIf radRestart_t.Checked Then
System.Diagnostics.Process.Start("Shutdown", "/r")
tmrCheckTime.Enabled = False
End If
End If
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
tmrCheckTime.Enabled = False
tmrDelayCount.Enabled = False
tbcTabs.Enabled = True
btnCancel.Enabled = False
If tmrCheckTime.Enabled = False And tmrDelayCount.Enabled = False Then
MsgBox("Shutdown procedure was successfully cancelled", MsgBoxStyle.Information, "Shutdown Cancelled")
Else
MsgBox("Shutdown procedure was not cancelled!" & vbNewLine & "To stop the timer:" & vbNewLine &
" - Open Task Manager" & vbNewLine &
" - Under the 'Processes tab find the process called: WindowsApplication1.exe" & vbNewLine &
" - Select the process and press the Delete key. CLick OK to the next dialog",
MsgBoxStyle.Exclamation, "Cancellation Failed")
End If
End Sub
End Class
Here are also a couple of screenshots so you get the idea: