2016-03-02 79 views
-1

当用户想要计算他们的BMI时,如何限制他们,使他们只能在程序加载时在文本框中输入整数?如何限制用户只能在vb.net的文本框中输入整数

如果可能的话,我该如何将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 

那设计

enter image description here

我知道它可能很简单,但我是相当新的节目,谢谢!

+0

图片是不是你在这里发布代码的方式,请编辑您的问题和职位的代码。 –

+1

确定,对不起,我认为图像与代码将是足够的:) – Rushi

+0

使用NumericUpDown控件。 –

回答

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 
+0

无论“e.KeyChar”还是“e.handled”,它都会出现蓝色下划线。它说:“不是'System.EventArgs的记忆' – Rushi

+0

你在哪种情况下放了它? –

+0

在你的文本框的_KeyPress_事件中添加代码 –

相关问题