﻿Imports System.IO
Imports System.Text

Public Class Condition
    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Dim g_start_trigger() As Short = {0,    ' Software
                                      1,    ' External trigger rising edge
                                      2,    ' External trigger falling edge
                                      3,    ' Level comparison
                                      4,    ' In Range comparison
                                      5}    ' Out Range comparison
    Dim g_clock_type() As Short = {0,       ' Internal clock
                                   1}       ' External clock
    Dim g_clock_edge() As Short = {0,       ' External input falling edge
                                   1}       ' External input rising edge
    Dim g_stop_trigger() As Short = {1,     ' External trigger rising edge
                                     2,     ' External trigger falling edge
                                     3,     ' Level comparison
                                     4,     ' Command (AioStopAi)
                                     5,     ' In Range comparison
                                     6}     ' Out Range comparison

    Private Sub Condition_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim message_string As String = ""                 ' For displaying message
        Dim ret As Integer                                ' For return code
        Dim index As Short                                ' Index of combobox
        Dim ai_start_trigger As Short                     ' Conversion start condition
        Dim ai_stop_trigger As Short                      ' Conversion stop condition
        Dim ai_level As Single                            ' Level comparison data
        Dim ai_direction As Short                         ' Level comparison direction
        Dim ai_range_level1 As Single                     ' Upper limit data of In Range / Out Range comparison
        Dim ai_range_level2 As Single                     ' Lower limit data of In Range / Out Range comparison
        Dim ai_state_times As Integer                     ' Sampling times of the state-holding
        Dim ai_clock_type As Short                        ' Clock type
        Dim ai_sampling_clock As Single                   ' Sampling clock
        Dim ai_clock_edge As Short                        ' Timing of the external clock input
        Dim ai_auto_mode As Short                         ' Set mode of transfer size
        Dim ai_transfer_size As Integer                   ' Transfer size
        Dim ai_scan_clock As Single                       ' Conversion speed
        Dim err_flag As Short                             ' Function execution error flag

        '----------------------------------------
        ' Initialize parameters
        '----------------------------------------
        err_flag = 0
        '----------------------------------------
        ' Create the combobox for analog input conversion start condition
        '----------------------------------------
        ComboBox_StartCondition.Items.Add("Software")
        ComboBox_StartCondition.Items.Add("External trigger rising edge")
        ComboBox_StartCondition.Items.Add("External trigger falling edge")
        ComboBox_StartCondition.Items.Add("Level comparison")
        ComboBox_StartCondition.Items.Add("In Range comparison")
        ComboBox_StartCondition.Items.Add("Out Range comparison")
        '----------------------------------------
        ' Get the conversion start condition
        '----------------------------------------
        ret = AioGetAiStartTrigger(Form1.g_id, ai_start_trigger)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                message_string = String.Format("AioGetAiStartTrigger = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                err_flag = 1
            Else
                '--------------------
                ' Disable control
                '--------------------
                ComboBox_StartCondition.Enabled = False
            End If
            '--------------------
            ' Change the selection of analog input conversion start condition combobox to 0
            '--------------------
            ComboBox_StartCondition.SelectedIndex = 0
            ComboBox_StartCondition_SelectedIndexChanged(New Object(), New EventArgs())
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Convert from conversion start condition to index
            '--------------------
            index = 0
            Select Case ai_start_trigger
                Case 0
                    index = 0
                Case 1
                    index = 1
                Case 2
                    index = 2
                Case 3
                    index = 3
                Case 4
                    index = 4
                Case 5
                    index = 5
                Case Else
                    index = 0
            End Select
            '--------------------
            ' Change the selection of the analog input conversion start condition combobox
            '--------------------
            ComboBox_StartCondition.SelectedIndex = index
            ComboBox_StartCondition_SelectedIndexChanged(New Object(), New EventArgs())
        End If
        '----------------------------------------
        ' Update the initial display of channel No.
        '----------------------------------------
        TextBox_StartLevelChNo.Text = "0"
        '----------------------------------------
        ' Create the combobox for level comparison direction
        '----------------------------------------
        ComboBox_StartLevelComprionDriection.Items.Add("Both edges")
        ComboBox_StartLevelComprionDriection.Items.Add("Rising edge")
        ComboBox_StartLevelComprionDriection.Items.Add("Falling edge")
        '----------------------------------------
        ' For Level comparison
        '----------------------------------------
        If (ai_start_trigger = 3) Then
            '----------------------------------------
            ' Get the level when the level comparison is started
            '----------------------------------------
            ret = AioGetAiStartLevelEx(Form1.g_id, 0, ai_level, ai_direction)
            '----------------------------------------
            ' Abnormal termination process
            '----------------------------------------
            If (ret <> 0) Then
                If (ret <> 20001) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Display message
                    '--------------------
                    message_string = String.Format("AioGetAiStartLevelEx = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                    err_flag = 1
                Else
                    '--------------------
                    ' Disable control
                    '--------------------
                    TextBox_StartLevelChNo.Enabled = FALSE
                    TextBox_StartLevelComprionValue.Enabled = FALSE
                    ComboBox_StartLevelComprionDriection.Enabled = FALSE
                End If
                '--------------------
                ' Update the initial display of comparison value
                '--------------------
                TextBox_StartLevelComprionValue.Text = "1.0"
                '--------------------
                ' Change the selection of the level comparison direction combobox to 0
                '--------------------
                ComboBox_StartLevelComprionDriection.SelectedIndex = 0
            '----------------------------------------
            ' Normal termination process
            '----------------------------------------
            Else
                '--------------------
                ' Update comparison value
                '--------------------
                TextBox_StartLevelComprionValue.Text = ai_level.ToString("F1")
                '--------------------
                ' Convert from level comparison direction to index
                '--------------------
                index = 0
                Select Case ai_direction
                    Case 0
                        index = 0
                    Case 1
                        index = 1
                    Case 2
                        index = 2
                    Case Else
                        index = 0
                End Select
                '--------------------
                ' Change the selection of the level comparison direction combobox
                '--------------------
                ComboBox_StartLevelComprionDriection.SelectedIndex = index
            End If
        '----------------------------------------
        ' Other than Level comparison
        '----------------------------------------
        Else
            '--------------------
            ' Update the initial display of comparison value
            '--------------------
            TextBox_StartLevelComprionValue.Text = "1.0"
            '--------------------
            ' Change the selection of the level comparison direction combobox to 0
            '--------------------
            ComboBox_StartLevelComprionDriection.SelectedIndex = 0
        End If
        '----------------------------------------
        ' Update the initial display of channel No.
        '----------------------------------------
        TextBox_StartRangeChNo.Text = "0"
        '----------------------------------------
        ' For In Range comparison
        '----------------------------------------
        If (ai_start_trigger = 4) Then
            '----------------------------------------
            ' Get the settings of In Range comparison start condition
            '----------------------------------------
            ret = AioGetAiStartInRangeEx(Form1.g_id, 0, ai_range_level1, ai_range_level2, ai_state_times)
            '----------------------------------------
            ' Abnormal termination process
            '----------------------------------------
            If (ret <> 0) Then
                If (ret <> 20001) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Display message
                    '--------------------
                    message_string = String.Format("AioGetAiStartInRangeEx = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                    err_flag = 1
                Else
                    '--------------------
                    ' Disable control
                    '--------------------
                    TextBox_StartRangeChNo.Enabled = FALSE
                    TextBox_StartRangeUpperLimitValue.Enabled = FALSE
                    TextBox_StartRangeLowerLimitValue.Enabled = FALSE
                    TextBox_StartRangeStateTimes.Enabled = FALSE
                End If
                '--------------------
                ' Update the initial display of upper limit
                '--------------------
                TextBox_StartRangeUpperLimitValue.Text = "10.0"
                '--------------------
                ' Update the initial display of lower limit
                '--------------------
                TextBox_StartRangeLowerLimitValue.Text = "1.0"
                '--------------------
                ' Update the initial display of state-holding times
                '--------------------
                TextBox_StartRangeStateTimes.Text = "1"
            '----------------------------------------
            ' Normal termination process
            '----------------------------------------
            Else
                '--------------------
                ' Update upper limit
                '--------------------
                TextBox_StartRangeUpperLimitValue.Text = ai_range_level1.ToString("F1")
                '--------------------
                ' Update lower limit
                '--------------------
                TextBox_StartRangeLowerLimitValue.Text = ai_range_level2.ToString("F1")
                '--------------------
                ' Update state-holding times
                '--------------------
                TextBox_StartRangeStateTimes.Text = ai_state_times.ToString()
            End If
        '----------------------------------------
        ' For Out Range comparison
        '----------------------------------------
        ElseIf (ai_start_trigger = 5) Then
            '----------------------------------------
            ' Get the settings of Out Range comparison start condition
            '----------------------------------------
            ret = AioGetAiStartOutRangeEx(Form1.g_id, 0, ai_range_level1, ai_range_level2, ai_state_times)
            '----------------------------------------
            ' Abnormal termination process
            '----------------------------------------
            If (ret <> 0) Then
                If (ret <> 20001) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Display message
                    '--------------------
                    message_string = String.Format("AioGetAiStartOutRangeEx = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                    err_flag = 1
                Else
                    '--------------------
                    ' Disable control
                    '--------------------
                    TextBox_StartRangeChNo.Enabled = FALSE
                    TextBox_StartRangeUpperLimitValue.Enabled = FALSE
                    TextBox_StartRangeLowerLimitValue.Enabled = FALSE
                    TextBox_StartRangeStateTimes.Enabled = FALSE
                End If
                '--------------------
                ' Update the initial display of upper limit
                '--------------------
                TextBox_StartRangeUpperLimitValue.Text = "10.0"
                '--------------------
                ' Update the initial display of lower limit
                '--------------------
                TextBox_StartRangeLowerLimitValue.Text = "1.0"
                '--------------------
                ' Update the initial display of state-holding times
                '--------------------
                TextBox_StartRangeStateTimes.Text = "1"
            '----------------------------------------
            ' Normal termination process
            '----------------------------------------
            Else
                '--------------------
                ' Update upper limit
                '--------------------
                TextBox_StartRangeUpperLimitValue.Text = ai_range_level1.ToString("F1")
                '--------------------
                ' Update lower limit
                '--------------------
                TextBox_StartRangeLowerLimitValue.Text = ai_range_level2.ToString("F1")
                '--------------------
                ' Update state-holding times
                '--------------------
                TextBox_StartRangeStateTimes.Text = ai_state_times.ToString()
            End If
        '----------------------------------------
        ' Oher than In Range comparison / Out Range comparisonO
        '----------------------------------------
        Else
            '--------------------
            ' Update the initial display of upper limit
            '--------------------
            TextBox_StartRangeUpperLimitValue.Text = "10.0"
            '--------------------
            ' Update the initial display of lower limit
            '--------------------
            TextBox_StartRangeLowerLimitValue.Text = "1.0"
            '--------------------
            ' Update the initial display of state-holding times
            '--------------------
            TextBox_StartRangeStateTimes.Text = "1"
        End If
        '----------------------------------------
        ' Create the combobox for analog input clock type
        '----------------------------------------
        ComboBox_ClockType.Items.Add("Internal clock")
        ComboBox_ClockType.Items.Add("External clock")
        '----------------------------------------
        ' Get clock type
        '----------------------------------------
        ret = AioGetAiClockType(Form1.g_id, ai_clock_type)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                message_string = String.Format("AioGetAiClockType = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                err_flag = 1
            Else
                '--------------------
                ' Disable control
                '--------------------
                ComboBox_ClockType.Enabled = FALSE
            End If
            '--------------------
            ' Change the selection of the analog input clock type combobox to 0
            '--------------------
            ComboBox_ClockType.SelectedIndex = 0
            ComboBox_ClockType_SelectedIndexChanged(New Object(), New EventArgs())
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Convert from clock type to index
            '--------------------
            index = 0
            Select Case ai_clock_type
                Case 0
                    index = 0
                Case 1
                    index = 1
                Case Else
                    index = 0
            End Select
            '--------------------
            ' Change the selection of the analog input clock type combobox
            '--------------------
            ComboBox_ClockType.SelectedIndex = index
            ComboBox_ClockType_SelectedIndexChanged(New Object(), New EventArgs())
        End If
        '----------------------------------------
        ' Get sampling clock
        '----------------------------------------
        ret = AioGetAiSamplingClock(Form1.g_id, ai_sampling_clock)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                message_string = String.Format("AioGetAiSamplingClock = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                err_flag = 1
            Else
                '--------------------
                ' Disable control
                '--------------------
                TextBox_SamplingClock.Enabled = FALSE
            End If
            '--------------------
            ' Update the initial display of sampling clock
            '--------------------
            TextBox_SamplingClock.Text = "1000"
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Update sampling clock
            '--------------------
            TextBox_SamplingClock.Text = Convert.ToDouble(ai_sampling_clock).ToString("F6")
        End If
        '----------------------------------------
        ' Create the combobox for analog input external clock input timing
        '----------------------------------------
        ComboBox_ExternalClockInputTiming.Items.Add("External input falling edge")
        ComboBox_ExternalClockInputTiming.Items.Add("External input rising edge")
        '----------------------------------------
        ' For External clock
        '----------------------------------------
        If (ai_clock_type = 1) Then
            '----------------------------------------
            ' Get the timing of external clock input
            '----------------------------------------
            ret = AioGetAiClockEdge(Form1.g_id, ai_clock_edge)
            '----------------------------------------
            ' Abnormal termination process
            '----------------------------------------
            If (ret <> 0) Then
                If (ret <> 20001) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Display message
                    '--------------------
                    message_string = String.Format("AioGetAiClockEdge = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                    err_flag = 1
                Else
                    '--------------------
                    ' Disable control
                    '--------------------
                    ComboBox_ExternalClockInputTiming.Enabled = FALSE
                End If
                '----------------------------------------
                ' Change the selection of the analog input external clock input timing combobox to 0
                '----------------------------------------
                ComboBox_ExternalClockInputTiming.SelectedIndex = 0
            '----------------------------------------
            ' Normal termination process
            '----------------------------------------
            Else
                '--------------------
                ' Convert from input timing to index
                '--------------------
                index = 0
                Select Case ai_clock_edge
                    Case 0
                        index = 0
                    Case 1
                        index = 1
                    Case Else
                        index = 0
                End Select
                '----------------------------------------
                ' Change the selection of the analog input external clock input timing combobox
                '----------------------------------------
                ComboBox_ExternalClockInputTiming.SelectedIndex = index
            End If
        '----------------------------------------
        ' Other than External clock
        '----------------------------------------
        Else
            '----------------------------------------
            ' Change the selection of the analog input external clock input timing combobox to 0
            '----------------------------------------
            ComboBox_ExternalClockInputTiming.SelectedIndex = 0
        End If
        '----------------------------------------
        ' Get transfer size
        '----------------------------------------
        ret = AioGetAiTransferSize(Form1.g_id, ai_auto_mode, ai_transfer_size)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                message_string = String.Format("AioGetAiTransferSize = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                err_flag = 1
            Else
                '--------------------
                ' Disable control
                '--------------------
                TextBox_TransferSize.Enabled = FALSE
            End If
            '--------------------
            ' Update the initial display of transfer size
            '--------------------
            TextBox_TransferSize.Text = "0"
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Update transfer size
            '--------------------
            TextBox_TransferSize.Text = ai_transfer_size.ToString()
        End If
        '----------------------------------------
        ' Get conversion speed
        '----------------------------------------
        ret = AioGetAiScanClock(Form1.g_id, ai_scan_clock)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                message_string = String.Format("AioGetAiScanClock = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                err_flag = 1
            Else
                '--------------------
                ' Disable control
                '--------------------
                TextBox_ScanClock.Enabled = FALSE
            End If
            '--------------------
            ' Update the initial display of conversion speed
            '--------------------
            TextBox_ScanClock.Text = "10"
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Update conversion speed
            '--------------------
            TextBox_ScanClock.Text = ai_scan_clock.ToString()
        End If
        '----------------------------------------
        ' Create the combobox for analog input conversion stop condition
        '----------------------------------------
        ComboBox_StopCondition.Items.Add("External trigger rising edge")
        ComboBox_StopCondition.Items.Add("External trigger falling edge")
        ComboBox_StopCondition.Items.Add("Level comparison")
        ComboBox_StopCondition.Items.Add("Command (AioStopAi)")
        ComboBox_StopCondition.Items.Add("In Range comparison")
        ComboBox_StopCondition.Items.Add("Out Range comparison")
        '----------------------------------------
        ' Get the conversion stop condition
        '----------------------------------------
        ret = AioGetAiStopTrigger(Form1.g_id, ai_stop_trigger)
        '----------------------------------------
        ' Abnormal termination process
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                message_string = String.Format("AioGetAiStopTrigger = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                err_flag = 1
            Else
                '--------------------
                ' Disable control
                '--------------------
                ComboBox_StopCondition.Enabled = FALSE
            End If
            '--------------------
            ' Change the selection in the analog input conversion stop condition combobox to 3
            '--------------------
            ComboBox_StopCondition.SelectedIndex = 3
            ComboBox_StopCondition_SelectedIndexChanged(New Object(), New EventArgs())
        '----------------------------------------
        ' Normal termination process
        '----------------------------------------
        Else
            '--------------------
            ' Convert from conversion stop condition to index
            '--------------------
            index = 0
            Select Case ai_stop_trigger
                Case 0
                    index = 3
                Case 1
                    index = 0
                Case 2
                    index = 1
                Case 3
                    index = 2
                Case 4
                    index = 3
                Case 5
                    index = 4
                Case 6
                    index = 5
                Case Else
                    index = 3
            End Select
            '--------------------
            ' Change the selection in the analog input conversion stop condition combobox
            '--------------------
            ComboBox_StopCondition.SelectedIndex = index
            ComboBox_StopCondition_SelectedIndexChanged(New Object(), New EventArgs())
        End If
        '----------------------------------------
        ' Update the initial display of channel No.
        '----------------------------------------
        TextBox_StopLevelChNo.Text = "0"
        '----------------------------------------
        ' Create the combobox for level comparison direction
        '----------------------------------------
        ComboBox_StopLevelComprionDriection.Items.Add("Both edges")
        ComboBox_StopLevelComprionDriection.Items.Add("Rising edge")
        ComboBox_StopLevelComprionDriection.Items.Add("Falling edge")
        '----------------------------------------
        ' For Level comparison
        '----------------------------------------
        If (ai_stop_trigger = 3) Then
            '----------------------------------------
            ' Get the level when the level comparison is stopped
            '----------------------------------------
            ret = AioGetAiStopLevelEx(Form1.g_id, 0, ai_level, ai_direction)
            '----------------------------------------
            ' Abnormal termination process
            '----------------------------------------
            If (ret <> 0) Then
                If (ret <> 20001) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Display message
                    '--------------------
                    message_string = String.Format("AioGetAiStopLevelEx = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                    err_flag = 1
                Else
                    '--------------------
                    ' Disable control
                    '--------------------
                    TextBox_StopLevelChNo.Enabled = FALSE
                    TextBox_StopLevelComprionValue.Enabled = FALSE
                    ComboBox_StopLevelComprionDriection.Enabled = FALSE
                End If
                '--------------------
                ' Update the initial display of comparison value
                '--------------------
                TextBox_StopLevelComprionValue.Text = "1.0"
                '--------------------
                ' Change the selection of the level comparison direction combobox to 0
                '--------------------
                ComboBox_StopLevelComprionDriection.SelectedIndex = 0
            '----------------------------------------
            ' Normal termination process
            '----------------------------------------
            Else
                '--------------------
                ' Update comparison value
                '--------------------
                TextBox_StopLevelComprionValue.Text = ai_level.ToString("F1")
                '--------------------
                ' Convert from level comparison direction to index
                '--------------------
                index = 0
                Select Case ai_direction
                    Case 0
                        index = 0
                    Case 1
                        index = 1
                    Case 2
                        index = 2
                    Case Else
                        index = 0
                End Select
                '--------------------
                ' Change the selection of the level comparison direction combobox
                '--------------------
                ComboBox_StopLevelComprionDriection.SelectedIndex = index
            End If
        '----------------------------------------
        ' Other than Level comparison
        '----------------------------------------
        Else
            '--------------------
            ' Update the initial display of comparison value
            '--------------------
            TextBox_StopLevelComprionValue.Text = "1.0"
            '--------------------
            ' Change the selection of the level comparison direction combobox to 0
            '--------------------
            ComboBox_StopLevelComprionDriection.SelectedIndex = 0
        End If
        '----------------------------------------
        ' Update the initial display of channel No.
        '----------------------------------------
        TextBox_StopRangeChNo.Text = "0"
        '----------------------------------------
        ' For In Range comparison
        '----------------------------------------
        If (ai_stop_trigger = 5) Then
            '----------------------------------------
            ' Get the settings of In Range comparison stop condition
            '----------------------------------------
            ret = AioGetAiStopInRangeEx(Form1.g_id, 0, ai_range_level1, ai_range_level2, ai_state_times)
            '----------------------------------------
            ' Abnormal termination process
            '----------------------------------------
            If (ret <> 0) Then
                If (ret <> 20001) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Display message
                    '--------------------
                    message_string = String.Format("AioGetAiStopInRangeEx = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                    err_flag = 1
                Else
                    '--------------------
                    ' Disable control
                    '--------------------
                    TextBox_StopRangeChNo.Enabled = FALSE
                    TextBox_StopRangeUpperLimitValue.Enabled = FALSE
                    TextBox_StopRangeLowerLimitValue.Enabled = FALSE
                    TextBox_StopRangeStateTimes.Enabled = FALSE
                End If
                '--------------------
                ' Update the initial display of upper limit
                '--------------------
                TextBox_StopRangeUpperLimitValue.Text = "10.0"
                '--------------------
                ' Update the initial display of lower limit
                '--------------------
                TextBox_StopRangeLowerLimitValue.Text = "1.0"
                '--------------------
                ' Update the initial display of state-holding times
                '--------------------
                TextBox_StopRangeStateTimes.Text = "1"
            '----------------------------------------
            ' Normal termination process
            '----------------------------------------
            Else
                '--------------------
                ' Update upper limit
                '--------------------
                TextBox_StopRangeUpperLimitValue.Text = ai_range_level1.ToString("F1")
                '--------------------
                ' Update lower limit
                '--------------------
                TextBox_StopRangeLowerLimitValue.Text = ai_range_level2.ToString("F1")
                '--------------------
                ' Update state-holding times
                '--------------------
                TextBox_StopRangeStateTimes.Text = ai_state_times.ToString()
            End If
        '----------------------------------------
        ' For Out Range comparison
        '----------------------------------------
        ElseIf (ai_stop_trigger = 6) Then
            '----------------------------------------
            ' Get the settings of Out Range comparison stop condition
            '----------------------------------------
            ret = AioGetAiStopOutRangeEx(Form1.g_id, 0, ai_range_level1, ai_range_level2, ai_state_times)
            '----------------------------------------
            ' Abnormal termination process
            '----------------------------------------
            If (ret <> 0) Then
                If (ret <> 20001) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Display message
                    '--------------------
                    message_string = String.Format("AioGetAiStopOutRangeEx = {0:D} : {1}{2}{3}*The display could not be updated since an error occurred.", ret, error_string, vbCrLf, vbCrLf)
                    err_flag = 1
                Else
                    '--------------------
                    ' Disable control
                    '--------------------
                    TextBox_StopRangeChNo.Enabled = FALSE
                    TextBox_StopRangeUpperLimitValue.Enabled = FALSE
                    TextBox_StopRangeLowerLimitValue.Enabled = FALSE
                    TextBox_StopRangeStateTimes.Enabled = FALSE
                End If
                '--------------------
                ' Update the initial display of upper limit
                '--------------------
                TextBox_StopRangeUpperLimitValue.Text = "10.0"
                '--------------------
                ' Update the initial display of lower limit
                '--------------------
                TextBox_StopRangeLowerLimitValue.Text = "1.0"
                '--------------------
                ' Update the initial display of state-holding times
                '--------------------
                TextBox_StopRangeStateTimes.Text = "1"
            '----------------------------------------
            ' Normal termination process
            '----------------------------------------
            Else
                '--------------------
                ' Update upper limit
                '--------------------
                TextBox_StopRangeUpperLimitValue.Text = ai_range_level1.ToString("F1")
                '--------------------
                ' Update lower limit
                '--------------------
                TextBox_StopRangeLowerLimitValue.Text = ai_range_level2.ToString("F1")
                '--------------------
                ' Update state-holding times
                '--------------------
                TextBox_StopRangeStateTimes.Text = ai_state_times.ToString()
            End If
        '----------------------------------------
        ' Other than In Range comparison / Out Range comparison
        '----------------------------------------
        Else
            '--------------------
            ' Update the initial display of upper limit
            '--------------------
            TextBox_StopRangeUpperLimitValue.Text = "10.0"
            '--------------------
            ' Update the initial display of lower limit
            '--------------------
            TextBox_StopRangeLowerLimitValue.Text = "1.0"
            '--------------------
            ' Update the initial display of state-holding times
            '--------------------
            TextBox_StopRangeStateTimes.Text = "1"
        End If
        '----------------------------------------
        ' 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 Start Condition combobox is changed
    ' Update available controls
    '================================================================================
    Private Sub ComboBox_StartCondition_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_StartCondition.SelectedIndexChanged
        Dim index As Integer    ' Index of combobox

        '----------------------------------------
        ' Get the index of start condition from combobox
        '----------------------------------------
        index = ComboBox_StartCondition.SelectedIndex
        '----------------------------------------
        ' Branch processing by start condition
        '----------------------------------------
        Select Case g_start_trigger(index)
            Case 0, 1, 2    ' Software, External trigger rising edge, External trigger falling edge
                TextBox_StartLevelChNo.Enabled = False
                TextBox_StartLevelComprionValue.Enabled = False
                ComboBox_StartLevelComprionDriection.Enabled = False
                TextBox_StartRangeChNo.Enabled = False
                TextBox_StartRangeUpperLimitValue.Enabled = False
                TextBox_StartRangeLowerLimitValue.Enabled = False
                TextBox_StartRangeStateTimes.Enabled = False
            Case 3    ' Level comparison
                TextBox_StartLevelChNo.Enabled = True
                TextBox_StartLevelComprionValue.Enabled = True
                ComboBox_StartLevelComprionDriection.Enabled = True
                TextBox_StartRangeChNo.Enabled = False
                TextBox_StartRangeUpperLimitValue.Enabled = False
                TextBox_StartRangeLowerLimitValue.Enabled = False
                TextBox_StartRangeStateTimes.Enabled = False
            Case 4, 5    ' In Range comparison, Out Range comparison
                TextBox_StartLevelChNo.Enabled = False
                TextBox_StartLevelComprionValue.Enabled = False
                ComboBox_StartLevelComprionDriection.Enabled = False
                TextBox_StartRangeChNo.Enabled = True
                TextBox_StartRangeUpperLimitValue.Enabled = True
                TextBox_StartRangeLowerLimitValue.Enabled = True
                TextBox_StartRangeStateTimes.Enabled = True
            Case Else
                TextBox_StartLevelChNo.Enabled = False
                TextBox_StartLevelComprionValue.Enabled = False
                ComboBox_StartLevelComprionDriection.Enabled = False
                TextBox_StartRangeChNo.Enabled = False
                TextBox_StartRangeUpperLimitValue.Enabled = False
                TextBox_StartRangeLowerLimitValue.Enabled = False
                TextBox_StartRangeStateTimes.Enabled = False
        End Select
    End Sub

    '================================================================================
    ' It is the process when the Clock Type combobox is changed
    ' Update available controls
    '================================================================================
    Private Sub ComboBox_ClockType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_ClockType.SelectedIndexChanged
        Dim index As Integer            ' Index of combobox
        Dim ret As Integer              ' For return code
        Dim auto_mode As Short          ' Set mode of transfer size
        Dim transfer_size As Integer    ' Transfer size

        '----------------------------------------
        ' Get the index of clock type from combobox
        '----------------------------------------
        index = ComboBox_ClockType.SelectedIndex
        '----------------------------------------
        ' Branch processing by clock type
        '----------------------------------------
        Select Case g_clock_type(index)
            Case 0    ' Internal clock
                TextBox_SamplingClock.Enabled = True
                ComboBox_ExternalClockInputTiming.Enabled = False
                TextBox_TransferSize.Enabled = False
            Case 1    ' External clock
                TextBox_SamplingClock.Enabled = False
                ComboBox_ExternalClockInputTiming.Enabled = True
                '----------------------------------------
                ' Get transfer size
                '----------------------------------------
                ret = AioGetAiTransferSize(Form1.g_id, auto_mode, transfer_size)
                '----------------------------------------
                ' Abnormal termination process
                '----------------------------------------
                If (ret <> 0) Then
                    TextBox_TransferSize.Enabled = False
                '----------------------------------------
                ' Normal termination process
                '----------------------------------------
                Else
                    TextBox_TransferSize.Enabled = True
                End If
            Case Else
                TextBox_SamplingClock.Enabled = True
                ComboBox_ExternalClockInputTiming.Enabled = False
                TextBox_TransferSize.Enabled = False
        End Select
    End Sub

    '================================================================================
    ' It is the process when the Stop Condition combobox is changed
    ' Update available controls
    '================================================================================
    Private Sub ComboBox_StopCondition_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_StopCondition.SelectedIndexChanged
        Dim index As Integer    ' Index of combobox

        '----------------------------------------
        ' Get the index of stop condition from combobox
        '----------------------------------------
        index = ComboBox_StopCondition.SelectedIndex
        '----------------------------------------
        ' Branch processing by stop condition
        '----------------------------------------
        Select Case g_stop_trigger(index)
            Case 1, 2, 4    ' External trigger rising edge, External trigger falling edge, Command (AioStopAi)
                TextBox_StopLevelChNo.Enabled = False
                TextBox_StopLevelComprionValue.Enabled = False
                ComboBox_StopLevelComprionDriection.Enabled = False
                TextBox_StopRangeChNo.Enabled = False
                TextBox_StopRangeUpperLimitValue.Enabled = False
                TextBox_StopRangeLowerLimitValue.Enabled = False
                TextBox_StopRangeStateTimes.Enabled = False
            Case 3    ' Level comparison
                TextBox_StopLevelChNo.Enabled = True
                TextBox_StopLevelComprionValue.Enabled = True
                ComboBox_StopLevelComprionDriection.Enabled = True
                TextBox_StopRangeChNo.Enabled = False
                TextBox_StopRangeUpperLimitValue.Enabled = False
                TextBox_StopRangeLowerLimitValue.Enabled = False
                TextBox_StopRangeStateTimes.Enabled = False
            Case 5, 6    ' In Range comparison, Out Range comparison
                TextBox_StopLevelChNo.Enabled = False
                TextBox_StopLevelComprionValue.Enabled = False
                ComboBox_StopLevelComprionDriection.Enabled = False
                TextBox_StopRangeChNo.Enabled = True
                TextBox_StopRangeUpperLimitValue.Enabled = True
                TextBox_StopRangeLowerLimitValue.Enabled = True
                TextBox_StopRangeStateTimes.Enabled = True
            Case Else
                TextBox_StopLevelChNo.Enabled = False
                TextBox_StopLevelComprionValue.Enabled = False
                ComboBox_StopLevelComprionDriection.Enabled = False
                TextBox_StopRangeChNo.Enabled = False
                TextBox_StopRangeUpperLimitValue.Enabled = False
                TextBox_StopRangeLowerLimitValue.Enabled = False
                TextBox_StopRangeStateTimes.Enabled = False
        End Select
    End Sub

    '================================================================================
    ' It is the process when the OK button is clicked
    ' Set sampling condition
    '================================================================================
    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 index As Integer                              ' Index of combobox
        Dim ai_level_channel As Short                     ' evel comparison channel
        Dim ai_level As Single                            ' evel comparison data
        Dim ai_direction As Short                         ' Level comparison direction
        Dim ai_range_channel As Short                     ' Channel of In Range / Out Range comparison
        Dim ai_range_level1 As Single                     ' Upper limit data of In Range / Out Range comparison
        Dim ai_range_level2 As Single                     ' Lower limit data of In Range / Out Range comparison
        Dim ai_state_times As Integer                     ' Sampling times of the state-holding
        Dim ai_sampling_clock As Single                   ' Sampling clock
        Dim ai_scan_clock As Single                       ' Conversion speed
        Dim ai_transfer_size As Integer                   ' Transfer size

        '----------------------------------------
        ' Set the data storage memory type to Overwrite the memory
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAiMemoryType(Form1.g_id, 1)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            text_string = String.Format("AioSetAiMemoryType = {0:D} : {1}", ret, error_string)
            MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' Get the index of the conversion start condition setting value from combobox
        '----------------------------------------
        index = ComboBox_StartCondition.SelectedIndex
        '----------------------------------------
        ' Set analog input conversion start condition
        '----------------------------------------
        ret = AioSetAiStartTrigger(Form1.g_id, g_start_trigger(index))
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            text_string = String.Format("AioSetAiStartTrigger = {0:D} : {1}", ret, error_string)
            MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' For Level comparison
        '----------------------------------------
        If (g_start_trigger(index) = 3) Then
            '----------------------------------------
            ' Get the Level comparison channel from textbox
            '----------------------------------------
            If (Short.TryParse(TextBox_StartLevelChNo.Text, ai_level_channel) = False) Then
                ai_level_channel = 0
            End If
            '----------------------------------------
            ' Get the Level comparison data from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_StartLevelComprionValue.Text, ai_level) = False) Then
                ai_level = -10
            End If
            '----------------------------------------
            ' Get the Level comparison direction from textbox
            '----------------------------------------
            ai_direction = ComboBox_StartLevelComprionDriection.SelectedIndex
            '----------------------------------------
            ' Set the level at start of level comparison
            '----------------------------------------
            ret = AioSetAiStartLevelEx(Form1.g_id, ai_level_channel, ai_level, ai_direction)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = String.Format("AioSetAiStartLevelEx = {0:D} : {1}", ret, error_string)
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        '----------------------------------------
        ' For In Range comparison
        '----------------------------------------
        ElseIf (g_start_trigger(index) = 4) Then
            '----------------------------------------
            ' Get the channel of In Range comparison from textbox
            '----------------------------------------
            If (Short.TryParse(TextBox_StartRangeChNo.Text, ai_range_channel) = False) Then
                ai_range_channel = 0
            End If
            '----------------------------------------
            ' Get the upper limit data of In Range comparison from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_StartRangeUpperLimitValue.Text, ai_range_level1) = False) Then
                ai_range_level1 = 10
            End If
            '----------------------------------------
            ' Get the lower limit data of In Range comparison from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_StartRangeLowerLimitValue.Text, ai_range_level2) = False) Then
                ai_range_level2 = 1
            End If
            '----------------------------------------
            ' Get the state-holding times from textbox
            '----------------------------------------
            If (Integer.TryParse(TextBox_StartRangeStateTimes.Text, ai_state_times) = False) Then
                ai_state_times = 1
            End If
            '----------------------------------------
            ' Set In Range comparison start condition
            '----------------------------------------
            ret = AioSetAiStartInRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = String.Format("AioSetAiStartInRangeEx = {0:D} : {1}", ret, error_string)
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        '----------------------------------------
        ' For Out Range comparison
        '----------------------------------------
        ElseIf (g_start_trigger(index) = 5) Then
            '----------------------------------------
            ' Get the channel of Out Range comparison from textbox
            '----------------------------------------
            If (Short.TryParse(TextBox_StartRangeChNo.Text, ai_range_channel) = False) Then
                ai_range_channel = 0
            End If
            '----------------------------------------
            ' Get the upper limit data of In Range comparison from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_StartRangeUpperLimitValue.Text, ai_range_level1) = False) Then
                ai_range_level1 = 10
            End If
            '----------------------------------------
            ' Get the lower limit data of In Range comparison from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_StartRangeLowerLimitValue.Text, ai_range_level2) = False) Then
                ai_range_level2 = 1
            End If
            '----------------------------------------
            ' Get the state-holding times from textbox
            '----------------------------------------
            If (Integer.TryParse(TextBox_StartRangeStateTimes.Text, ai_state_times) = False) Then
                ai_state_times = 1
            End If
            '----------------------------------------
            ' Set Out Range comparison start condition
            '----------------------------------------
            ret = AioSetAiStartOutRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = String.Format("AioSetAiStartOutRangeEx = {0:D} : {1}", ret, error_string)
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        End If
        '----------------------------------------
        ' Get the index of the clock type setting value from combobox
        '----------------------------------------
        index = ComboBox_ClockType.SelectedIndex
        '----------------------------------------
        ' Set analog input clock type
        '----------------------------------------
        ret = AioSetAiClockType(Form1.g_id, g_clock_type(index))
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            text_string = String.Format("AioSetAiClockType = {0:D} : {1}", ret, error_string)
            MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' For Internal clock
        '----------------------------------------
        If (g_clock_type(index) = 0) Then
            '----------------------------------------
            ' Get the sampling clock from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_SamplingClock.Text, ai_sampling_clock) = False) Then
                ai_sampling_clock = 1000
            End If
            '----------------------------------------
            ' Set analog input sampling clock
            '----------------------------------------
            ret = AioSetAiSamplingClock(Form1.g_id, ai_sampling_clock)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = String.Format("AioSetAiSamplingClock = {0:D} : {1}", ret, error_string)
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
            '----------------------------------------
            ' Set analog input transfer size
            '----------------------------------------
            ret = AioSetAiTransferSize(Form1.g_id, AI_TRANSFERSIZE_AUTO, 0)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If ((ret <> 0) And (ret <> 20001)) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = String.Format("AioSetAiTransferSize = {0:D} : {1}", ret, error_string)
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        '----------------------------------------
        ' For External clock
        '----------------------------------------
        ElseIf (g_clock_type(index) = 1) Then
            '----------------------------------------
            ' Get the index of the external clock input timing setting value from combobox
            '----------------------------------------
            index = ComboBox_ExternalClockInputTiming.SelectedIndex
            '----------------------------------------
            ' Set the timing of external clock input
            '----------------------------------------
            ret = AioSetAiClockEdge(Form1.g_id, g_clock_edge(index))
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If ((ret <> 0) And (ret <> 20001)) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = String.Format("AioSetAiClockEdge = {0:D} : {1}", ret, error_string)
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
            '----------------------------------------
            ' Get the transfer size from textbox
            '----------------------------------------
            If (Integer.TryParse(TextBox_TransferSize.Text, ai_transfer_size) = False) Then
                ai_transfer_size = 1
            End If
            '----------------------------------------
            ' Set analog input transfer size
            '----------------------------------------
            ret = AioSetAiTransferSize(Form1.g_id, AI_TRANSFERSIZE_MANUAL, ai_transfer_size)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If ((ret <> 0) And (ret <> 20001)) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = String.Format("AioSetAiTransferSize = {0:D} : {1}", ret, error_string)
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        End If
        '----------------------------------------
        ' When conversion speed function is available
        '----------------------------------------
        If (TextBox_ScanClock.Enabled <> False) Then
            '----------------------------------------
            ' Get the conversion speed from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_ScanClock.Text, ai_scan_clock) = False) Then
                ai_scan_clock = 2
            End If
            '----------------------------------------
            ' Set analog input conversion speed
            '----------------------------------------
            ret = AioSetAiScanClock(Form1.g_id, ai_scan_clock)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = String.Format("AioSetAiScanClock = {0:D} : {1}", ret, error_string)
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        End If
        '----------------------------------------
        ' Get the index of the conversion stop condition setting value from combobox
        '----------------------------------------
        index = ComboBox_StopCondition.SelectedIndex
        '----------------------------------------
        ' Set analog input conversion stop condition
        '----------------------------------------
        ret = AioSetAiStopTrigger(Form1.g_id, g_stop_trigger(index))
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            text_string = String.Format("AioSetAiStopTrigger = {0:D} : {1}", ret, error_string)
            MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' For Level comparison
        '----------------------------------------
        If (g_stop_trigger(index) = 3) Then
            '----------------------------------------
            ' Get the Level comparison channel from textbox
            '----------------------------------------
            If (Short.TryParse(TextBox_StopLevelChNo.Text, ai_level_channel) = False) Then
                ai_level_channel = 0
            End If
            '----------------------------------------
            ' Get the Level comparison data from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_StopLevelComprionValue.Text, ai_level) = False) Then
                ai_level = -10
            End If
            '----------------------------------------
            ' Get the Level comparison direction from textbox
            '----------------------------------------
            ai_direction = ComboBox_StopLevelComprionDriection.SelectedIndex
            '----------------------------------------
            ' Set the level at stop of level comparison
            '----------------------------------------
            ret = AioSetAiStopLevelEx(Form1.g_id, ai_level_channel, ai_level, ai_direction)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = String.Format("AioSetAiStopLevelEx = {0:D} : {1}", ret, error_string)
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        '----------------------------------------
        ' For In Range comparison
        '----------------------------------------
        ElseIf (g_stop_trigger(index) = 5) Then
            '----------------------------------------
            ' Get the channel of In Range comparison from textbox
            '----------------------------------------
            If (Short.TryParse(TextBox_StopRangeChNo.Text, ai_range_channel) = False) Then
                ai_range_channel = 0
            End If
            '----------------------------------------
            ' Get the upper limit data of In Range comparison from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_StopRangeUpperLimitValue.Text, ai_range_level1) = False) Then
                ai_range_level1 = 10
            End If
            '----------------------------------------
            ' Get the lower limit data of In Range comparison from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_StopRangeLowerLimitValue.Text, ai_range_level2) = False) Then
                ai_range_level2 = 1
            End If
            '----------------------------------------
            ' Get the state-holding times from textbox
            '----------------------------------------
            If (Integer.TryParse(TextBox_StopRangeStateTimes.Text, ai_state_times) = False) Then
                ai_state_times = 1
            End If
            '----------------------------------------
            ' Set In Range comparison stop condition
            '----------------------------------------
            ret = AioSetAiStopInRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = String.Format("AioSetAiStopInRangeEx = {0:D} : {1}", ret, error_string)
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK)
                Exit Sub
            End If
        '----------------------------------------
        ' For Out Range comparison
        '----------------------------------------
        ElseIf (g_stop_trigger(index) = 6) Then
            '----------------------------------------
            ' Get the channel of Out Range comparison from textbox
            '----------------------------------------
            If (Short.TryParse(TextBox_StopRangeChNo.Text, ai_range_channel) = False) Then
                ai_range_channel = 0
            End If
            '----------------------------------------
            ' Get the upper limit data of In Range comparison from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_StopRangeUpperLimitValue.Text, ai_range_level1) = False) Then
                ai_range_level1 = 10
            End If
            '----------------------------------------
            ' Get the lower limit data of In Range comparison from textbox
            '----------------------------------------
            If (Single.TryParse(TextBox_StopRangeLowerLimitValue.Text, ai_range_level2) = False) Then
                ai_range_level2 = 1
            End If
            '----------------------------------------
            ' Get the state-holding times from textbox
            '----------------------------------------
            If (Integer.TryParse(TextBox_StopRangeStateTimes.Text, ai_state_times) = False) Then
                ai_state_times = 1
            End If
            '----------------------------------------
            ' Set Out Range comparison stop condition
            '----------------------------------------
            ret = AioSetAiStopOutRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            '--------------------
            ' Exit the process if not completed successfully
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                text_string = String.Format("AioSetAiStopOutRangeEx = {0:D} : {1}", ret, error_string)
                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