﻿Public Class alarm
    Dim m_Id As Short

    ' コール元からIDを取得
    Public Sub SetId(ByVal id As Short)

        m_Id = id

    End Sub

    ' ダイアログの初期化
    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"

        ' チャネル番号を0に設定
        cboAlarm.SelectedIndex = 0
        btnGet_Click(btnGet, New System.EventArgs())

    End Sub

    ' [読み込み]ボタンクリック
    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

        ' アラームの設定状態を取得
        ret = PtGetAlarm(m_Id, Val(txtChannel.Text), enable, uu, ul, lu, ll)
        If ret <> 0 Then
            ErrorHandling("PtGetAlarm", ret)
            Exit Sub
        End If

        ' 設定状態をダイアログに反映
        cboAlarm.SelectedIndex = enable
        txtUU.Text = uu / 100
        txtUL.Text = ul / 100
        txtLU.Text = lu / 100
        txtLL.Text = ll / 100

    End Sub

    ' [設定]ボタンクリック
    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

        ' ダイアログの設定内容を取得
        enable = cboAlarm.SelectedIndex
        uu = Val(txtUU.Text) * 100
        ul = Val(txtUL.Text) * 100
        lu = Val(txtLU.Text) * 100
        ll = Val(txtLL.Text) * 100

        ' アラームの設定
        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

    ' [閉じる]ボタンクリック
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Me.Close()

    End Sub

    ' エラー処理
    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