﻿Public Class alarm
    Dim m_Id As Short

    ' Get the caller ID
    Public Sub SetId(ByVal id As Short)

        m_Id = id

    End Sub

    ' Initialize dialog
    Private Sub alarm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        txtChannel.Text = "0"
        txtUU.Text = "0"
        txtUL.Text = "0"
        txtLU.Text = "0"
        txtLL.Text = "0"

        ' Set the channel number to 0
        cboAlarm.SelectedIndex = 0
        btnGet_Click(btnGet, New System.EventArgs())

    End Sub

    ' Click the [Read] button
    Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click

        Dim ret As Integer
        Dim enable As Short
        Dim uu, ul, lu, ll As Integer

        ' Get the alarm setting status
        ret = PtGetAlarm(m_Id, Val(txtChannel.Text), enable, uu, ul, lu, ll)
        If ret <> 0 Then
            ErrorHandling("PtGetAlarm", ret)
            Exit Sub
        End If

        ' Reflect the setting status in dialog
        cboAlarm.SelectedIndex = enable
        txtUU.Text = uu / 100
        txtUL.Text = ul / 100
        txtLU.Text = lu / 100
        txtLL.Text = ll / 100

    End Sub

    ' Click the [Set] button
    Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSet.Click

        Dim ret As Integer
        Dim enable As Short
        Dim uu, ul, lu, ll As Integer

        ' Get the setting in the dialog
        enable = cboAlarm.SelectedIndex
        uu = Val(txtUU.Text) * 100
        ul = Val(txtUL.Text) * 100
        lu = Val(txtLU.Text) * 100
        ll = Val(txtLL.Text) * 100

        ' Set alarm
        ret = PtSetAlarm(m_Id, Val(txtChannel.Text), enable, uu, ul, lu, ll)
        If ret <> 0 Then
            ErrorHandling("PtSetAlarm", ret)
            Exit Sub
        End If

    End Sub

    ' Click the [Close] button
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Me.Close()

    End Sub

    ' Process error
    Private Sub ErrorHandling(ByVal Dfunction As String, ByVal ret As Integer)

        Dim errstr As New System.Text.StringBuilder("", 256)
        PtGetErrorString(ret, errstr)
        MsgBox(errstr.ToString(), MsgBoxStyle.OkOnly, Dfunction)

    End Sub
End Class