﻿Option Strict Off
Option Explicit On 
Imports System.Text

Friend Class ConditionDlg
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents STATIC1 As System.Windows.Forms.GroupBox
    Friend WithEvents STATICc As System.Windows.Forms.GroupBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents Label8 As System.Windows.Forms.Label
    Friend WithEvents Label11 As System.Windows.Forms.Label
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Combo_StartTrg As System.Windows.Forms.ComboBox
    Friend WithEvents Combo_ClockTrg As System.Windows.Forms.ComboBox
    Friend WithEvents Combo_StopTrg As System.Windows.Forms.ComboBox
    Friend WithEvents Edit_InternalClock As System.Windows.Forms.TextBox
    Friend WithEvents Edit_StopNum As System.Windows.Forms.TextBox
    Friend WithEvents Radio_WriteOnce As System.Windows.Forms.RadioButton
    Friend WithEvents Radio_WriteRing As System.Windows.Forms.RadioButton
    Friend WithEvents Button_Ok As System.Windows.Forms.Button
    Friend WithEvents Button_Cancel As System.Windows.Forms.Button
    Friend WithEvents Edit_NotifyNumber As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.STATIC1 = New System.Windows.Forms.GroupBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Combo_StartTrg = New System.Windows.Forms.ComboBox()
        Me.Combo_ClockTrg = New System.Windows.Forms.ComboBox()
        Me.Combo_StopTrg = New System.Windows.Forms.ComboBox()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label7 = New System.Windows.Forms.Label()
        Me.Label8 = New System.Windows.Forms.Label()
        Me.Label11 = New System.Windows.Forms.Label()
        Me.Edit_InternalClock = New System.Windows.Forms.TextBox()
        Me.Edit_StopNum = New System.Windows.Forms.TextBox()
        Me.STATICc = New System.Windows.Forms.GroupBox()
        Me.Radio_WriteOnce = New System.Windows.Forms.RadioButton()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Radio_WriteRing = New System.Windows.Forms.RadioButton()
        Me.Button_Ok = New System.Windows.Forms.Button()
        Me.Button_Cancel = New System.Windows.Forms.Button()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Edit_NotifyNumber = New System.Windows.Forms.TextBox()
        Me.STATIC1.SuspendLayout()
        Me.STATICc.SuspendLayout()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'STATIC1
        '
        Me.STATIC1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Combo_StartTrg, Me.Combo_ClockTrg, Me.Combo_StopTrg, Me.Label3, Me.Label4, Me.Label7, Me.Label8, Me.Label11, Me.Edit_InternalClock, Me.Edit_StopNum})
        Me.STATIC1.Location = New System.Drawing.Point(10, 8)
        Me.STATIC1.Name = "STATIC1"
        Me.STATIC1.Size = New System.Drawing.Size(486, 140)
        Me.STATIC1.TabIndex = 6
        Me.STATIC1.TabStop = False
        Me.STATIC1.Text = "Generation Conditions"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(6, 28)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(88, 20)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "Start Condition:"
        '
        'Combo_StartTrg
        '
        Me.Combo_StartTrg.Location = New System.Drawing.Point(104, 28)
        Me.Combo_StartTrg.Name = "Combo_StartTrg"
        Me.Combo_StartTrg.Size = New System.Drawing.Size(136, 21)
        Me.Combo_StartTrg.TabIndex = 2
        Me.Combo_StartTrg.Text = "ComboBox1"
        '
        'Combo_ClockTrg
        '
        Me.Combo_ClockTrg.Location = New System.Drawing.Point(104, 65)
        Me.Combo_ClockTrg.Name = "Combo_ClockTrg"
        Me.Combo_ClockTrg.Size = New System.Drawing.Size(136, 21)
        Me.Combo_ClockTrg.TabIndex = 3
        Me.Combo_ClockTrg.Text = "ComboBox1"
        '
        'Combo_StopTrg
        '
        Me.Combo_StopTrg.Location = New System.Drawing.Point(104, 102)
        Me.Combo_StopTrg.Name = "Combo_StopTrg"
        Me.Combo_StopTrg.Size = New System.Drawing.Size(136, 21)
        Me.Combo_StopTrg.TabIndex = 4
        Me.Combo_StopTrg.Text = "ComboBox1"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(6, 65)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(104, 23)
        Me.Label3.TabIndex = 1
        Me.Label3.Text = "Clock Condition:"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(6, 102)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(88, 18)
        Me.Label4.TabIndex = 1
        Me.Label4.Text = "Stop Condition:"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(260, 67)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(80, 19)
        Me.Label7.TabIndex = 1
        Me.Label7.Text = "Internal Clock:"
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(260, 104)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(80, 18)
        Me.Label8.TabIndex = 1
        Me.Label8.Text = "Stop Number:"
        '
        'Label11
        '
        Me.Label11.Location = New System.Drawing.Point(456, 71)
        Me.Label11.Name = "Label11"
        Me.Label11.Size = New System.Drawing.Size(16, 19)
        Me.Label11.TabIndex = 1
        Me.Label11.Text = "ns"
        '
        'Edit_InternalClock
        '
        Me.Edit_InternalClock.Location = New System.Drawing.Point(352, 65)
        Me.Edit_InternalClock.Name = "Edit_InternalClock"
        Me.Edit_InternalClock.Size = New System.Drawing.Size(96, 20)
        Me.Edit_InternalClock.TabIndex = 5
        Me.Edit_InternalClock.Text = "TextBox2"
        Me.Edit_InternalClock.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'Edit_StopNum
        '
        Me.Edit_StopNum.Location = New System.Drawing.Point(352, 102)
        Me.Edit_StopNum.Name = "Edit_StopNum"
        Me.Edit_StopNum.Size = New System.Drawing.Size(96, 20)
        Me.Edit_StopNum.TabIndex = 6
        Me.Edit_StopNum.Text = "TextBox2"
        Me.Edit_StopNum.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'STATICc
        '
        Me.STATICc.Controls.AddRange(New System.Windows.Forms.Control() {Me.Radio_WriteOnce, Me.Label1, Me.Radio_WriteRing})
        Me.STATICc.Location = New System.Drawing.Point(10, 157)
        Me.STATICc.Name = "STATICc"
        Me.STATICc.Size = New System.Drawing.Size(486, 56)
        Me.STATICc.TabIndex = 8
        Me.STATICc.TabStop = False
        Me.STATICc.Text = "Set Buffer Condition"
        '
        'Radio_WriteOnce
        '
        Me.Radio_WriteOnce.Checked = True
        Me.Radio_WriteOnce.Location = New System.Drawing.Point(112, 24)
        Me.Radio_WriteOnce.Name = "Radio_WriteOnce"
        Me.Radio_WriteOnce.Size = New System.Drawing.Size(80, 16)
        Me.Radio_WriteOnce.TabIndex = 7
        Me.Radio_WriteOnce.TabStop = True
        Me.Radio_WriteOnce.Text = "Write Once"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(24, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(72, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Buffer Write:"
        '
        'Radio_WriteRing
        '
        Me.Radio_WriteRing.Location = New System.Drawing.Point(216, 23)
        Me.Radio_WriteRing.Name = "Radio_WriteRing"
        Me.Radio_WriteRing.Size = New System.Drawing.Size(80, 19)
        Me.Radio_WriteRing.TabIndex = 8
        Me.Radio_WriteRing.Text = "Write Ring"
        '
        'Button_Ok
        '
        Me.Button_Ok.Location = New System.Drawing.Point(320, 289)
        Me.Button_Ok.Name = "Button_Ok"
        Me.Button_Ok.Size = New System.Drawing.Size(80, 27)
        Me.Button_Ok.TabIndex = 0
        Me.Button_Ok.Text = "OK"
        '
        'Button_Cancel
        '
        Me.Button_Cancel.Location = New System.Drawing.Point(416, 289)
        Me.Button_Cancel.Name = "Button_Cancel"
        Me.Button_Cancel.Size = New System.Drawing.Size(80, 27)
        Me.Button_Cancel.TabIndex = 1
        Me.Button_Cancel.Text = "Cancel"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label5, Me.Edit_NotifyNumber})
        Me.GroupBox1.Location = New System.Drawing.Point(10, 224)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(486, 55)
        Me.GroupBox1.TabIndex = 9
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Notification"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(8, 23)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(88, 17)
        Me.Label5.TabIndex = 1
        Me.Label5.Text = "Number of Data:"
        '
        'Edit_NotifyNumber
        '
        Me.Edit_NotifyNumber.Location = New System.Drawing.Point(104, 19)
        Me.Edit_NotifyNumber.Name = "Edit_NotifyNumber"
        Me.Edit_NotifyNumber.Size = New System.Drawing.Size(112, 20)
        Me.Edit_NotifyNumber.TabIndex = 9
        Me.Edit_NotifyNumber.Text = "TextBox1"
        Me.Edit_NotifyNumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'ConditionDlg
        '
        Me.AcceptButton = Me.Button_Ok
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(504, 323)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button_Ok, Me.STATICc, Me.STATIC1, Me.Button_Cancel, Me.GroupBox1})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "ConditionDlg"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Set Generating Conditions "
        Me.STATIC1.ResumeLayout(False)
        Me.STATICc.ResumeLayout(False)
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim Ret As Integer                      'Return code
    Dim szError As New StringBuilder("", 256)
    Public Shared NumberOfData As Integer
    Public Shared IsRing As Integer

    '=========================================================
    ' Form load
    '=========================================================
    Private Sub ConditionDlg_load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2

        '--------------------------------
        ' Screen item initialization
        '--------------------------------
        NumberOfData = 500
        Edit_InternalClock.Text = "1000"
        Edit_StopNum.Text = "1000"
        Edit_NotifyNumber.Text = Str(NumberOfData)
        '--------------------------------
        ' Start condition initialization
        '--------------------------------
        With Combo_StartTrg
            .Items.Clear()
            .Items.Insert(0, "Software Start")
            .Items.Insert(1, "External UP")
            .Items.Insert(2, "External Down")
            .SelectedIndex = 0
        End With
        '--------------------------------
        ' Clock condition initialization
        '--------------------------------
        With Combo_ClockTrg
            .Items.Clear()
            .Items.Insert(0, "Internal Clock")
            .Items.Insert(1, "External Clock")
            .Items.Insert(2, "Hand Shake")
            .SelectedIndex = 0
        End With
        '--------------------------------
        ' Stop condition initialization
        '--------------------------------
        With Combo_StopTrg
            .Items.Clear()
            .Items.Insert(0, "Software Stop")
            .Items.Insert(1, "External UP")
            .Items.Insert(2, "External Down")
            .Items.Insert(3, "Number")
            .SelectedIndex = 0
        End With
    End Sub

    '=========================================================
    ' Modify clock condition
    '=========================================================
    Private Sub COMBO_CLOCK_TRG_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Combo_ClockTrg.SelectedIndexChanged
        '--------------------------------
        ' Enable/disable internal clock
        '--------------------------------
        With Edit_InternalClock
            If Combo_ClockTrg.Text = "Internal Clock" Then                  'Enable
                .BackColor = System.Drawing.SystemColors.Window
                .Enabled = True
            Else                                                            'Disable
                .BackColor = System.Drawing.SystemColors.InactiveBorder
                .Enabled = False
            End If
        End With
    End Sub

    '=========================================================
    ' Modify stop condition
    '=========================================================
    Private Sub COMBO_STOP_TRG_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Combo_StopTrg.SelectedIndexChanged
        '--------------------------------
        ' Enable/disable stop number
        '--------------------------------
        With Edit_StopNum
            If Combo_StopTrg.Text = "Number" Then                           'Enable
                .BackColor = System.Drawing.SystemColors.Window
                .Enabled = True
            Else                                                            'Disable
                .BackColor = System.Drawing.SystemColors.InactiveBorder
                .Enabled = False
            End If
        End With
    End Sub

    '=========================================================
    ' OK button
    '=========================================================
    Private Sub BUTTON_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Ok.Click
        Dim StartTrg As Short
        Dim ClockTrg As Short
        Dim StopTrg As Short
        Dim Direction As Short = DIODM_DIR_OUT

        '--------------------------------
        ' parameter exchange
        '--------------------------------
        '--------------------------------
        ' Start condition
        '--------------------------------
        StartTrg = 0
        If Combo_StartTrg.Text = "Software Start" Then StartTrg = DIODM_START_SOFT
        If Combo_StartTrg.Text = "External UP" Then StartTrg = DIODM_START_EXT_RISE
        If Combo_StartTrg.Text = "External Down" Then StartTrg = DIODM_START_EXT_FALL
        '--------------------------------
        ' Clock condition
        '--------------------------------
        ClockTrg = 0
        If Combo_ClockTrg.Text = "Internal Clock" Then ClockTrg = DIODM_CLK_CLOCK
        If Combo_ClockTrg.Text = "External Clock" Then ClockTrg = DIODM_CLK_EXT_TRG
        If Combo_ClockTrg.Text = "Hand Shake" Then ClockTrg = DIODM_CLK_HANDSHAKE
        '--------------------------------
        ' Stop condition
        '--------------------------------
        StopTrg = 0
        If Combo_StopTrg.Text = "Software Stop" Then StopTrg = DIODM_STOP_SOFT
        If Combo_StopTrg.Text = "External UP" Then StopTrg = DIODM_STOP_EXT_RISE
        If Combo_StopTrg.Text = "External Down" Then StopTrg = DIODM_STOP_EXT_FALL
        If Combo_StopTrg.Text = "Number" Then StopTrg = DIODM_STOP_NUM
        '--------------------------------
        ' Save transfer condition
        '--------------------------------
        If Radio_WriteOnce.Checked = True Then
            IsRing = DIODM_WRITE_ONCE
        Else
            IsRing = DIODM_WRITE_RING
        End If
        '--------------------------------
        ' Save notify number
        '--------------------------------
        NumberOfData = Val(Edit_NotifyNumber.Text)
        '--------------------------------
        ' Set direction(output)
        '--------------------------------
        Ret = DioDmSetDirection(Generating.Id, PO_32)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetDirection Ret = " & Ret & ":" & szError.ToString(), 0, "Generating")
            Exit Sub
        End If
        '--------------------------------
        ' StandAlone
        '--------------------------------
        Ret = DioDmSetStandAlone(Generating.Id)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetStandAlone Ret = " & Ret & ":" & szError.ToString(), 0, "Generating")
            Exit Sub
        End If
        '--------------------------------
        ' Set start condition
        '--------------------------------
        Ret = DioDmSetStartTrigger(Generating.Id, Direction, StartTrg)
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetStartTrigger Ret = " & Ret & ":" & szError.ToString(), 0, "Generating")
            Exit Sub
        End If
        '--------------------------------
        ' Set clock condition
        '--------------------------------
        Ret = DioDmSetClockTrigger(Generating.Id, Direction, ClockTrg)
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetClockTrigger Ret = " & Ret & ":" & szError.ToString(), 0, "Generating")
            Exit Sub
        End If
        '--------------------------------
        ' Set stop condition
        '--------------------------------
        Ret = DioDmSetStopTrigger(Generating.Id, Direction, StopTrg)
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetStopTrigger Ret = " & Ret & ":" & szError.ToString(), 0, "Generating")
            Exit Sub
        End If
        '--------------------------------
        ' Set internal clock
        '--------------------------------
        Ret = DioDmSetInternalClock(Generating.Id, Direction, CLng(Edit_InternalClock.Text), DIODM_TIM_UNIT_NS)
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetInternalClock Ret = " & Ret & ":" & szError.ToString(), 0, "Generating")
            Exit Sub
        End If
        '--------------------------------
        ' Set stop number
        '--------------------------------
        Ret = DioDmSetStopNumber(Generating.Id, Direction, CLng(Edit_StopNum.Text))
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            MsgBox("DioDmSetStopNumber Ret = " & Ret & ":" & szError.ToString(), 0, "Generating")
            Exit Sub
        End If
        Me.Hide()
    End Sub

    '=========================================================
    ' Cancel button
    '=========================================================
    Private Sub BUTTON_CANCEL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Cancel.Click
        Hide()
    End Sub

End Class
