Public Class Form1 Private Sub btnExe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExe.Click Dim time As Integer Dim pc As New System.Diagnostics.Process ' 必要な変数を宣言する Dim dtNow As DateTime = DateTime.Now Dim dtShutdown As DateTime = DateTime.Now ' テキストボックスの文字を黒にする changeFontColorBlack("hour") changeFontColorBlack("min") changeFontColorBlack("sec") ' 空白チェック If txtHour.Text = "" Then txtHour.Text = 0 End If If txtMin.Text = "" Then txtMin.Text = 0 End If If txtSec.Text = "" Then txtSec.Text = 0 End If ' 入力範囲チェック If Not (checkhourFormat(txtHour.Text) = True And checkMinFormat(txtMin.Text) = True And checkSecFormat(txtSec.Text) = True) Then Exit Sub End If ' 秒換算 Try time = Integer.Parse(txtHour.Text) * 3600 + Integer.Parse(txtMin.Text) * 60 + Integer.Parse(txtSec.Text) Catch ex As Exception Exit Sub End Try ' 実行コマンド生成 With pc.StartInfo .WindowStyle = ProcessWindowStyle.Normal .UseShellExecute = True .FileName = "SHUTDOWN" .Arguments = "-s -t " & time.ToString End With ' シャットダウン開始時刻の計算 Dim tsShutdown As New TimeSpan(txtHour.Text, txtMin.Text, txtSec.Text) dtShutdown += tsShutdown ' プロセスの実行 pc.Start() Threading.Thread.Sleep(1000) ' コマンド実行時刻とシャットダウン開始時刻の表示 lbMsg.Text = dtNow & "から" & vbCr _ & txtHour.Text & "時間" & txtMin.Text & "分" & txtSec.Text & "秒後の" & vbCr lbMsg.Text += dtShutdown & "にシャットダウンされます" ' プロセスの解放 pc.Dispose() ' キャンセルボタンの表示 btnCancel.Visible = True Exit Sub End Sub ' 時間の範囲チェック Function checkhourFormat(ByVal hour) As Boolean Try If (Integer.Parse(hour) < 0) Then MsgBox("時間は0以上の範囲で入力してください") changeFontColorRed("hour") Return False Else Return True End If Catch ex As Exception Debug.Print("時間が数値でありません") changeFontColorRed("hour") Return False End Try End Function ' 分の範囲チェック Function checkMinFormat(ByVal min) As Boolean Try If ((Integer.Parse(min) > 59) Or (Integer.Parse(min) < 0)) Then MsgBox("分は0〜59の範囲で入力してください") changeFontColorRed("min") Return False Else Return True End If Catch ex As Exception Debug.Print("分が数値でありません") changeFontColorRed("min") Return False End Try End Function ' 秒の範囲チェック Function checkSecFormat(ByVal sec) As Boolean Try If ((Integer.Parse(sec) > 59) Or (Integer.Parse(sec) < 0)) Then MsgBox("秒は0〜59の範囲で入力してください") changeFontColorRed("sec") Return False Else Return True End If Catch ex As Exception Debug.Print("秒が数値でありません") changeFontColorRed("sec") Return False End Try End Function Sub changeFontColorRed(ByVal param As String) If param = "hour" Then txtHour.ForeColor = Color.Red End If If param = "min" Then txtMin.ForeColor = Color.Red End If If param = "sec" Then txtSec.ForeColor = Color.Red End If End Sub Sub changeFontColorBlack(ByVal param As String) If param = "hour" Then txtHour.ForeColor = Color.Black End If If param = "min" Then txtMin.ForeColor = Color.Black End If If param = "sec" Then txtSec.ForeColor = Color.Black End If End Sub ' フォームロード時の処理 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load btnCancel.Visible = False changeFontColorBlack("hour") changeFontColorBlack("min") changeFontColorBlack("sec") End Sub ' キャンセルボタンクリック時の処理 Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click ' コマンド生成と実行 Dim pc As New System.Diagnostics.Process With pc.StartInfo .WindowStyle = ProcessWindowStyle.Normal .UseShellExecute = True .FileName = "SHUTDOWN" .Arguments = "-a" End With pc.Start() Threading.Thread.Sleep(1000) pc.Dispose() ' 開始時刻メッセージを削除 lbMsg.Text = "" ' キャンセルボタンを非表示 btnCancel.Visible = False End Sub Private Sub txtHour_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtHour.GotFocus changeFontColorBlack("hour") End Sub Private Sub txtMin_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMin.GotFocus changeFontColorBlack("min") End Sub Private Sub txtSec_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSec.GotFocus changeFontColorBlack("sec") End Sub End Class