Validating a numeric value entered in a text field in Visual Basic

I am working on a program for my Visual Basic class and ask a quick question. One of the things we recommended doing is to check that the quantity entered in the text box is actually a number. Our professor suggested using IsNumeric to perform this check, but I have problems. I already had good code written before he added it in the instructions, so I’m not sure how to integrate it into the code that I already have.

The main goal of the program is to provide the user with the opportunity to add ingredients from one list to the recipe list box, enter the amount for each selected ingredient in the text box and calculate the total calories for the recipe. As I wrote the code, IsNumeric is part of the nested if statement at the beginning, where I will start adding the selected ingredients to the recipe list. I'm not sure this is the right place, though.

Here is the code that I have written so far.

Public Class Form1

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim i As Integer = lstIngredients.SelectedIndex
        Dim Quantity As Double
        Dim intCount As Integer = 0

        If Trim(txtQuantity.Text = "") Then
            Quantity = 1
        Else
            Quantity = Me.txtQuantity.Text
        End If

        If txtQuantity.Text Is IsNumeric() 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


    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        lstRecipe.Items.Clear()
        txtQuantity.Clear()
        txtAnswer.Clear()
    End Sub

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click


    End Sub
End Class

Also, here is the error I get when I try to run this program as I write it.

Error   1   Argument not specified for parameter 'Expression' of 'Public   Function IsNumeric(Expression As Object) As Boolean'.    

Any suggestions would be greatly appreciated.

+3
source share
4 answers

A better way to do this is to use a method TryParseavailable in the class Int32orDouble

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

, .

TryParse , - , , - , , . , false.

Double.TryParse IsNumeric.

, TryParse , IsNumeric .

, IsNumeric , (, , Button), . . TryParse .

+12

- .

If IsNumeric(txtQuantity.Text) Then
+5

IsNumeric(txtQuantity.Text), . Int32.TryParse(). true, .

+1

Regex.IsMatch:

Public Function isNumeric(input As String) As Boolean
    Return Regex.IsMatch(input.Trim, "\A-{0,1}[0-9.]*\Z")
End Function
+1

All Articles