﻿Option Strict Off
Option Explicit On
Imports System.Text

Public Class Form1
    Dim CountId As Short

    '==============================================================================
    ' TimInit
    '==============================================================================
    Private Sub Button_TimInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_TimInit.Click
        Dim DeviceName As New String("", 256)
        Dim ProductName As New String("", 256)
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' デバイス名取得 (文字数を256に固定)
        '-----------------------------------
        DeviceName = Text_DeviceName.Text.PadRight(256, ControlChars.NullChar)
        '-----------------------------------
        ' 初期化
        '-----------------------------------
        Ret = TimInit(DeviceName, ProductName)
        '-----------------------------------
        ' エラー処理
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimInit Ret = " & Ret & " " & ErrorString.ToString()
        If Ret = TIM_ERR_SUCCESS Then
            Text_DeviceName.Text = DeviceName
            Label_ProductName.Text = ProductName
        End If
    End Sub

    '==============================================================================
    ' カウント開始
    '==============================================================================
    Private Sub Button_CountStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_CountStart.Click
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' カウント開始
        '-----------------------------------
        Ret = TimCountStart(CountId)
        '-----------------------------------
        ' エラー処理
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimCountStart Ret = " & Ret & " " & ErrorString.ToString()
    End Sub

    '==============================================================================
    ' ラップタイム取得
    '==============================================================================
    Private Sub Button_CountLap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_CountLap.Click
        Dim Microseconds As Integer
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' ラップタイム取得
        '-----------------------------------
        Ret = TimCountLap(CountId, Microseconds)
        '-----------------------------------
        ' エラー処理
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimCountLap Ret = " & Ret & " " & ErrorString.ToString()
        If Ret = TIM_ERR_SUCCESS Then
            Label_Time.Text = Microseconds.ToString
        End If
    End Sub

    '==============================================================================
    ' カウントリセット
    '==============================================================================
    Private Sub Button_CountReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_CountReset.Click
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' カウントリセット
        '-----------------------------------
        Ret = TimCountReset(CountId)
        '-----------------------------------
        ' エラー処理
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimCountReset Ret = " & Ret & " " & ErrorString.ToString()
    End Sub

    '==============================================================================
    ' カウント停止
    '==============================================================================
    Private Sub Button_CountStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_CountStop.Click
        Dim Microseconds As Integer
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' カウント停止
        '-----------------------------------
        Ret = TimCountStop(CountId, Microseconds)
        '-----------------------------------
        ' エラー処理
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimCountStop Ret = " & Ret & " " & ErrorString.ToString()
        If Ret = TIM_ERR_SUCCESS Then
            Label_Time.Text = Microseconds.ToString
        End If
    End Sub

    '==============================================================================
    ' TimExit
    '==============================================================================
    Private Sub Button_TimExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_TimExit.Click
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' 終了処理
        '-----------------------------------
        Ret = TimExit()
        '-----------------------------------
        ' エラー処理
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimExit Ret = " & Ret & " " & ErrorString.ToString()
    End Sub

    '==============================================================================
    ' 終了処理
    '==============================================================================
    Private Sub Button_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Exit.Click
        '-----------------------------------
        ' 終了処理
        '-----------------------------------
        Call TimExit()
        '-----------------------------------
        ' ダイアログ終了
        '-----------------------------------
        End
    End Sub
End Class
