VB程序高手请进

设计“健康称”程序,具体要求如下:
(1)将两个文本框的文字对齐方式均设置为右对齐,最多接受3个字符;
(2)两个文本框均不接受非数字键
(3)单击“健康状况”按钮后,根据计算公式将相应提示信息通过标签显示在按钮下面。
计算公式为:标准体重=身高-105
体重高于标准体重的1.1倍为偏胖,提示“偏胖,加强锻炼,注意节食”;
体重低于标准体重的90%为偏瘦,提示“偏瘦,增加营养”;
其他为正常,提示“正常,继续保持”。

以下代码假设Text1输入身高(cm),Text2输入体重(kg):

Private Sub Form_Load()
Command1.Caption = "健康状况"
Text1.Alignment = 1
Text2.Alignment = 1
Text1.MaxLength = 3
Text2.MaxLength = 3
Text1.Text = ""
Text2.Text = ""
Label1.Caption = ""
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub

Private Sub Command1_Click()
Select Case Val(Text2.Text) / (Val(Text1.Text) - 105)
Case Is > 1.1
Label1.Caption = "偏胖,加强锻炼,注意节食"
Case Is < 0.9
Label1.Caption = "偏瘦,增加营养"
Case Else
Label1.Caption = "正常,继续保持"
End Select
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-23
text1是身高
text2是体重
Private Sub Command1_Click()
If Text2.Text > (Text1.Text - 105) * 1.1 Then
MsgBox "偏胖,加强锻炼,注意节食"
End If
If Text2.Text < (Text1.Text - 105) * 0.9 Then
MsgBox "偏瘦,增加营养"
End If
If Not Text2.Text > (Text1.Text - 105) * 1.1 And Not Text2.Text < (Text1.Text) Then
MsgBox "正常,继续保持"
End If
End Sub

Private Sub Form_Load()
Text1.MaxLength = 3
Text1.Text = ""
Text1.Alignment = 1
Text2.MaxLength = 3
Text2.Text = ""
Text2.Alignment = 1
End Sub

Private Sub Text1_Change()
On Error Resume Next
If Text1.Text = 0 Then
End If
If Err.Number = 13 Then
SendKeys "{Backspace}"
End If
End Sub

Private Sub Text2_Change()
On Error Resume Next
If Text2.Text = 0 Then
End If
If Err.Number = 13 Then
SendKeys "{Backspace}"
End If
End Sub
相似回答