Public Class Form1

    '//// OUTPUT METHODS: ////
    Sub say(ByVal s As String)
        '''' Output a string ''''
        Me.ListBox1.Items.Add(s)
    End Sub
    Sub say(ByVal n As Integer)
        '''' Output a number.
        say(Format(n))
    End Sub
    Sub say(ByVal n As Double)
        '''' Output a number.
        say(Format(n))
    End Sub
    Sub saywhat(ByVal s As String, ByVal n As Integer)
        '''' Output a msg and a number.
        say(s & Format(n))
    End Sub
    Sub saywhat(ByVal s As String, ByVal n As Double)
        '''' Output a msg and a number.
        say(s & Format(n))
    End Sub

    '//// INPUT METHODS: ////
    Function getFirst() As Integer
        '''' Input value from first textbox.
        Return Val(Me.TextBox1.Text)
    End Function
    Function getSecond() As Integer
        '''' Input value from 2nd textbox.
        Return Val(Me.TextBox2.Text)
    End Function


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' Check if low & high are in order.
        Dim lo = Val(Me.TextBox1.Text)
        Dim hi = Val(Me.TextBox2.Text)
        If (lo <= hi) Then
            Me.ListBox1.Items.Add(" Numbers were in order.  (No swap.)")
        Else
            Me.BackColor = Color.Red
            Me.ListBox1.Items.Add(" Numbers were out of order")
            TextBox1.Text = hi
            TextBox1.Text = lo
        End If
    End Sub
    Private Sub GREEN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GREEN.Click
        '''' Change form color to green
        Me.BackColor = Color.Green
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '''' Change form color to yellow
        Me.BackColor = Color.Yellow
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        '''' Clear output
        Me.ListBox1.ClearSelected()
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        '''' Exit
        Me.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        '''' Count down from hi to lo
        Dim lo = Val(Me.TextBox1.Text)
        Dim hi = Val(Me.TextBox2.Text)
        Dim j As Integer
        For j = hi To lo Step -1
            say(j)
        Next
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        '''' Count up from lo to hi 
        Dim lo = Val(Me.TextBox1.Text)
        Dim hi = Val(Me.TextBox2.Text)
        Dim j As Integer
        For j = lo To hi
            say(j)
        Next
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        '''' Even only from lo to hi 
        Dim lo = Val(Me.TextBox1.Text)
        Dim hi = Val(Me.TextBox2.Text)
        Dim j As Integer
        For j = lo To hi
            If (isEven(j)) Then
                say(j)
            End If
        Next
    End Sub
    Function isEven(ByVal n As Integer) As Boolean
        '''' Return true iff n is EVEN (i.e. if remainder mod 2 is zero)
        If (n Mod 2) = 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        '''' Fibbonaci #s in specified range
        Dim lo = Val(Me.TextBox1.Text)  ' Low & High
        Dim hi = Val(Me.TextBox2.Text)
        Dim n1 As Integer = 1           ' First 2 Fib #s
        Dim n2 As Integer = 1
        Dim nextfib As Integer

        ' (Special case for first two)
        If (lo <= 1) Then say(n1)
        If (lo <= 21) Then say(n2)
        ' Loop to generate Fib #s (after the first two)
        Dim j As Integer
        For j = 3 To hi
            nextfib = n1 + n2       ' Next Fib. #
            n1 = n2                 ' Slide them down.
            n2 = nextfib
            If (j >= lo) Then
                say(nextfib)
            End If
        Next
    End Sub

End Class
