Checking for numeric value entered in text box in Visual Basic

前端 未结 6 1250
醉酒成梦
醉酒成梦 2021-01-18 19:21

I am working on a program for my Visual Basic class and have a quick question. One of the things we were encouraged to do was to check to make sure the quantity entered in a

相关标签:
6条回答
  • 2021-01-18 19:29

    You're just using the function incorrectly - you need to pass the string as a parameter.

    If IsNumeric(txtQuantity.Text) Then
    
    0 讨论(0)
  • 2021-01-18 19:30
    Private Sub txbDwellTime_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txbDwellTime.KeyPress
    
        numDecOnly(e)
    
    End Sub
    
    
    Public Sub numDecOnly(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If (Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57 Or Asc(e.KeyChar) = 46) Then
            'good job do nothing we only allow positive Decimal numbers in this field
            'Asc(e.KeyChar) 48 Through 57 i.e. 0 through 9 Or Asc(e.KeyChar) 46 (dot= .)
        Else
            e.Handled = True
            MsgBox("Only Positive Decimal Numbers Allowed this field")
        End If
    
    End Sub
    
    0 讨论(0)
  • 2021-01-18 19:34

    A more correct way to do that is to use the TryParse method available in the Int32 or Double class

    If Double.TryParse(txtQuantity.Text, Quantity) Then
         If intCount < Quantity Then
             lstRecipe.Items.Add(Quantity & " " & lstIngredients.Text)
              intCount += 1
         End If
     Else
         MessageBox.Show("The quantity entered is not numeric. Please add a numeric    quantity.")
     End If
    

    And you could also remove the code that test for the empty textbox.

    The TryParse method wants two parameters, the first one is the string that could be converted, the second parameter is the variable that receives the result of the conversion if it is possible. If the conversion cannot be executed the function returns false.

    There are numerous reasons to prefer Double.TryParse instead of IsNumeric.

    The first reason is that with TryParse you also get the result of the conversion while with IsNumeric you would have to do the conversion after the check.

    The second reason is that you could give to IsNumeric whatever object you want (also a Button for example) and it accepts it. You would never discover this kind of errors at compile time. Instead, with TryParse, you could only pass a string as its first parameter.

    0 讨论(0)
  • 2021-01-18 19:39

    Use IsNumeric(txtQuantity.Text) if you have that method defined. Otherwise use Int32.TryParse() method. It will return true if the text passed in is a number.

    0 讨论(0)
  • 2021-01-18 19:42

    Use Regex.IsMatch:

    Public Function isNumeric(input As String) As Boolean
        Return Regex.IsMatch(input.Trim, "\A-{0,1}[0-9.]*\Z")
    End Function
    
    0 讨论(0)
  • 2021-01-18 19:51

    Yes, Double.Tryparse is the best answer to this question, but to save you time on coding and ensure that the value entered is always numeric, use the NumericDropdown control instead of the plain Text Box so that you are sure that inputted value is always numeric and save you time checking the inputted value since that control will not accept anything but numeric values only.

    0 讨论(0)
提交回复
热议问题