﻿Imports System.Text

Public Class Setting
    Private Sub Setting_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim message_string As String                      ' For displaying message
        Dim index As Integer                              ' Index of combobox
        Dim cnt_mode As Short                             ' Operation mode
        Dim err_flag As Short                             ' Function execution error flag

        '----------------------------------------
        ' Initialize parameters
        '----------------------------------------
        err_flag = 0
        message_string = ""
        '----------------------------------------
        ' Set the counter operation as comparison count match occurs
        '----------------------------------------
        ret = AioGetCntComparisonMode(Form1.g_id, Form1.g_channel, cnt_mode)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            message_string = "AioGetCntComparisonMode = " & ret & " : " & error_string.ToString() & vbCrLf & vbCrLf & "*The display could not be updated since an error occurred."
            err_flag = 1
            '----------------------------------------
            ' Set comparison count
            '----------------------------------------
            CheckBox_CompareLoad.Checked = False
            '----------------------------------------
            ' Set comparison match preset
            '----------------------------------------
            CheckBox_PresetLoad.Checked = False
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Set operation mode
            '--------------------
            Select Case cnt_mode
                Case 0
                    '----------------------------------------
                    ' Set comparison count
                    '----------------------------------------
                    CheckBox_CompareLoad.Checked = False
                    '----------------------------------------
                    ' Set comparison match preset
                    '----------------------------------------
                    CheckBox_PresetLoad.Checked = False
                Case CNT_LOADPRESET
                    '----------------------------------------
                    ' Set comparison count
                    '----------------------------------------
                    CheckBox_CompareLoad.Checked = False
                    '----------------------------------------
                    ' Set comparison match preset
                    '----------------------------------------
                    CheckBox_PresetLoad.Checked = True
                Case CNT_LOADCOMP
                    '----------------------------------------
                    ' Set comparison count
                    '----------------------------------------
                    CheckBox_CompareLoad.Checked = True
                    '----------------------------------------
                    ' Set comparison match preset
                    '----------------------------------------
                    CheckBox_PresetLoad.Checked = False
                Case (CNT_LOADPRESET Or CNT_LOADCOMP)
                    '----------------------------------------
                    ' Set comparison count
                    '----------------------------------------
                    CheckBox_CompareLoad.Checked = True
                    '----------------------------------------
                    ' Set comparison match preset
                    '----------------------------------------
                    CheckBox_PresetLoad.Checked = True
                Case Else
                    '----------------------------------------
                    ' Set comparison count
                    '----------------------------------------
                    CheckBox_CompareLoad.Checked = False
                    '----------------------------------------
                    ' Set comparison match preset
                    '----------------------------------------
                    CheckBox_PresetLoad.Checked = False
            End Select
        End If
        '----------------------------------------
        ' Updated available controls by operation mode
        '----------------------------------------
        CheckBox_CompareLoad_CheckedChanged(New Object(), New EventArgs())
        CheckBox_PresetLoad_CheckedChanged(New Object(), New EventArgs())
        '----------------------------------------
        ' Comparison Count Set: Create the combobox for the number of setting data
        '----------------------------------------
        ComboBox_SettingCompareDataNum.Items.Add("1")
        ComboBox_SettingCompareDataNum.Items.Add("2")
        ComboBox_SettingCompareDataNum.Items.Add("3")
        ComboBox_SettingCompareDataNum.Items.Add("4")
        ComboBox_SettingCompareDataNum.Items.Add("5")
        '----------------------------------------
        ' Comparison Count Set: Change the selection of the combobox for the number of setting data
        '----------------------------------------
        index = (Form1.g_compare_data_num - 1)
        ComboBox_SettingCompareDataNum.SelectedIndex = index
        If ((cnt_mode And CNT_LOADCOMP) = CNT_LOADCOMP) Then
            ComboBox_SettingCompareDataNum_SelectedIndexChanged(New Object(), New EventArgs())
        Else
            TextBox_SettingCompareData0.Enabled = False
            TextBox_SettingCompareData1.Enabled = False
            TextBox_SettingCompareData2.Enabled = False
            TextBox_SettingCompareData3.Enabled = False
            TextBox_SettingCompareData4.Enabled = False
        End If
        '----------------------------------------
        ' Comparison Count Set: Create the combobox for the repeat setting
        '----------------------------------------
        ComboBox_SettingCompareRepeat.Items.Add("Not repeat")
        ComboBox_SettingCompareRepeat.Items.Add("Repeat")
        '----------------------------------------
        ' Comparison Count Set: Change the selection of the combobox for the repeat setting
        '----------------------------------------
        index = Form1.g_compare_repeat
        ComboBox_SettingCompareRepeat.SelectedIndex = index
        '----------------------------------------
        ' Comparison Count Set: Update comparison count data
        '----------------------------------------
        TextBox_SettingCompareData0.Text = Form1.g_compare_data_0.ToString()
        TextBox_SettingCompareData1.Text = Form1.g_compare_data_1.ToString()
        TextBox_SettingCompareData2.Text = Form1.g_compare_data_2.ToString()
        TextBox_SettingCompareData3.Text = Form1.g_compare_data_3.ToString()
        TextBox_SettingCompareData4.Text = Form1.g_compare_data_4.ToString()
        '----------------------------------------
        ' Comparison Match Preset Set: Create the combobox for the number of setting data
        '----------------------------------------
        ComboBox_SettingPresetDataNum.Items.Add("1")
        ComboBox_SettingPresetDataNum.Items.Add("2")
        ComboBox_SettingPresetDataNum.Items.Add("3")
        ComboBox_SettingPresetDataNum.Items.Add("4")
        ComboBox_SettingPresetDataNum.Items.Add("5")
        '----------------------------------------
        ' Comparison Match Preset Set: Change the selection of the combobox for the number of setting data
        '----------------------------------------
        index = (Form1.g_preset_data_num - 1)
        ComboBox_SettingPresetDataNum.SelectedIndex = index
        If ((cnt_mode And CNT_LOADPRESET) = CNT_LOADPRESET) Then
            ComboBox_SettingPresetDataNum_SelectedIndexChanged(New Object(), New EventArgs())
        Else
            TextBox_SettingPresetData0.Enabled = False
            TextBox_SettingPresetData1.Enabled = False
            TextBox_SettingPresetData2.Enabled = False
            TextBox_SettingPresetData3.Enabled = False
            TextBox_SettingPresetData4.Enabled = False
        End If
        '----------------------------------------
        ' Comparison Match Preset Set: Create the combobox for the repeat setting
        '----------------------------------------
        ComboBox_SettingPresetRepeat.Items.Add("Not repeat")
        ComboBox_SettingPresetRepeat.Items.Add("Repeat")
        '----------------------------------------
        ' Comparison Match Preset Set: Change the selection of the combobox for the repeat setting
        '----------------------------------------
        index = Form1.g_preset_repeat
        ComboBox_SettingPresetRepeat.SelectedIndex = index
        '----------------------------------------
        ' Comparison Match Preset Set: Update preset data
        '----------------------------------------
        TextBox_SettingPresetData0.Text = Form1.g_preset_data_0.ToString()
        TextBox_SettingPresetData1.Text = Form1.g_preset_data_1.ToString()
        TextBox_SettingPresetData2.Text = Form1.g_preset_data_2.ToString()
        TextBox_SettingPresetData3.Text = Form1.g_preset_data_3.ToString()
        TextBox_SettingPresetData4.Text = Form1.g_preset_data_4.ToString()
        '----------------------------------------
        ' Display message if an error occurs
        '----------------------------------------
        If (err_flag <> 0) Then
            MessageBox.Show(message_string, "Error", MessageBoxButtons.OK)
        End If
    End Sub

    '================================================================================
    ' It is the process when the Comparison Count Set checkobox is clicked
    ' Update available controls
    '================================================================================
    Private Sub CheckBox_CompareLoad_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_CompareLoad.CheckedChanged
        '----------------------------------------
        ' Update available controls
        '----------------------------------------
        If (CheckBox_CompareLoad.Checked = True) Then
            ComboBox_SettingCompareDataNum.Enabled = True
            ComboBox_SettingCompareRepeat.Enabled = True
            TextBox_SettingCompareData0.Enabled = True
            TextBox_SettingCompareData1.Enabled = True
            TextBox_SettingCompareData2.Enabled = True
            TextBox_SettingCompareData3.Enabled = True
            TextBox_SettingCompareData4.Enabled = True
            ComboBox_SettingCompareDataNum_SelectedIndexChanged(New Object(), New EventArgs())
        Else
            ComboBox_SettingCompareDataNum.Enabled = False
            ComboBox_SettingCompareRepeat.Enabled = False
            TextBox_SettingCompareData0.Enabled = False
            TextBox_SettingCompareData1.Enabled = False
            TextBox_SettingCompareData2.Enabled = False
            TextBox_SettingCompareData3.Enabled = False
            TextBox_SettingCompareData4.Enabled = False
        End If
    End Sub

    '================================================================================
    ' It is the process when the Number of Setting Data combobox of Comparison Count Set is changed
    ' Update available controls
    '================================================================================
    Private Sub ComboBox_SettingCompareDataNum_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_SettingCompareDataNum.SelectedIndexChanged
        Dim index As Integer       ' Index of combobox
        Dim data_num As Integer    ' Number of setting data

        '----------------------------------------
        ' Get the index of number of data from combobox
        '----------------------------------------
        index = ComboBox_SettingCompareDataNum.SelectedIndex
        '----------------------------------------
        ' Branch processing by number of data
        '----------------------------------------
        data_num = (index + 1)
        Select Case data_num
            Case 1
                TextBox_SettingCompareData0.Enabled = True
                TextBox_SettingCompareData1.Enabled = False
                TextBox_SettingCompareData2.Enabled = False
                TextBox_SettingCompareData3.Enabled = False
                TextBox_SettingCompareData4.Enabled = False

            Case 2
                TextBox_SettingCompareData0.Enabled = True
                TextBox_SettingCompareData1.Enabled = True
                TextBox_SettingCompareData2.Enabled = False
                TextBox_SettingCompareData3.Enabled = False
                TextBox_SettingCompareData4.Enabled = False
            Case 3
                TextBox_SettingCompareData0.Enabled = True
                TextBox_SettingCompareData1.Enabled = True
                TextBox_SettingCompareData2.Enabled = True
                TextBox_SettingCompareData3.Enabled = False
                TextBox_SettingCompareData4.Enabled = False
            Case 4
                TextBox_SettingCompareData0.Enabled = True
                TextBox_SettingCompareData1.Enabled = True
                TextBox_SettingCompareData2.Enabled = True
                TextBox_SettingCompareData3.Enabled = True
                TextBox_SettingCompareData4.Enabled = False
            Case 5
                TextBox_SettingCompareData0.Enabled = True
                TextBox_SettingCompareData1.Enabled = True
                TextBox_SettingCompareData2.Enabled = True
                TextBox_SettingCompareData3.Enabled = True
                TextBox_SettingCompareData4.Enabled = True
            Case Else
                TextBox_SettingCompareData0.Enabled = True
                TextBox_SettingCompareData1.Enabled = False
                TextBox_SettingCompareData2.Enabled = False
                TextBox_SettingCompareData3.Enabled = False
                TextBox_SettingCompareData4.Enabled = False
        End Select
    End Sub

    '================================================================================
    ' It is the process when the Comparison Match Preset Set checkobox is clicked
    ' Update available controls
    '================================================================================
    Private Sub CheckBox_PresetLoad_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_PresetLoad.CheckedChanged
        '----------------------------------------
        ' Update available controls
        '----------------------------------------
        If (CheckBox_PresetLoad.Checked = True) Then
            ComboBox_SettingPresetDataNum.Enabled = True
            ComboBox_SettingPresetRepeat.Enabled = True
            TextBox_SettingPresetData0.Enabled = True
            TextBox_SettingPresetData1.Enabled = True
            TextBox_SettingPresetData2.Enabled = True
            TextBox_SettingPresetData3.Enabled = True
            TextBox_SettingPresetData4.Enabled = True
            ComboBox_SettingPresetDataNum_SelectedIndexChanged(New Object(), New EventArgs())
        Else
            ComboBox_SettingPresetDataNum.Enabled = False
            ComboBox_SettingPresetRepeat.Enabled = False
            TextBox_SettingPresetData0.Enabled = False
            TextBox_SettingPresetData1.Enabled = False
            TextBox_SettingPresetData2.Enabled = False
            TextBox_SettingPresetData3.Enabled = False
            TextBox_SettingPresetData4.Enabled = False
        End If
    End Sub

    '================================================================================
    ' It is the process when the Number of Setting Data combobox of Comparison Match Preset Set is changed
    ' Update available controls
    '================================================================================
    Private Sub ComboBox_SettingPresetDataNum_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_SettingPresetDataNum.SelectedIndexChanged
        Dim index As Integer       ' Index of combobox
        Dim data_num As Integer    ' Number of setting data

        '----------------------------------------
        ' Get the index of number of data from combobox
        '----------------------------------------
        index = ComboBox_SettingPresetDataNum.SelectedIndex
        '----------------------------------------
        ' Branch processing by number of data
        '----------------------------------------
        data_num = (index + 1)
        Select Case data_num
            Case 1
                TextBox_SettingPresetData0.Enabled = True
                TextBox_SettingPresetData1.Enabled = False
                TextBox_SettingPresetData2.Enabled = False
                TextBox_SettingPresetData3.Enabled = False
                TextBox_SettingPresetData4.Enabled = False
            Case 2
                TextBox_SettingPresetData0.Enabled = True
                TextBox_SettingPresetData1.Enabled = True
                TextBox_SettingPresetData2.Enabled = False
                TextBox_SettingPresetData3.Enabled = False
                TextBox_SettingPresetData4.Enabled = False
            Case 3
                TextBox_SettingPresetData0.Enabled = True
                TextBox_SettingPresetData1.Enabled = True
                TextBox_SettingPresetData2.Enabled = True
                TextBox_SettingPresetData3.Enabled = False
                TextBox_SettingPresetData4.Enabled = False
            Case 4
                TextBox_SettingPresetData0.Enabled = True
                TextBox_SettingPresetData1.Enabled = True
                TextBox_SettingPresetData2.Enabled = True
                TextBox_SettingPresetData3.Enabled = True
                TextBox_SettingPresetData4.Enabled = False
            Case 5
                TextBox_SettingPresetData0.Enabled = True
                TextBox_SettingPresetData1.Enabled = True
                TextBox_SettingPresetData2.Enabled = True
                TextBox_SettingPresetData3.Enabled = True
                TextBox_SettingPresetData4.Enabled = True
            Case Else
                TextBox_SettingPresetData0.Enabled = True
                TextBox_SettingPresetData1.Enabled = False
                TextBox_SettingPresetData2.Enabled = False
                TextBox_SettingPresetData3.Enabled = False
                TextBox_SettingPresetData4.Enabled = False
        End Select
    End Sub

    '================================================================================
    ' It is the process when the OK button is clicked
    ' Set counter
    '================================================================================
    Private Sub Button_OK_Click(sender As Object, e As EventArgs) Handles Button_OK.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ret As Integer                                ' For return code
        Dim cnt_mode As Short                             ' Operation mode
        Dim index As Integer                              ' Index of combobox
        Dim comparison_data(7) As Integer                 ' Comparison count data
        Dim preset_data(7) As Integer                     ' Preset data of counter

        '----------------------------------------
        ' Get the counter operation mode
        '----------------------------------------
        cnt_mode = (IIf(CheckBox_PresetLoad.Checked, CNT_LOADPRESET, 0) Or IIf(CheckBox_CompareLoad.Checked, CNT_LOADCOMP, 0))
        '----------------------------------------
        ' Set the counter operation mode
        '----------------------------------------
        ret = AioSetCntComparisonMode(Form1.g_id, Form1.g_channel, cnt_mode)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            text_string = "AioSetCntComparisonMode = " & ret & " : " & error_string.ToString()
            MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' Set the comparison count data
        '----------------------------------------
        If (CheckBox_CompareLoad.Checked = True) Then
            '----------------------------------------
            ' Get the index of number of data from combobox
            '----------------------------------------
            index = ComboBox_SettingCompareDataNum.SelectedIndex
            Form1.g_compare_data_num = (index + 1)
            '----------------------------------------
            ' Get counter comparison count data from textbox
            '----------------------------------------
            If (Integer.TryParse(TextBox_SettingCompareData0.Text, Form1.g_compare_data_0) = False) Then
                Form1.g_compare_data_0 = 0
            End If
            comparison_data(0) = Form1.g_compare_data_0
            If (Integer.TryParse(TextBox_SettingCompareData1.Text, Form1.g_compare_data_1) = False) Then
                Form1.g_compare_data_1 = 0
            End If
            comparison_data(1) = Form1.g_compare_data_1
            If (Integer.TryParse(TextBox_SettingCompareData2.Text, Form1.g_compare_data_2) = False) Then
                Form1.g_compare_data_2 = 0
            End If
            comparison_data(2) = Form1.g_compare_data_2
            If (Integer.TryParse(TextBox_SettingCompareData3.Text, Form1.g_compare_data_3) = False) Then
                Form1.g_compare_data_3 = 0
            End If
            comparison_data(3) = Form1.g_compare_data_3
            If (Integer.TryParse(TextBox_SettingCompareData4.Text, Form1.g_compare_data_4) = False) Then
                Form1.g_compare_data_4 = 0
            End If
            comparison_data(4) = Form1.g_compare_data_4
            '----------------------------------------
            ' Get the index of the repeat setting from combobox
            '----------------------------------------
            index = ComboBox_SettingCompareRepeat.SelectedIndex
            Form1.g_compare_repeat = index
            '----------------------------------------
            ' Set the next comparison count value loaded after comparison count match
            '----------------------------------------
            ret = AioSetCntComparisonReg(Form1.g_id, Form1.g_channel, Form1.g_compare_data_num, comparison_data, Form1.g_compare_repeat)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = "AioSetCntComparisonReg = " & ret & " : " & error_string.ToString()
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        End If
        '----------------------------------------
        ' Set the preset load data
        '----------------------------------------
        If (CheckBox_PresetLoad.Checked = True) Then
            '----------------------------------------
            ' Get the index of number of data from combobox
            '----------------------------------------
            index = ComboBox_SettingPresetDataNum.SelectedIndex
            Form1.g_preset_data_num = (index + 1)
            '----------------------------------------
            ' Get counter preset data from textbox
            '----------------------------------------
            If (Integer.TryParse(TextBox_SettingPresetData0.Text, Form1.g_preset_data_0) = False) Then
                Form1.g_preset_data_0 = 0
            End If
            preset_data(0) = Form1.g_preset_data_0
            If (Integer.TryParse(TextBox_SettingPresetData1.Text, Form1.g_preset_data_1) = False) Then
                Form1.g_preset_data_1 = 0
            End If
            preset_data(1) = Form1.g_preset_data_1
            If (Integer.TryParse(TextBox_SettingPresetData2.Text, Form1.g_preset_data_2) = False) Then
                Form1.g_preset_data_2 = 0
            End If
            preset_data(2) = Form1.g_preset_data_2
            If (Integer.TryParse(TextBox_SettingPresetData3.Text, Form1.g_preset_data_3) = False) Then
                Form1.g_preset_data_3 = 0
            End If
            preset_data(3) = Form1.g_preset_data_3
            If (Integer.TryParse(TextBox_SettingPresetData4.Text, Form1.g_preset_data_4) = False) Then
                Form1.g_preset_data_4 = 0
            End If
            preset_data(4) = Form1.g_preset_data_4
            '----------------------------------------
            ' Get the index of the repeat setting from combobox
            '----------------------------------------
            index = ComboBox_SettingPresetRepeat.SelectedIndex
            Form1.g_preset_repeat = index
            '----------------------------------------
            ' Set the preset value loaded after the comparison count match
            '----------------------------------------
            ret = AioSetCntPresetReg(Form1.g_id, Form1.g_channel, Form1.g_preset_data_num, preset_data, Form1.g_preset_repeat)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = "AioSetCntPresetReg = " & ret & " : " & error_string.ToString()
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        End If
        '----------------------------------------
        ' OK process
        '----------------------------------------
        Me.Close()
    End Sub

    '================================================================================
    ' It is the process when the Close button is clicked
    ' Terminate the dialog
    '================================================================================
    Private Sub Button_Cancel_Click(sender As Object, e As EventArgs) Handles Button_Cancel.Click
        '----------------------------------------
        ' Cancel process
        '----------------------------------------
        Me.Close()
    End Sub
End Class