vb急救!

来源:百度知道 编辑:UC知道 时间:2024/06/10 13:55:44
设计一个健康秤,界面如下图所示。具体要求:
(1)设置两个文本框文字对齐方式均为右对齐,最多接受3个字符,不接受非数字键。
(2)单击“健康状况”按钮,根据计算公式分别将提示信息显示在label5中。
计算公式为:标准体重=身高-105;体重>标准体重*1.1为偏胖,提示“偏胖,注意节食”;体重<标准体重*0.9为偏瘦,提示“偏瘦,增加营养”;其他为正常,提示'正常,继续保持”。

Private Sub Command1_Click()
If Val(Text1(1)) > (Val(Text1(0)) - 105) * 1.1 Then
Label5.Caption = "偏胖,注意节制"
ElseIf Val(Text1(1)) < (Val(Text1(0)) - 105) * 0.9 Then
Label5.Caption = "偏瘦,增加营养"
Else
Label5.Caption = "正常,继续保持"
End If
End Sub

Private Sub Form_Load()
Text1(0).Alignment = 1 '身高输入框
Text1(0).MaxLength = 3
Text1(1).Alignment = 1 '体重输入框
Text1(1).MaxLength = 3
End Sub

Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
'楼上的都没有这些
If KeyAscii < 48 Or KeyAscii > 57 And KeyAscii <> 8 Then KeyAscii = 0
'限制输入的必须是数字或者退格键
End Sub

完整代码如下

Private Sub Command1_Click()
If Val(Text2) > (Val(Text1) - 105) * 1.1 Then
Label5.Caption = "偏胖,注意节制"
ElseIf Val(Text2) < (Val(Text1) - 105) * 0.9 Then
Label5.Caption = "偏瘦,增加营养"
Else