Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleConvert between units of distance (meter, fathom, mile, etc.)
Keywordsconvert, units, meter, mile, foot, fathom, centimeter
CategoriesAlgorithms
 
The program loads the unit names and the number of feet per unit from a text file. It uses those values to make the conversions.

This program's most interesting feature is the fact that it loads its data from a file. With a few changes, you could make the program accept new units (ells, microns, etc.) just by modifying the file.

With a little more work, you could add a unit type field to allow other types of units such as time, weight, area, and so forth.

Thanks to Ernie Jean.

 
Dim typecon(1 To 9) As String
Dim conversion(1 To 9) As Single
Dim orig As Single, des As Single, leng As Single
Dim answer As Single, numfeet As Single

Private Sub Form_Load()
Dim i As Integer

    Open App.Path & "\Lengths.txt" For Input As #1
    For i = 1 To 9
        Input #1, typecon(i), conversion(i)
    Next i
    Close #1
    For i = 1 To 2
        Load lblNumber(i)
        lblNumber(i).Left = lblNumber(i - 1).Left + _
            lblNumber(0).Width
        lblNumber(i).Top = lblNumber(0).Top
        lblNumber(i).Caption = (i + 1) & ". " & typecon(i + _
            1)
        lblNumber(i).Visible = True
    Next i
        Load lblNumber(3)
        lblNumber(3).Top = lblNumber(0).Top + _
            lblNumber(0).Height
        lblNumber(3).Caption = "4. " & typecon(4)
        lblNumber(3).Visible = True
    For i = 4 To 5
        Load lblNumber(i)
        lblNumber(i).Top = lblNumber(i - 3).Top + _
            lblNumber(i - 3).Height
        lblNumber(i).Left = lblNumber(i - 1).Left + _
            lblNumber(3).Width
        lblNumber(i).Caption = (i + 1) & ". " & typecon(i + _
            1)
        lblNumber(i).Visible = True
    Next i
        Load lblNumber(6)
        lblNumber(6).Top = lblNumber(3).Top + _
            lblNumber(3).Height
        lblNumber(6).Caption = "7. " & typecon(7)
        lblNumber(6).Visible = True
    For i = 7 To 8
        Load lblNumber(i)
        lblNumber(i).Top = lblNumber(i - 3).Top + _
            lblNumber(i - 3).Height
        lblNumber(i).Left = lblNumber(i - 1).Left + _
            lblNumber(3).Width
        lblNumber(i).Caption = (i + 1) & ". " & typecon(i + _
            1)
        lblNumber(i).Visible = True
    Next i
End Sub

Private Sub cmdConvert_Click()
    lblAnswer.Caption = ""
    orig = Val(txtOriginal.Text)
    des = Val(txtDesired.Text)
    leng = Val(txtLength.Text)
    ConvertToFeet
    ConvertToDesired
    lblAnswer.Caption = "The desired length is " & _
        Round(answer, 2)
End Sub

Public Sub ConvertToDesired()
    answer = numfeet / conversion(des)
End Sub

Public Sub ConvertToFeet()
    numfeet = leng * conversion(orig)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated