Imports System.Threading

Public Class Form_FocusAGF

    Dim Cmd As String = ""
    Dim Rsp As String = "No responses received"
    Dim WithEvents KeyControl As New KeyButton()
    Dim InOutCount As Integer = 0
    Dim InOutCount2 As Integer = 0
    Dim IOrealCount, IOrealCount2 As Integer
    Dim FocusTime, FocusTime2 As Integer
    Dim NormColor, ActiveColor As Color
    Dim Ding As Boolean
    Dim TimeStartF, TimeEndF, TimeStartF2, TimeEndF2 As Single
    Dim ProgI, ProgO, ProgI2, ProgO2 As Single
    Dim ReadDelay As Integer = 200
    Dim PrevSerialPort As String = ""
    Dim KeyHeld As Boolean = False
    Dim IgnoreKey As Boolean = False

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            Const CS_DBLCLKS As Int32 = &H8
            Const CS_NOCLOSE As Int32 = &H200
            cp.ClassStyle = CS_DBLCLKS Or CS_NOCLOSE
            Return cp
        End Get
    End Property


    Private Sub Form_FocusAGF_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim Kx, Ky, Kw, Kh As Integer
        Dim Kt As String

        ' Make sure form is on the screen
        If Me.Location.X < 0 Or Me.Location.Y < 0 Then
            Me.Location = New Point(0, 0)
        End If

        ' Create button for keyboard control using arrows
        Kx = ButtonKeyboardDummy.Location.X
        Ky = ButtonKeyboardDummy.Location.Y
        Kw = ButtonKeyboardDummy.Size.Width
        Kh = ButtonKeyboardDummy.Size.Height
        Kt = ButtonKeyboardDummy.Text

        KeyControl.Size = New Size(Kw, Kh)
        KeyControl.Location = New Point(Kx, Ky)
        KeyControl.Text = New String(Kt)
        Me.Controls.Add(KeyControl)
        ToolTip1.SetToolTip(KeyControl, "Click to activate keyboard control")
        KeyControl.BringToFront()

        NormColor = Form1.NormColor
        ActiveColor = Form1.ActiveColor

        TabPage1.Enabled = False
        TabPage2.Enabled = False

        ' lock count text boxes
        TextBoxIOcount.ReadOnly = True
        TextBoxIOcount2.ReadOnly = True

        ' set last command string
        Cmd = "No commands sent"

    End Sub

    Private Sub Form_FocusAGF_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Form1.FocusAgfW = Me.Width
        Form1.FocusAgfH = Me.Height

    End Sub

    Private Sub ButtonClose_Click(sender As System.Object, e As System.EventArgs) Handles ButtonClose.Click
        Form1.FocusAgfW = Me.Width
        Form1.FocusAgfH = Me.Height
        Me.Hide()

    End Sub

    Private Sub ButtonConnect_Click(sender As System.Object, e As System.EventArgs) Handles ButtonConnect.Click
        Dim Purge As String
        Dim ComPort As String

        ComPort = Trim(Microsoft.VisualBasic.Left(ToolStripComboBoxCom.Items(ToolStripComboBoxCom.SelectedIndex), 6))
        If SerialPort1.IsOpen = False Then
            SerialPort1.PortName = ComPort
            TextBox1.SelectedText = "Opening " & SerialPort1.PortName & vbCrLf
            Application.DoEvents()
            On Error GoTo Open_Error
            SerialPort1.Open()
            Purge = SerialPort1.ReadExisting()
            ButtonConnect.Text = "Disconnect"
            TextBox1.SelectedText = "Connected " & SerialPort1.PortName & vbCrLf
            TabPage1.Enabled = True
            TabPage2.Enabled = True
            If SerialPort1.PortName <> PrevSerialPort Then
                ZeroCounterFocus()
                ZeroCounterFocus2()
            End If
            SendCmdResp(":V#")

        Else
            StopAny()
            PrevSerialPort = SerialPort1.PortName
            SerialPort1.Close()
            ButtonConnect.Text = "Connect"
            TextBox1.SelectedText = "Disconnected " & SerialPort1.PortName & vbCrLf
            TabPage1.Enabled = False
            TabPage2.Enabled = False
        End If
        Exit Sub

Open_Error:
        TextBox1.SelectedText = Err.Description & vbCrLf


    End Sub

    Private Sub AGF_Command(ByVal command As String)
        Dim APcmd As String
        Dim DirIn1 As String = "-"
        Dim DirIn2 As String = "-"
        Dim DirOut1 As String = "+"
        Dim DirOut2 As String = "+"
        If CheckBoxRev1.Checked = True Then
            DirIn1 = "+"
            DirOut1 = "-"
        End If
        If CheckBoxRev2.Checked = True Then
            DirIn2 = "+"
            DirOut2 = "-"
        End If

        If SerialPort1.IsOpen = False Then
            ButtonConnect.Text = "Removed"
            TextBox1.SelectedText = "COM port removed, must exit." & vbCrLf
            Exit Sub
        End If

        Cmd = "#"
        APcmd = ""
        Select Case command

            Case "In"
                Cmd = ":F" & DirIn1 & "#"
                ButtonOut.BackColor = NormColor
                ButtonIn.BackColor = ActiveColor
            Case "InS"
                Cmd = ":F" & DirIn1 & Format(NumericUpDownFocus.Value, "0000") & "#"
                ButtonOut.BackColor = NormColor
                ButtonIn.BackColor = ActiveColor
            Case "Out"
                Cmd = ":F" & DirOut1 & "#"
                ButtonIn.BackColor = NormColor
                ButtonOut.BackColor = ActiveColor
            Case "OutS"
                Cmd = ":F" & DirOut1 & Format(NumericUpDownFocus.Value, "0000") & "#"
                ButtonIn.BackColor = NormColor
                ButtonOut.BackColor = ActiveColor
            Case "StopF"
                Cmd = ":FQ#"
                ButtonIn.BackColor = NormColor
                ButtonOut.BackColor = NormColor

            Case "In2"
                Cmd = ":O" & DirIn2 & "#"
                ButtonOut2.BackColor = NormColor
                ButtonIn2.BackColor = ActiveColor
            Case "In2S"
                Cmd = ":O" & DirIn2 & Format(NumericUpDownFocus2.Value, "0000") & "#"
                ButtonOut2.BackColor = NormColor
                ButtonIn2.BackColor = ActiveColor
            Case "Out2"
                Cmd = ":O" & DirOut2 & "#"
                ButtonIn2.BackColor = NormColor
                ButtonOut2.BackColor = ActiveColor
            Case "Out2S"
                Cmd = ":O" & DirOut2 & Format(NumericUpDownFocus2.Value, "0000") & "#"
                ButtonIn2.BackColor = NormColor
                ButtonOut2.BackColor = ActiveColor
            Case "StopF2"
                Cmd = ":OQ#"
                ButtonIn2.BackColor = NormColor
                ButtonOut2.BackColor = NormColor

        End Select

        SerialPort1.Write(Cmd)
        Thread.Sleep(ReadDelay)
        Rsp = SerialPort1.ReadExisting()

        If ShowCommandsSentToolStripMenuItem.Checked = True Then
            APcmd = Cmd & " "
        End If
        TextBox1.SelectedText = "AGF --> " & APcmd & command & vbCrLf

        If Len(Rsp) > 0 Then
            TextBox1.SelectedText = "AGF <-- " & Rsp & vbCrLf
        End If

    End Sub

    Private Sub SendCmdResp(ByVal Cmd As String)
        Dim Status As String = ""

        If SerialPort1.IsOpen = True Then
            Status = ""
            SerialPort1.Write(Cmd)
            Thread.Sleep(ReadDelay)
            Rsp = SerialPort1.ReadExisting()
            If Len(Rsp) > 0 Then
                TextBox1.SelectedText = Rsp & vbCrLf
            Else
                TextBox1.SelectedText = "No response" & vbCrLf
            End If
        Else
            TextBox1.SelectedText = "Not connected" & vbCrLf
        End If

    End Sub


    Private Sub StopAny()
        If TimerI.Enabled = True Or TimerO.Enabled = True Then
            ButtonFocusStop.PerformClick()
        End If
        If TimerI2.Enabled = True Or TimerO2.Enabled = True Then
            ButtonFocusStop2.PerformClick()
        End If

    End Sub


    ' Focus Motor 1 Section

    Private Sub TimerI_Tick(sender As System.Object, e As System.EventArgs) Handles TimerI.Tick
        TimerI.Enabled = False
        TimerProgI.Enabled = False
        AGF_Command("StopF")
        InOutCount = IOrealCount
        UpdateFocuscount()
        If Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub TimerO_Tick(sender As System.Object, e As System.EventArgs) Handles TimerO.Tick
        TimerO.Enabled = False
        TimerProgO.Enabled = False
        AGF_Command("StopF")
        InOutCount = IOrealCount
        UpdateFocuscount()
        If Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub TimerProgI_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerProgI.Tick
        If ProgI <= 0 Then
            TimerProgI.Enabled = False
            InOutCount = InOutCount + FocusTime
        End If
        InOutCount = InOutCount - TimerProgI.Interval
        UpdateFocuscount()
        ProgI = ProgI - 1

    End Sub

    Private Sub TimerProgO_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerProgO.Tick
        If ProgO <= 0 Then
            TimerProgO.Enabled = False
            InOutCount = InOutCount - FocusTime
        End If
        InOutCount = InOutCount + TimerProgO.Interval
        UpdateFocuscount()
        ProgO = ProgO - 1

    End Sub

    Private Sub TimerHomeI_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerHomeI.Tick
        TimerHomeI.Enabled = False
        TimerProgI.Enabled = False
        AGF_Command("StopF")
        InOutCount = 0
        UpdateFocuscount()
        If Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub TimerHomeO_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerHomeO.Tick
        TimerHomeO.Enabled = False
        TimerProgO.Enabled = False
        AGF_Command("StopF")
        InOutCount = 0
        UpdateFocuscount()
        If Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub ZeroCounterFocus()
        InOutCount = 0
        TextBoxIOcount.Text = InOutCount

    End Sub

    Private Sub UpdateFocuscount()
        If InOutCount < 0 Then
            TextBoxIOcount.Text = InOutCount & " In"
        Else
            TextBoxIOcount.Text = InOutCount & " Out"
        End If
    End Sub


    ' Focus motor 2 section

    Private Sub TimerI2_Tick(sender As System.Object, e As System.EventArgs) Handles TimerI2.Tick
        TimerI2.Enabled = False
        TimerProgI2.Enabled = False
        AGF_Command("StopF2")
        InOutCount2 = IOrealCount2
        UpdateFocuscount2()
        If Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub TimerO2_Tick(sender As System.Object, e As System.EventArgs) Handles TimerO2.Tick
        TimerO2.Enabled = False
        TimerProgO2.Enabled = False
        AGF_Command("StopF2")
        InOutCount2 = IOrealCount2
        UpdateFocuscount2()
        If Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub TimerProgI2_Tick(sender As System.Object, e As System.EventArgs) Handles TimerProgI2.Tick
        If ProgI2 <= 0 Then
            TimerProgI2.Enabled = False
            InOutCount2 = InOutCount2 + FocusTime2
        End If
        InOutCount2 = InOutCount2 - TimerProgI2.Interval
        UpdateFocuscount2()
        ProgI2 = ProgI2 - 1

    End Sub

    Private Sub TimerProgO2_Tick(sender As System.Object, e As System.EventArgs) Handles TimerProgO2.Tick
        If ProgO2 <= 0 Then
            TimerProgO2.Enabled = False
            InOutCount2 = InOutCount2 - FocusTime2
        End If
        InOutCount2 = InOutCount2 + TimerProgO2.Interval
        UpdateFocuscount2()
        ProgO2 = ProgO2 - 1

    End Sub

    Private Sub TimerHomeI2_Tick(sender As System.Object, e As System.EventArgs) Handles TimerHomeI2.Tick
        TimerHomeI2.Enabled = False
        TimerProgI2.Enabled = False
        AGF_Command("StopF2")
        InOutCount2 = 0
        UpdateFocuscount2()
        If Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub TimerHomeO2_Tick(sender As System.Object, e As System.EventArgs) Handles TimerHomeO2.Tick
        TimerHomeO2.Enabled = False
        TimerProgO2.Enabled = False
        AGF_Command("StopF2")
        InOutCount2 = 0
        UpdateFocuscount2()
        If Ding = True Then
            My.Computer.Audio.Play(My.Resources.ding, AudioPlayMode.WaitToComplete)
        End If

    End Sub

    Private Sub ZeroCounterFocus2()
        InOutCount2 = 0
        TextBoxIOcount2.Text = InOutCount2

    End Sub

    Private Sub UpdateFocuscount2()
        If InOutCount2 < 0 Then
            TextBoxIOcount2.Text = InOutCount2 & " In"
        Else
            TextBoxIOcount2.Text = InOutCount2 & " Out"
        End If
    End Sub



    Private Function GetLongestItemWidth(ByVal combo As ToolStripComboBox) As Integer
        Dim longest As Integer
        Dim str As String
        Dim g As Graphics = Graphics.FromImage(New Bitmap(1, 1))

        'Enumerate the items in the combo
        For Each str In combo.Items
            Dim wid As Integer

            'Get the width of the item; if it's longer than the
            'longest one so far, store it.
            wid = g.MeasureString(str, combo.Font).Width
            If wid > longest Then longest = wid
        Next
        If combo.Items.Count > combo.MaxDropDownItems Then
            longest = longest + SystemInformation.VerticalScrollBarWidth
        End If

        Return longest
    End Function



    Private Sub ButtonIn_Click(sender As System.Object, e As System.EventArgs) Handles ButtonIn.Click
        If CheckBoxFocusTime.Checked = True Then
            If TimerO.Enabled = True Then
                AGF_Command("StopF")
            End If
            FocusTime = NumericUpDownFocus.Value
            IOrealCount = InOutCount - FocusTime
            If FocusTime < 10000 Then
                AGF_Command("InS")
            Else
                AGF_Command("In")
            End If
            TimerI.Interval = FocusTime
            TimerI.Enabled = True
            TimerO.Enabled = False
            ProgI = (FocusTime / TimerProgI.Interval) - 1
            TimerProgI.Enabled = True
        End If

    End Sub

    Private Sub ButtonIn_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ButtonIn.MouseDown
        If CheckBoxFocusTime.Checked = False Then
            ProgIstart()
        End If

    End Sub

    Private Sub ButtonIn_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ButtonIn.MouseUp
        If CheckBoxFocusTime.Checked = False Then
            ProgIend()
        End If

    End Sub

    Private Sub ButtonOut_Click(sender As System.Object, e As System.EventArgs) Handles ButtonOut.Click
        If CheckBoxFocusTime.Checked = True Then
            If TimerI.Enabled = True Then
                AGF_Command("StopF")
            End If
            FocusTime = NumericUpDownFocus.Value
            IOrealCount = InOutCount + FocusTime
            If FocusTime < 10000 Then
                AGF_Command("OutS")
            Else
                AGF_Command("Out")
            End If
            TimerO.Interval = FocusTime
            TimerO.Enabled = True
            TimerI.Enabled = False
            ProgO = (FocusTime / TimerProgO.Interval) - 1
            TimerProgO.Enabled = True
        End If

    End Sub

    Private Sub ButtonOut_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ButtonOut.MouseDown
        If CheckBoxFocusTime.Checked = False Then
            ProgOstart()
        End If

    End Sub

    Private Sub ButtonOut_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ButtonOut.MouseUp
        If CheckBoxFocusTime.Checked = False Then
            ProgOend()
        End If

    End Sub

    Private Sub ButtonFocusStop_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusStop.Click
        TimerI.Enabled = False
        TimerO.Enabled = False
        TimerHomeI.Enabled = False
        TimerHomeO.Enabled = False
        TimerProgI.Enabled = False
        TimerProgO.Enabled = False
        AGF_Command("StopF")

    End Sub

    Private Sub ProgIstart()
        AGF_Command("In")
        TimeStartF = Microsoft.VisualBasic.DateAndTime.Timer
        IOrealCount = InOutCount
        ProgI = 1000000
        TimerProgI.Enabled = True

    End Sub

    Private Sub ProgIend()
        AGF_Command("StopF")
        TimeEndF = Microsoft.VisualBasic.DateAndTime.Timer
        InOutCount = IOrealCount - (1000 * (TimeEndF - TimeStartF))
        TimerProgI.Enabled = False
        UpdateFocuscount()

    End Sub
    Private Sub ProgOstart()
        AGF_Command("Out")
        TimeStartF = Microsoft.VisualBasic.DateAndTime.Timer
        IOrealCount = InOutCount
        ProgO = 1000000
        TimerProgO.Enabled = True

    End Sub

    Private Sub ProgOend()
        AGF_Command("StopF")
        TimeEndF = Microsoft.VisualBasic.DateAndTime.Timer
        InOutCount = IOrealCount + (1000 * (TimeEndF - TimeStartF))
        TimerProgO.Enabled = False
        UpdateFocuscount()

    End Sub


    Private Sub ButtonIn2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonIn2.Click
        If CheckBoxfocusTime2.Checked = True Then
            If TimerO2.Enabled = True Then
                AGF_Command("StopF2")
            End If
            FocusTime2 = NumericUpDownFocus2.Value
            IOrealCount2 = InOutCount2 - FocusTime2
            If FocusTime2 < 10000 Then
                AGF_Command("In2S")
            Else
                AGF_Command("In2")
            End If
            TimerI2.Interval = FocusTime2
            TimerI2.Enabled = True
            TimerO2.Enabled = False
            ProgI2 = (FocusTime2 / TimerProgI2.Interval) - 1
            TimerProgI2.Enabled = True
        End If

    End Sub

    Private Sub ButtonIn2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ButtonIn2.MouseDown
        If CheckBoxfocusTime2.Checked = False Then
            ProgIstart2()
        End If

    End Sub

    Private Sub ButtonIn2_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ButtonIn2.MouseUp
        If CheckBoxfocusTime2.Checked = False Then
            ProgIend2()
        End If

    End Sub

    Private Sub ButtonOut2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonOut2.Click
        If CheckBoxfocusTime2.Checked = True Then
            If TimerI2.Enabled = True Then
                AGF_Command("StopF2")
            End If
            FocusTime2 = NumericUpDownFocus2.Value
            IOrealCount2 = InOutCount2 + FocusTime2
            If FocusTime2 < 10000 Then
                AGF_Command("Out2S")
            Else
                AGF_Command("Out2")
            End If
            TimerO2.Interval = FocusTime2
            TimerO2.Enabled = True
            TimerI2.Enabled = False
            ProgO2 = (FocusTime2 / TimerProgO2.Interval) - 1
            TimerProgO2.Enabled = True
        End If

    End Sub
    Private Sub ButtonOut2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ButtonOut2.MouseDown
        If CheckBoxfocusTime2.Checked = False Then
            ProgOstart2()
        End If

    End Sub

    Private Sub ButtonOut2_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ButtonOut2.MouseUp
        If CheckBoxfocusTime2.Checked = False Then
            ProgOend2()
        End If

    End Sub

    Private Sub ProgIstart2()
        AGF_Command("In2")
        TimeStartF2 = Microsoft.VisualBasic.DateAndTime.Timer
        IOrealCount2 = InOutCount2
        ProgI2 = 1000000
        TimerProgI2.Enabled = True

    End Sub

    Private Sub ProgIend2()
        AGF_Command("StopF2")
        TimeEndF2 = Microsoft.VisualBasic.DateAndTime.Timer
        InOutCount2 = IOrealCount2 - (1000 * (TimeEndF2 - TimeStartF2))
        TimerProgI2.Enabled = False
        UpdateFocuscount2()

    End Sub
    Private Sub ProgOstart2()
        AGF_Command("Out2")
        TimeStartF2 = Microsoft.VisualBasic.DateAndTime.Timer
        IOrealCount2 = InOutCount2
        ProgO2 = 1000000
        TimerProgO2.Enabled = True

    End Sub

    Private Sub ProgOend2()
        AGF_Command("StopF2")
        TimeEndF2 = Microsoft.VisualBasic.DateAndTime.Timer
        InOutCount2 = IOrealCount2 + (1000 * (TimeEndF2 - TimeStartF2))
        TimerProgO2.Enabled = False
        UpdateFocuscount2()

    End Sub

    Private Sub ButtonFocusStop2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusStop2.Click
        TimerI2.Enabled = False
        TimerO2.Enabled = False
        TimerHomeI2.Enabled = False
        TimerHomeO2.Enabled = False
        TimerProgI2.Enabled = False
        TimerProgO2.Enabled = False
        AGF_Command("StopF2")

    End Sub


    Private Sub ButtonHomeF_Click(sender As System.Object, e As System.EventArgs) Handles ButtonHomeF.Click
        If InOutCount > 0 Then
            TimerHomeI.Interval = InOutCount
            AGF_Command("In")
            TimerHomeI.Enabled = True
            ProgI = (TimerHomeI.Interval / TimerProgI.Interval) - 1
            TimerProgI.Enabled = True
        End If
        If InOutCount < 0 Then
            TimerHomeO.Interval = InOutCount * -1
            AGF_Command("Out")
            TimerHomeO.Enabled = True
            ProgO = (TimerHomeO.Interval / TimerProgO.Interval) - 1
            TimerProgO.Enabled = True
        End If

    End Sub

    Private Sub ButtonHomeF2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonHomeF2.Click
        If InOutCount2 > 0 Then
            TimerHomeI2.Interval = InOutCount2
            AGF_Command("In2")
            TimerHomeI2.Enabled = True
            ProgI2 = (TimerHomeI2.Interval / TimerProgI2.Interval) - 1
            TimerProgI2.Enabled = True
        End If
        If InOutCount2 < 0 Then
            TimerHomeO2.Interval = InOutCount2 * -1
            AGF_Command("Out2")
            TimerHomeO2.Enabled = True
            ProgO2 = (TimerHomeO2.Interval / TimerProgO2.Interval) - 1
            TimerProgO2.Enabled = True
        End If

    End Sub


    Private Sub ButtonClearFocus_Click(sender As System.Object, e As System.EventArgs) Handles ButtonClearFocus.Click
        ZeroCounterFocus()

    End Sub

    Private Sub ButtonClearFocus2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonClearFocus2.Click
        ZeroCounterFocus2()

    End Sub

    Private Sub ClearLoggingToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ClearLoggingToolStripMenuItem.Click
        TextBox1.Text = ""

    End Sub

    Private Sub ServoControllerVersionToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ServoControllerVersionToolStripMenuItem.Click
        SendCmdResp(":V#")

    End Sub

    Private Sub InitializeResetControllerToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles InitializeResetControllerToolStripMenuItem.Click
        SendCmdResp(":I#")
        TimerI.Enabled = False
        TimerO.Enabled = False
        TimerHomeI.Enabled = False
        TimerHomeO.Enabled = False
        TimerProgI.Enabled = False
        TimerProgO.Enabled = False
        TimerI2.Enabled = False
        TimerO2.Enabled = False
        TimerHomeI2.Enabled = False
        TimerHomeO2.Enabled = False
        TimerProgI2.Enabled = False
        TimerProgO2.Enabled = False
        ButtonIn.BackColor = NormColor
        ButtonOut.BackColor = NormColor
        ButtonIn2.BackColor = NormColor
        ButtonOut2.BackColor = NormColor
        TextBox1.SelectedText = "Guide & Focus stopped" & vbCrLf

    End Sub

    Private Sub ShowCommandsSentToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ShowCommandsSentToolStripMenuItem.Click
        If ShowCommandsSentToolStripMenuItem.Checked = False Then
            ShowCommandsSentToolStripMenuItem.Checked = True
        Else
            ShowCommandsSentToolStripMenuItem.Checked = False
        End If

    End Sub

    Private Sub EnableControllerDebugToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EnableControllerDebugToolStripMenuItem.Click
        If EnableControllerDebugToolStripMenuItem.Checked = False Then
            SendCmdResp(":D1#")
            EnableControllerDebugToolStripMenuItem.Checked = True
        Else
            SendCmdResp(":D0#")
            EnableControllerDebugToolStripMenuItem.Checked = False
        End If

    End Sub

    Private Sub TimedBeepToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles TimedBeepToolStripMenuItem.Click
        If TimedBeepToolStripMenuItem.Checked = False Then
            TimedBeepToolStripMenuItem.Checked = True
            Ding = True
        Else
            TimedBeepToolStripMenuItem.Checked = False
            Ding = False
        End If

    End Sub

    Private Sub KeyControl_GotFocus(sender As Object, e As System.EventArgs) Handles KeyControl.GotFocus
        KeyControl.BackColor = ActiveColor
        LabelKey.Visible = True

    End Sub

    Private Sub KeyControl_LostFocus(sender As Object, e As System.EventArgs) Handles KeyControl.LostFocus
        KeyControl.BackColor = Color.Transparent
        LabelKey.Visible = False

    End Sub

    Private Sub KeyControl_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles KeyControl.KeyDown
        Dim K As String

        If IgnoreKey = False Then
            If KeyHeld = False Then
                K = e.KeyCode
                If K = Keys.Menu Then
                    IgnoreKey = True
                Else
                    IgnoreKey = False
                    Select Case TabControl1.SelectedIndex
                        Case 0
                            ' Motor 1
                            If CheckBoxFocusTime.Checked = False Then
                                Select Case K
                                    Case Keys.PageUp, Keys.NumPad4, Keys.OemOpenBrackets
                                        ProgIstart()
                                    Case Keys.PageDown, Keys.NumPad6, Keys.OemCloseBrackets
                                        ProgOstart()
                                End Select
                            End If

                        Case 1
                            ' Motor 2
                            If CheckBoxfocusTime2.Checked = False Then
                                Select Case K
                                    Case Keys.PageUp, Keys.NumPad4, Keys.OemOpenBrackets
                                        ProgIstart2()
                                    Case Keys.PageDown, Keys.NumPad6, Keys.OemCloseBrackets
                                        ProgOstart2()
                                End Select
                            End If

                    End Select

                    If Form1.GroupBox1.Enabled = True Then
                        ' Mount movement
                        If Form1.CheckBoxSlewTime.Checked = False Then
                            ' Mount movement
                            Select Case K
                                Case Keys.Up, Keys.NumPad5
                                    Form1.ProgNstart()
                                Case Keys.Down, Keys.NumPad2
                                    Form1.ProgSstart()
                                Case Keys.Right, Keys.NumPad3
                                    Form1.ProgEstart()
                                Case Keys.Left, Keys.NumPad1
                                    Form1.ProgWstart()
                            End Select
                        End If
                    End If

                    KeyHeld = True
                    End If
            End If
        End If

    End Sub

    Private Sub KeyControl_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles KeyControl.KeyUp
        Dim K As String = ""

        K = e.KeyCode
        If IgnoreKey = False Then
            Select Case TabControl1.SelectedIndex
                Case 0
                    ' Motor 1
                    Select Case K
                        Case Keys.PageUp, Keys.NumPad4, Keys.OemOpenBrackets
                            If CheckBoxFocusTime.Checked = True Then
                                ButtonIn.PerformClick()
                            Else
                                ProgIend()
                            End If
                        Case Keys.PageDown, Keys.NumPad6, Keys.OemCloseBrackets
                            If CheckBoxFocusTime.Checked = True Then
                                ButtonOut.PerformClick()
                            Else
                                ProgOend()
                            End If
                    End Select

                Case 1
                    ' Motor 2
                    Select Case K
                        Case Keys.PageUp, Keys.NumPad4, Keys.OemOpenBrackets
                            If CheckBoxfocusTime2.Checked = True Then
                                ButtonIn2.PerformClick()
                            Else
                                ProgIend2()
                            End If
                        Case Keys.PageDown, Keys.NumPad6, Keys.OemCloseBrackets
                            If CheckBoxFocusTime.Checked = True Then
                                ButtonOut2.PerformClick()
                            Else
                                ProgOend2()
                            End If
                    End Select

            End Select

            If Form1.GroupBox1.Enabled = True Then
                ' Mount movement
                If Form1.CheckBoxSlewTime.Checked = False Then
                    ' Mount movement
                    Select Case K
                        Case Keys.Up, Keys.NumPad5
                            Form1.ProgNend()
                        Case Keys.Down, Keys.NumPad2
                            Form1.progSend()
                        Case Keys.Right, Keys.NumPad3
                            Form1.progEend()
                        Case Keys.Left, Keys.NumPad1
                            Form1.progWend()
                    End Select
                Else
                    Select Case K
                        Case Keys.Up, Keys.NumPad5
                            Form1.ButtonN.PerformClick()
                        Case Keys.Down, Keys.NumPad2
                            Form1.ButtonS.PerformClick()
                        Case Keys.Right, Keys.NumPad3
                            Form1.ButtonE.PerformClick()
                        Case Keys.Left, Keys.NumPad1
                            Form1.ButtonW.PerformClick()
                    End Select
                End If
            End If

            KeyHeld = False
        Else
            If K = Keys.Menu Then
                IgnoreKey = False
            End If
        End If

    End Sub

    Private Sub LabelKeyHelp_Click(sender As System.Object, e As System.EventArgs) Handles LabelKeyHelp.Click
        If Form_FocusHelp.Visible = True Then
            Form_FocusHelp.Close()
        Else
            Form_FocusHelp.Show()
        End If

    End Sub
End Class