Imports System.Windows.Forms

Public Class RgrTst_SetGetResponse

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Friend Property Response() As ResponsePoints()
        Get
            Dim rspPts(-1) As ResponsePoints
            If lstResponse.Items.Count > 0 Then
                ReDim rspPts(lstResponse.Items.Count - 1)
                For i As Integer = 0 To lstResponse.Items.Count - 1
                    rspPts(i).Frequency = CDbl(lstResponse.Items(i).ToString.Substring(0, 10))
                    rspPts(i).Amplitude = CDbl(lstResponse.Items(i).ToString.Substring(10, 15))
                Next
            End If
            Return rspPts
        End Get
        Set(ByVal value As ResponsePoints())
            Dim blnDup As Boolean

            For i As Integer = value.GetLowerBound(0) To value.GetUpperBound(0)
                If lstResponse.Items.Count > 0 Then
                    blnDup = False
                    'make sure we are not duplicating a frequency
                    For j As Integer = 0 To lstResponse.Items.Count - 1
                        If lstResponse.Items(j).ToString.Substring(0, 10).Trim = String.Format("{0,10:F3}", value(i).Frequency).Trim Then
                            blnDup = True
                            Exit For
                        End If
                    Next
                    If Not blnDup Then
                        lstResponse.Items.Add(String.Format("{0,10:F3}{1,15:F3}", value(i).Frequency, value(i).Amplitude))
                    End If
                Else
                    lstResponse.Items.Add(String.Format("{0,10:F3}{1,15:F3}", value(i).Frequency, value(i).Amplitude))
                End If
            Next
        End Set
    End Property

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If lstResponse.Items.Count > 0 Then
            'make sure we are not duplicating a frequency
            For i As Integer = 0 To lstResponse.Items.Count - 1
                If CDec(lstResponse.Items(i).ToString.Substring(0, 10)) = udFreq.Value Then
                    MessageBox.Show("Can't have duplicate frequencies...", "Respose", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return
                End If
            Next
        End If
        lstResponse.Items.Add(String.Format("{0,10:F3}{1,15:F3}", udFreq.Value, udCorr.Value))
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If lstResponse.SelectedIndex >= 0 Then
            lstResponse.Items.RemoveAt(lstResponse.SelectedIndex)
        End If
    End Sub
End Class
