Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。
    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

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    ' Windows フォーム デザイナを使って変更してください。  
    ' コード エディタは使用しないでください。
    Friend WithEvents EndBtn As System.Windows.Forms.Button
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.EndBtn = New System.Windows.Forms.Button()
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.SuspendLayout()
        '
        'EndBtn
        '
        Me.EndBtn.Location = New System.Drawing.Point(96, 32)
        Me.EndBtn.Name = "EndBtn"
        Me.EndBtn.Size = New System.Drawing.Size(136, 32)
        Me.EndBtn.TabIndex = 0
        Me.EndBtn.Text = "終了"
        '
        'Timer1
        '
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(320, 102)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.EndBtn})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Form1"
        Me.Text = "TUSB-K16DIOサンプルプログラム"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private DevStatus(15) As Boolean ' Device Connection Flag TRUE : connecting
    Private IdDevForm(15) As KDIOForm ' Device Control Panel Form Object

    ' Main Form Load
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim DevId As Integer

        TUSBK16DIO_Load() 'Driver Load

        'Initialize
        For DevId = 0 To 15
            DevStatus(DevId) = False 'Device is not Connected
            IdDevForm(DevId) = New KDIOForm()   'Device Control Panel Form Create
        Next DevId

        ' Timer Start
        Timer1.Interval = 500
        Timer1.Enabled = True
    End Sub

    ' Main Form Unload ( Program exit )
    Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        Dim DevId As Integer

        Timer1.Enabled = False ' Timer Disable

        For DevId = 0 To 15
            IdDevForm(DevId) = Nothing ' Device Control Panel Unload
        Next DevId

        TUSBK16DIO_Unload() ' Driver Unload
    End Sub
    ' Device Control Panel Open
    Private Sub OpenPanel(ByVal Id As Short)
        IdDevForm(Id).Show() ' Form Show
        IdDevForm(Id).SetId(Id)
        DevStatus(Id) = True
    End Sub

    ' Timer Handler
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim DevId As Short

        For DevId = 0 To 15
            If DevStatus(DevId) = False Then ' Device is not connected
                If TUSBK16DIO_Chk(DevId) = 0 Then ' Connection check
                    OpenPanel(DevId) '
                End If
            Else 'Device is connected
                If IdDevForm(DevId).Visible = False Then ' Device is not visible
                    DevStatus(DevId) = False
                End If
            End If
        Next DevId
    End Sub

    ' Program End Command
    Private Sub EndBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndBtn.Click
        Me.Close()
    End Sub
End Class
