如何在vb.net中限制用户只能在文本框中输入整数

如何在vb.net中限制用户只能在文本框中输入整数,vb.net,textbox,integer,restriction,Vb.net,Textbox,Integer,Restriction,当用户想要计算他们的体重指数时,我如何限制他们,使他们在程序加载时只能在文本框中输入整数 如果可能的话,我如何将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 t

当用户想要计算他们的体重指数时,我如何限制他们,使他们在程序加载时只能在文本框中输入整数

如果可能的话,我如何将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
这就是设计

我知道这可能很简单,但我对编程相当陌生,谢谢

参考


图片不是你在这里发布代码的方式,请编辑你的问题并发布代码。好的,当然,对不起,我认为带有代码的图片就足够了:)使用NumericUpDown控件。无论“e.KeyChar”和“e.handled”在哪里,都会出现蓝色下划线。上面写着“不是'System.EventArgs'的成员。你把它放在哪一个事件中?在你的文本框的按键事件中添加代码。哦,我把它放在了“TextChanged”事件中。哈哈,然后我意识到你必须把它放在“KeyPress”事件中。谢谢你,它能工作!:D
 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