当用户想要计算他们的BMI时,如何限制他们,以便他们只能在程序加载时在文本框中输入整数?
如果可能,我怎样才能将BMI答案变成2或1位小数?
CODE:
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
txtHeight.Text = txtHeight.Text / 100 ' converts txtheight into meter
lblBMI.Text = txtWeight.Text / txtHeight.Text ^ 2 'BMI is equal to txtweight divided by (txtheight)^2
'BMI = x KG / (y M * y M)
End Sub
End Class
多数设计
我知道它可能非常简单,但我对编程很新,谢谢!
答案 0 :(得分:0)
参考here.
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'97 - 122 = Ascii codes for simple letters
'65 - 90 = Ascii codes for capital letters
'48 - 57 = Ascii codes for numbers
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
End Sub
将数字格式化为小数位:
YourLabel.Text = FormatNumber(YourNumber,N) 'N = Number of Decimal Places