Dim c As Double 'we'll be entering decimal values
Dim L1 As Integer
Dim L2 As Integer
Dim measure(6) As String

Private Sub Form_Load()

    measure(0) = "inches"
    measure(1) = "feet"
    measure(2) = "yards"
    measure(3) = "millimetres"
    measure(4) = "centimetres"
    measure(5) = "metres"

    ' fill the listboxes

    For k = 0 To 5

        List1.AddItem measure(k)
        List2.AddItem measure(k)

    Next

End Sub

Private Sub Command1_Click()

    L1 = List1.ListIndex
    L2 = List2.ListIndex

    c = Val(Text1)

    If L1 = L2 Then

        Label1.Caption = "Conversion not required"

    Else

        ' look at the listindex for List1 and then go to the
        ' appropriate subroutine carrying the listindex for List2

        Select Case L1

            Case 0
                Call inches(L2)
            Case 1
                Call feet(L2)
            Case 2
                Call yards(L2)
            Case 3
                Call millimetres(L2)
            Case 4
                Call centimetres(L2)
            Case 5
                Call metres(L2)

        End Select

    End If

End Sub

Private Sub Command2_Click()

    'reset form to its load settings
    Unload convert_measure ' Unload form.
    Load convert_measure ' Load it.
    Show ' Show it.

End Sub

Public Sub inches(L2)

    ' Note number formatting to two decimal places

    Select Case L2

        Case 1
            Label1.Caption = c & " inches = " & Format(c / 12, "##,##0.00") & " feet"
        Case 2
            Label1.Caption = c & " inches = " & Format(c / 36, "##,##0.00") & " yards"
        Case 3
            Label1.Caption = c & " inches = " & Format(c * 25.6, "##,##0.00") & " millimetres"
        Case 4
            Label1.Caption = c & " inches = " & Format(c * 2.56, "##,##0.00") & " centimetres"
        Case 5
            Label1.Caption = c & " inches = " & Format(c * 0.0256, "##,##0.00") & " metres"

    End Select

End Sub

Public Sub feet(L2)

    Select Case L2

        Case 0
            Label1.Caption = c & " feet = " & Format(c * 12, "##,##0.00") & " inches"
        Case 2
            Label1.Caption = c & " feet = " & Format(c / 3, "##,##0.00") & " yards"
        Case 3
            Label1.Caption = c & " feet = " & Format(c * 307.2, "##,##0.00") & " millimetres"
        Case 4
            Label1.Caption = c & " feet = " & Format(c * 37.2, "##,##0.00") & " centimetres"
        Case 5
            Label1.Caption = c & " feet = " & Format(c * 0.3072, "##,##0.00") & " metres"

    End Select

End Sub

Public Sub yards(L2)

    Select Case L2

        Case 0
            Label1.Caption = c & " yards = " & Format(c * 36, "##,##0.00") & " inches"
        Case 1
            Label1.Caption = c & " yards = " & Format(c * 3, "##,##0.00") & " feet"
        Case 3
            Label1.Caption = c & " yards = " & Format(c * 921.6, "##,##0.00") & " millimetres"
        Case 4
            Label1.Caption = c & " yards = " & Format(c * 92.16, "##,##0.00") & " centimetres"
        Case 5
            Label1.Caption = c & " yards = " & Format(c * 0.9216, "##,##0.00") & " metres"

    End Select

End Sub

Public Sub millimetres(L2)

    'note the additional decimal places allowed

    Select Case L2

        Case 0
            Label1.Caption = c & " millimetres = " & Format(c / 25.6, "##,##0.0000") & " inches"
        Case 1
            Label1.Caption = c & " millimetres = " & Format(c / 307.2, "##,##0.0000") & " feet"
        Case 2
            Label1.Caption = c & " millimetres = " & Format(c / 921.6, "##,##0.0000") & " yards"
        Case 4
            Label1.Caption = c & " millimetres = " & Format(c / 10, "##,##0.0000") & " centimetres"
        Case 5
            Label1.Caption = c & " millimetres = " & Format(c / 1000, "##,##0.0000") & " metres"

    End Select

End Sub

Public Sub centimetres(L2)

    Select Case L2

        Case 0
            Label1.Caption = c & " centimetres = " & Format(c / 2.56, "##,##0.00") & " inches"
        Case 1
            Label1.Caption = c & " centimetres = " & Format(c / 30.72, "##,##0.00") & " feet"
        Case 2
            Label1.Caption = c & " centimetres = " & Format(c / 92.16, "##,##0.00") & " yards"
        Case 3
            Label1.Caption = c & " centimetres = " & Format(c * 10, "##,##0.00") & " millimetres"
        Case 5
            Label1.Caption = c & " centimetres = " & Format(c / 100, "##,##0.00") & " metres"

    End Select

End Sub

Public Sub metres(L2)

    Select Case L2

        Case 0
            Label1.Caption = c & " metres = " & Format(c / 0.0256, "##,##0.00") & " inches"
        Case 1
            Label1.Caption = c & " metres = " & Format(c / 0.3072, "##,##0.00") & " feet"
        Case 2
            Label1.Caption = c & " metres = " & Format(c / 0.9216, "##,##0.00") & " yards"
        Case 3
            Label1.Caption = c & " metres = " & Format(c * 1000, "##,##0.00") & " millimetres"
        Case 4
            Label1.Caption = c & " metres = " & Format(c * 100, "##,##0.00") & " centimetres"

    End Select

End Sub

Back to vb6 exercises