﻿Option Strict Off
Option Explicit On

Public Class Comp
    Dim Ret As Int32                                         ' 戻り値
    Dim szError As New System.Text.StringBuilder("", 256)    ' エラー文字列
    Dim szReturnCode As New String("", 256)                  ' 表示エラー文字列
    Dim szText As New String("", 100)                        ' テンポラリ文字列
    Dim m_Comp(7) As TextBox                                 ' 比較値コントロール

    Dim CompValue(7) As Int32                                ' 比較値
    Dim PulseWidth As Int32                                  ' パルス長

    '---------------------------------------------------------
    ' ダイアログ初期化
    '---------------------------------------------------------
    Private Sub Comp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '-----------------------------------
        ' 画面アイテムの初期化
        '-----------------------------------
        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
        '-----------------------------------
        ' カウント一致パルスの取得と表示
        '-----------------------------------
        Dim PlsWidth As Int32
        Ret = CntGetPulseWidth(Id, 0, PlsWidth)
        If Ret <> CNT_ERR_SUCCESS Then
            PlsWidth = 0
        End If
        txtPulse.Text = Hex(PlsWidth)
        '-----------------------------------
        ' カウント比較値の取得と表示
        '-----------------------------------
        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

    '---------------------------------------------------------
    ' OKボタンの処理
    '---------------------------------------------------------
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim i As Short
        '-----------------------------------
        ' 画面から設定値取得
        '-----------------------------------
        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)
        '-----------------------------------
        ' 各チャネル毎に
        '-----------------------------------
        For i = 0 To (ChannelNum - 1)
            '-----------------------------------
            ' 比較値設定
            '-----------------------------------
            Ret = CntSetCountMatchEvent(Id, i, 0, CompValue(i), 1)
            '-----------------------------
            ' エラー処理
            '-----------------------------
            If Ret <> CNT_ERR_SUCCESS Then
                CntGetErrorString(Ret, szError)
                szReturnCode = "CntSetCountMatchEvent : " & szError.ToString()
                MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "エラー")
                Exit Sub
            End If
            '-----------------------------------
            ' パルス長設定
            '-----------------------------------
            Ret = CntSetPulseWidth(Id, i, PulseWidth)
            '-----------------------------
            ' エラー処理
            '-----------------------------
            If Ret <> CNT_ERR_SUCCESS Then
                CntGetErrorString(Ret, szError)
                szReturnCode = "CntSetPulseWidth : " & szError.ToString()
                MsgBox(szReturnCode, MsgBoxStyle.OkOnly, "エラー")
                Exit Sub
            End If
        Next i
        '-----------------------------------
        ' パルス長設定
        '-----------------------------------
        Me.Close()

    End Sub

    '---------------------------------------------------------
    ' キャンセルボタンの処理
    '---------------------------------------------------------
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Me.Close()

    End Sub

    '---------------------------------------------------------
    ' 比較値の表示変更
    '---------------------------------------------------------
    ' 10進数
    Private Sub rdoComp0_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rdoComp0.MouseDown

        '-----------------------------------
        ' 画面情報を更新
        '-----------------------------------
        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

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

        '-----------------------------------
        ' 画面情報を更新
        '-----------------------------------
        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