检查在Visual Basic的文本框中输入的数值 [英] Checking for numeric value entered in text box in Visual Basic

查看:112
本文介绍了检查在Visual Basic的文本框中输入的数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Visual Basic类编写程序,并且有一个快速问题.我们被鼓励做的一件事是检查以确保在文本框中输入的数量实际上是一个数字.我们的教授建议使用IsNumeric来执行此检查,但是我遇到了一些麻烦.在他将其添加到说明中之前,我已经编写了很多代码,因此不确定如何将其集成到我已有的代码中.

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 text box is actually a number. Our professor suggested using IsNumeric to perform this check, but I'm running into some trouble. I already had a good bit of the code written before he added this to the instructions, so not sure how to integrate this into the code I already have.

该程序的主要目的是允许用户从一个列表框向配方列表框添加成分,在文本框中输入每种所选成分的数量,并计算配方的总卡路里.我现在编写代码的方式是,IsNumeric是嵌套if语句的一部分,在该语句的开头,我将开始将所选成分添加到配方列表框中.我不确定那是否是正确的地方.

The main purpose of the program is to allow the user to add ingredients from one list box to the recipe list box, input a quantity for each selected ingredient in a text box, and calculate the total calories for the recipe. The way I have the code written now, IsNumeric is part of a nested if statement at the beginning of where I will start adding the selected ingredients to the recipe list box. I'm not sure if that's the correct place for it though.

这是我到目前为止编写的代码.

Here is the code 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 receive when I attempt to run this program as it is written.

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

任何建议将不胜感激.

Any suggestions would be greatly appreciated.

推荐答案

一种更正确的方法是使用Int32Double类中可用的TryParse方法

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.

TryParse方法需要两个参数,第一个是可以转换的字符串,第二个参数是在可能的情况下接收转换结果的变量.如果无法执行转换,则该函数返回false.

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.

有很多理由偏爱第一个原因是,使用TryParse您还可以获得转换结果,而使用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.

第二个原因是您可以给IsNumeric任何想要的对象(例如,还有一个Button)并接受它.您永远不会在编译时发现这种错误.相反,使用TryParse,您只能将字符串作为第一个参数传递.

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.

这篇关于检查在Visual Basic的文本框中输入的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆