﻿Option Strict Off
Option Explicit On

Public Class Comp
    Dim Ret As Int32                                    ' Return value
    Dim szError As New System.Text.StringBuilder(256)   ' Error string
    Dim szReturnCode As New String("", 256)             ' The string for error
    Dim szText As New String("", 100)                   ' Temporary string
    Dim m_Comp(7) As TextBox                            ' Controls of comparison value

    Dim CompValue(7) As Int32                           ' Comparison value
    Dim PulseWidth As Int32                             ' Pulse width

    '---------------------------------------------------------
    ' Initialize dialog
    '---------------------------------------------------------
    Private Sub Comp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '-----------------------------------
        ' Initialize screen items
        '-----------------------------------
        Dim i As Short
        rdoComp0.Checked = True
        m_Comp(0) = txtComp0
        m_Comp(1) = txtComp1
        m_Comp(2) = txtComp2
        m_Comp(3) = txtComp3
        m_Comp(4) = txtComp4
        m_Comp(5) = txtComp5
        m_Comp(6) = txtComp6
        m_Comp(7) = txtComp7
        For i = 0 To 7
            If i >= ChannelNum Then
                m_Comp(i).Enabled = False
            End If
        Next i
        '-----------------------------------
        ' Get and display the count-match pulse
        '-----------------------------------
        Dim PlsWidth As Integer
        Ret = CntGetPulseWidth(Id, 0, PlsWidth)
        If Ret <> CNT_ERR_SUCCESS Then
            PlsWidth = 0
        End If
        txtPulse.Text = Hex(PlsWidth)
        '-----------------------------------
        ' Get and display count comparison value
        '-----------------------------------
        For i = 0 To (ChannelNum - 1)
            CompValue(i) = 0
            Ret = CntGetCountMatchReg(Id, i, 0, CompValue(i))
            m_Comp(i).Text = String.Format("{0:d}", CompValue(i))
        Next i

    End Sub

    '---------------------------------------------------------
    ' Process OK button
    '---------------------------------------------------------
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim i As Short
        '-----------------------------------
        ' Get setting value from screen
        '-----------------------------------
        For i = 0 To (ChannelNum - 1)
            szText = m_Comp(i).Text
            Try
                If rdoComp0.Checked = True Then
                    CompValue(i) = Integer.Parse(szText)
                Else
                    CompValue(i) = Integer.Parse(szText, System.Globalization.NumberStyles.HexNumber)
                End If
            Catch ex As Exception
                CompValue(i) = 0
            End Try
        Next i
        szText = txtPulse.Text
        PulseWidth = Integer.Parse(szText, System.Globalization.NumberStyles.HexNumber)
        '-----------------------------------
        ' Each channel
        '-----------------------------------
        For i = 0 To (ChannelNum - 1)
            '-----------------------------------
            ' Set comparison value
            '-----------------------------------
            Ret = CntSetCountMatchEvent(Id, i, 0, CompValue(i), 1)
            '-----------------------------
            ' Process error
            '-----------------------------
            If Ret <> CNT_ERR_SUCCESS Then
                CntGetErrorString(Ret, szError)
                szReturnCode = "CntSetCountMatchEvent : " & szError.ToString()
                MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "Error")
                Exit Sub
            End If
            '-----------------------------------
            ' Set pulse width
            '-----------------------------------
            Ret = CntSetPulseWidth(Id, i, PulseWidth)
            '-----------------------------
            ' Process error
            '-----------------------------
            If Ret <> CNT_ERR_SUCCESS Then
                CntGetErrorString(Ret, szError)
                szReturnCode = "CntSetPulseWidth : " & szError.ToString()
                MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "Error")
                Exit Sub
            End If
        Next i
        '-----------------------------------
        ' Set pulse width
        '-----------------------------------
        Me.Close()

    End Sub

    '---------------------------------------------------------
    ' Process Cancel button
    '---------------------------------------------------------
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Me.Close()

    End Sub

    '---------------------------------------------------------
    ' Change the display of the comparison value
    '---------------------------------------------------------
    ' Dec
    Private Sub rdoComp0_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rdoComp0.MouseDown

        '-----------------------------------
        ' Refresh screen information
        '-----------------------------------
        Dim i As Short
        For i = 0 To 7
            szText = String.Format("{0:d}", CompValue(i))
            m_Comp(i).Text = szText
        Next i

    End Sub

    ' Hex
    Private Sub rdoComp1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rdoComp1.MouseDown

        '-----------------------------------
        ' Refresh screen information
        '-----------------------------------
        Dim i As Short
        For i = 0 To 7
            szText = String.Format("{0:X8}", CompValue(i))
            m_Comp(i).Text = szText
        Next i

    End Sub
End Class