﻿Imports System.Text

Public Class Notify
    Const CHANNEL_NUM As Short = 4

    Dim bNotifyError As Boolean = True
    Dim bNotifyCarry As Boolean = True
    Dim CompData(CHANNEL_NUM, 2) As Integer
    Dim txt_Reg0(CHANNEL_NUM) As TextBox
    Dim txt_Reg1(CHANNEL_NUM) As TextBox

    Private Sub frmNotify_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim i As Short

        '-----------------------------------
        ' Control array settings
        '-----------------------------------
        '-----------------
        ' Compare register 0
        '-----------------
        txt_Reg0(0) = txtCh0Reg0
        txt_Reg0(1) = txtCh1Reg0
        txt_Reg0(2) = txtCh2Reg0
        txt_Reg0(3) = txtCh3Reg0
         '-----------------
        ' Compare register 1
        '-----------------
        txt_Reg1(0) = txtCh0Reg1
        txt_Reg1(1) = txtCh1Reg1
        txt_Reg1(2) = txtCh2Reg1
        txt_Reg1(3) = txtCh3Reg1
        '-------------------------------
        ' Set initial data
        '-------------------------------
        For i = 0 To CHANNEL_NUM - 1
            CompData(i, 0) = (i + 1) * &H100S
            CompData(i, 1) = (i + 1) * &H100S + &H50S
        Next i

        If bNotifyError Then
            rdoError0.Checked = True
            rdoError1.Checked = False
        Else
            rdoError0.Checked = False
            rdoError1.Checked = True
        End If
        If bNotifyCarry Then
            rdoCarry0.Checked = True
            rdoCarry1.Checked = False
        Else
            rdoCarry0.Checked = False
            rdoCarry1.Checked = True
        End If
    End Sub

    Private Sub frmNotify_Actived(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated

        Dim i As Short

        '-------------------------------
        ' Toggles the compare register display according to the total number of channels.
        '-------------------------------
        For i = 0 To CHANNEL_NUM - 1
            txt_Reg0(i).Text = Hex(CompData(i, 0))
            txt_Reg1(i).Text = Hex(CompData(i, 1))
            '-------------------------------
            ' Disables the display when the number of loops exceeds the total number of channels.
            '-------------------------------
            if (i >= Form1.ChannelNum) Then
                txt_Reg0(i).Enabled = False
                txt_Reg1(i).Enabled = False
            '-------------------------------
            ' Enables the display when the number of loops is less than or equal to the total number of channels.
            '-------------------------------
            Else
                txt_Reg0(i).Enabled = True
                txt_Reg1(i).Enabled = True
            End If
        Next i
    End Sub

    '---------------------------------------------
    ' Setting completion processing
    '---------------------------------------------
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim ret As Integer
        Dim ChNo, RegNo As Short
        Dim str As String
        Dim error_string As New StringBuilder("", 256)

        '-------------------------------
        ' Get the set information
        '-------------------------------
        For ChNo = 0 To Form1.ChannelNum - 1
            ' To display the comparison value as unsigned, convert it once through hexadecimal characters
            CompData(ChNo, 0) = Convert.ToInt32(Hex(Val("&h" & txt_Reg0(ChNo).Text)), 16)
            CompData(ChNo, 1) = Convert.ToInt32(Hex(Val("&h" & txt_Reg1(ChNo).Text)), 16)
        Next ChNo

        If rdoError0.Checked = True Then
            bNotifyError = True
        Else
            bNotifyError = False
        End If
        If rdoCarry0.Checked = True Then
            bNotifyCarry = True
        Else
            bNotifyCarry = False
        End If
        '-------------------------------
        ' Set the set information to device
        '-------------------------------
        For ChNo = 0 To Form1.ChannelNum - 1
            For RegNo = 0 To 1
                '---------------------------------------------
                ' Set count match notification and compare register
                '---------------------------------------------
                ret = CntNotifyCountUp(Form1.Id, ChNo, RegNo, CompData(ChNo, RegNo), 0)
                If ret <> 0 Then
                    CntGetErrorString(ret, error_string)
                    str = String.Format("CntNotifyCountUp : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                    MsgBox(str, MsgBoxStyle.OkOnly, "Error")
                    Me.Hide()
                    Exit Sub
                End If
            Next RegNo
        Next ChNo
        If bNotifyError = True Then
            '---------------------------------------------
            ' Set counter error notificaton
            '---------------------------------------------
            ret = CntNotifyCounterError(Form1.Id, 0)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntNotifyCounterError : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
                Me.Hide()
                Exit Sub
            End If
        End If
        If bNotifyCarry = True Then
            '---------------------------------------------
            ' Set carry/borrow notification
            '---------------------------------------------
            ret = CntNotifyCarryBorrow(Form1.Id, 0)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntNotifyCarryBorrow : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
            End If
        End If
        Me.Hide()
    End Sub

    '---------------------------------------------
    ' Cancel processing
    '---------------------------------------------
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Hide()
    End Sub

    Private Sub frmNotify_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
End Class