Option Strict Off
Option Explicit On 
Imports System.Text

Public Class StopWatch
    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()

        'Add any initialization after the InitializeComponent() call

    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 Button_TimInit As System.Windows.Forms.Button
    Friend WithEvents Button_CountStart As System.Windows.Forms.Button
    Friend WithEvents Button_CountLap As System.Windows.Forms.Button
    Friend WithEvents Button_CountReset As System.Windows.Forms.Button
    Friend WithEvents Button_CountStop As System.Windows.Forms.Button
    Friend WithEvents Button_TimExit As System.Windows.Forms.Button
    Friend WithEvents Button_Exit As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Text_DeviceName As System.Windows.Forms.TextBox
    Friend WithEvents Label_ProductName As System.Windows.Forms.Label
    Friend WithEvents Label_Time As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Text_ReturnCode As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button_TimInit = New System.Windows.Forms.Button
        Me.Button_CountStart = New System.Windows.Forms.Button
        Me.Button_CountLap = New System.Windows.Forms.Button
        Me.Button_CountReset = New System.Windows.Forms.Button
        Me.Button_CountStop = New System.Windows.Forms.Button
        Me.Button_TimExit = New System.Windows.Forms.Button
        Me.Button_Exit = New System.Windows.Forms.Button
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.Label_ProductName = New System.Windows.Forms.Label
        Me.Text_DeviceName = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label_Time = New System.Windows.Forms.Label
        Me.GroupBox3 = New System.Windows.Forms.GroupBox
        Me.Text_ReturnCode = New System.Windows.Forms.TextBox
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.SuspendLayout()
        '
        'Button_TimInit
        '
        Me.Button_TimInit.Location = New System.Drawing.Point(256, 8)
        Me.Button_TimInit.Name = "Button_TimInit"
        Me.Button_TimInit.Size = New System.Drawing.Size(88, 20)
        Me.Button_TimInit.TabIndex = 0
        Me.Button_TimInit.Text = "TimInit"
        '
        'Button_CountStart
        '
        Me.Button_CountStart.Location = New System.Drawing.Point(256, 32)
        Me.Button_CountStart.Name = "Button_CountStart"
        Me.Button_CountStart.Size = New System.Drawing.Size(88, 20)
        Me.Button_CountStart.TabIndex = 1
        Me.Button_CountStart.Text = "CountStart"
        '
        'Button_CountLap
        '
        Me.Button_CountLap.Location = New System.Drawing.Point(256, 56)
        Me.Button_CountLap.Name = "Button_CountLap"
        Me.Button_CountLap.Size = New System.Drawing.Size(88, 20)
        Me.Button_CountLap.TabIndex = 2
        Me.Button_CountLap.Text = "CountLap"
        '
        'Button_CountReset
        '
        Me.Button_CountReset.Location = New System.Drawing.Point(256, 80)
        Me.Button_CountReset.Name = "Button_CountReset"
        Me.Button_CountReset.Size = New System.Drawing.Size(88, 20)
        Me.Button_CountReset.TabIndex = 3
        Me.Button_CountReset.Text = "CountReset"
        '
        'Button_CountStop
        '
        Me.Button_CountStop.Location = New System.Drawing.Point(256, 104)
        Me.Button_CountStop.Name = "Button_CountStop"
        Me.Button_CountStop.Size = New System.Drawing.Size(88, 20)
        Me.Button_CountStop.TabIndex = 4
        Me.Button_CountStop.Text = "CountStop"
        '
        'Button_TimExit
        '
        Me.Button_TimExit.Location = New System.Drawing.Point(256, 128)
        Me.Button_TimExit.Name = "Button_TimExit"
        Me.Button_TimExit.Size = New System.Drawing.Size(88, 20)
        Me.Button_TimExit.TabIndex = 5
        Me.Button_TimExit.Text = "TimExit"
        '
        'Button_Exit
        '
        Me.Button_Exit.Location = New System.Drawing.Point(256, 152)
        Me.Button_Exit.Name = "Button_Exit"
        Me.Button_Exit.Size = New System.Drawing.Size(88, 20)
        Me.Button_Exit.TabIndex = 6
        Me.Button_Exit.Text = "Exit"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.Label_ProductName)
        Me.GroupBox1.Controls.Add(Me.Text_DeviceName)
        Me.GroupBox1.Controls.Add(Me.Label2)
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(240, 72)
        Me.GroupBox1.TabIndex = 7
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Initialization"
        '
        'Label_ProductName
        '
        Me.Label_ProductName.Location = New System.Drawing.Point(88, 48)
        Me.Label_ProductName.Name = "Label_ProductName"
        Me.Label_ProductName.Size = New System.Drawing.Size(144, 16)
        Me.Label_ProductName.TabIndex = 3
        Me.Label_ProductName.Text = "Not Initialization"
        '
        'Text_DeviceName
        '
        Me.Text_DeviceName.Location = New System.Drawing.Point(88, 16)
        Me.Text_DeviceName.Name = "Text_DeviceName"
        Me.Text_DeviceName.Size = New System.Drawing.Size(112, 20)
        Me.Text_DeviceName.TabIndex = 2
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 48)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(80, 16)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "Product Name:"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 19)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(80, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Device Name:"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.Add(Me.Label4)
        Me.GroupBox2.Controls.Add(Me.Label_Time)
        Me.GroupBox2.Location = New System.Drawing.Point(8, 88)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(240, 80)
        Me.GroupBox2.TabIndex = 8
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Elapsed Time"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(168, 40)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(40, 16)
        Me.Label4.TabIndex = 1
        Me.Label4.Text = "[us]"
        '
        'Label_Time
        '
        Me.Label_Time.Location = New System.Drawing.Point(8, 40)
        Me.Label_Time.Name = "Label_Time"
        Me.Label_Time.Size = New System.Drawing.Size(152, 16)
        Me.Label_Time.TabIndex = 0
        Me.Label_Time.Text = "0"
        Me.Label_Time.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.Add(Me.Text_ReturnCode)
        Me.GroupBox3.Location = New System.Drawing.Point(8, 176)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(336, 48)
        Me.GroupBox3.TabIndex = 9
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "Ret"
        '
        'Text_ReturnCode
        '
        Me.Text_ReturnCode.Location = New System.Drawing.Point(8, 16)
        Me.Text_ReturnCode.Name = "Text_ReturnCode"
        Me.Text_ReturnCode.Size = New System.Drawing.Size(304, 20)
        Me.Text_ReturnCode.TabIndex = 0
        '
        'StopWatch
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(352, 230)
        Me.Controls.Add(Me.GroupBox3)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.Button_Exit)
        Me.Controls.Add(Me.Button_TimExit)
        Me.Controls.Add(Me.Button_CountStop)
        Me.Controls.Add(Me.Button_CountReset)
        Me.Controls.Add(Me.Button_CountLap)
        Me.Controls.Add(Me.Button_CountStart)
        Me.Controls.Add(Me.Button_TimInit)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "StopWatch"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Stop Watch"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox1.PerformLayout()
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.GroupBox3.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

#End Region
    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

        '-----------------------------------
        ' Get Device Name
        '-----------------------------------
        DeviceName = Text_DeviceName.Text.PadRight(256, ControlChars.NullChar)
        '-----------------------------------
        ' Initialization
        '-----------------------------------
        Ret = TimInit(DeviceName, ProductName)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        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

    '==============================================================================
    ' Start Count
    '==============================================================================
    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

        '-----------------------------------
        ' Start Count
        '-----------------------------------
        Ret = TimCountStart(CountId)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimCountStart Ret = " & Ret & " " & ErrorString.ToString()
    End Sub

    '==============================================================================
    ' Get elapsed time
    '==============================================================================
    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

        '-----------------------------------
        ' Get elapsed time
        '-----------------------------------
        Ret = TimCountLap(CountId, Microseconds)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        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

    '==============================================================================
    ' Reset count
    '==============================================================================
    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

        '-----------------------------------
        ' Reset count
        '-----------------------------------
        Ret = TimCountReset(CountId)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimCountReset Ret = " & Ret & " " & ErrorString.ToString()
    End Sub

    '==============================================================================
    ' Stop count
    '==============================================================================
    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

        '-----------------------------------
        ' Stop count
        '-----------------------------------
        Ret = TimCountStop(CountId, Microseconds)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        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

        '-----------------------------------
        ' Exit handle
        '-----------------------------------
        Ret = TimExit()
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimExit Ret = " & Ret & " " & ErrorString.ToString()
    End Sub

    '==============================================================================
    ' Exit
    '==============================================================================
    Private Sub Button_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Exit.Click
        '-----------------------------------
        'Exit handle
        '-----------------------------------
        Call TimExit()
        '-----------------------------------
        ' Exit
        '-----------------------------------
        End
    End Sub
End Class
