如何限制用户只能在 vb.net 的文本框中输入整数
How to restrict user only being able to enter integer in textbox in vb.net
当用户想要计算他们的 BMI 时,如何限制他们在程序加载时只能在文本框中输入整数?
此外,如果可能,我怎样才能将 BMI 答案转换为小数点后 2 位或 1 位?
代码:
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
这就是设计
我知道它可能很简单,但我对编程还很陌生,谢谢!
参考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
当用户想要计算他们的 BMI 时,如何限制他们在程序加载时只能在文本框中输入整数?
此外,如果可能,我怎样才能将 BMI 答案转换为小数点后 2 位或 1 位?
代码:
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
这就是设计
我知道它可能很简单,但我对编程还很陌生,谢谢!
参考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