如何用vb 制作一个单位换算器呢?

1个回答

  • 我大概明白楼主的意思了,不知道对不对!

    楼主好像是需要把19个长度单位一一写入关系,然后换算,按照排列组合的原理,代码数量应该是惊人的.

    其实编程没有这么麻烦,不多的代码就搞定了!

    下面我简单写了一个例子,你看一看是否符合你的条件

    请在窗体上放上text1(0),text1(1),combo1(0),combo1(1),command1 这样几个控件

    Dim biaozhun(1) As Single,bianliang(1) As Single

    Private Sub Combo1_Click(Index As Integer)

    Select Case Combo1(Index).Text

    Case "千米"

    biaozhun(Index) = 1000

    Case "百米"

    biaozhun(Index) = 100

    Case "十米"

    biaozhun(Index) = 10

    Case "米"

    biaozhun(Index) = 1

    Case "分米"

    biaozhun(Index) = 0.1

    Case "厘米"

    biaozhun(Index) = 0.01

    Case "毫米"

    biaozhun(Index) = 0.001

    Case Else

    MsgBox "单位符号错误",vbOKOnly,"警告"

    Combo1(Index).ListIndex = 3

    End Select

    End Sub

    Private Sub Command1_Click()

    If IsNumeric(Text1(0).Text) = False Then MsgBox "请输入数字!",vbOKOnly,"警告":Exit Sub

    bianliang(0) = Val(Text1(0).Text)

    bianliang(1) = bianliang(0) * biaozhun(0) / biaozhun(1)

    Text1(1).Text = Trim(Str(bianliang(1)))

    MsgBox "换算成功",vbOKOnly,"恭喜"

    End Sub

    Private Sub Form_Load()

    For i = 0 To 1

    Combo1(i).AddItem "千米"

    Combo1(i).AddItem "百米"

    Combo1(i).AddItem "十米"

    Combo1(i).AddItem "米"

    Combo1(i).AddItem "分米"

    Combo1(i).AddItem "厘米"

    Combo1(i).AddItem "毫米"

    Combo1(i).ListIndex = 3

    Next i

    End Su