如何验证在Visual Basic文本框号范围数组? [英] How to validate number range array in textbox in visual basic?

查看:113
本文介绍了如何验证在Visual Basic文本框号范围数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,在基本形成,你有3个文本框和他们每个人,你可以输入一个号码,那么你有一个按钮,检查是否这些文本框在一个特定范围内的数字。这非常像一把锁的组合,但我需要帮助检查值,例如;

So basically in a form, you have 3 textboxes and each of them you can input a number, then you have a button that check if each of these textboxes are in a range of numbers specified. It's very much like a lock combo, but I need help checking values, for example;

我可以计算出的唯一的事情就是

the only thing i can figure out is

Dim intOne As Integer
    Dim intTwo As Integer
    Dim intThree As Integer
    Dim blnInputOk As Boolean = True

    If Integer.TryParse(lblOne.Text, intOne) = False Then
        MessageBox.Show("Value must be an integer")
        blnInputOk = False
    End If

    If Integer.TryParse(lblTwo.Text, intTwo) = False Then
        MessageBox.Show("Value must be an integer")
        blnInputOk = False
    End If

    If Integer.TryParse(lblThree.Text, intThree) = False Then
        MessageBox.Show("Value must be an integer")
        blnInputOk = False
    End If

    If intOne >= 6 And intOne <= 8 Then
        If intTwo >= 2 And intOne <= 9 Then
            If intThree >= 0 And intThree <= 8 Then
                MessageBox.Show("Good code!")
            Else
                MessageBox.Show("Wrong, number must be between range 0 to 8")
            End If
        Else
            MessageBox.Show("Wrong, number must be between range 2 to 9")
        End If
    Else
        MessageBox.Show("Wrong, number must be between range 6 to 8")
    End If

所以我的问题是你怎么能增加了对数字范围的数组,每个文本框使这个code更简单呢?我也知道有添加循环的可能性,但我不知道如何构建它,任何人都可以帮忙吗?谢谢

So my question is how can you make this code simpler by adding an array for number range for each textbox? I also know there is a possibility of adding a loop, but i'm not sure how to structure it, can anyone help? thanks

推荐答案

有很多方法,一切都取决于你要比较值的数量,最简单的是通过增加一个比较函数。

There are many ways, all depends on the number of values you are going to compare, the simplest is by adding a comparison function.

Private Function IsInRange(x As Integer, a As Integer, b As Integer) As Boolean
    Dim r As Boolean
    r = (x >= a And x <= b)
    If r Then
        MessageBox.Show("Good code!")
    Else
        MessageBox.Show(String.Format("Wrong, number {0} must be between range {1} to {2}", x, a, b))
    End If
    Return r
End Function

然后根据你的问题显示的code,你可以这样做:

Then according to the code shown in your question, you can do this:

If IsInRange(intOne, 6, 8) Then
    If IsInRange(intTwo, 2, 9) Then
        IsInRange(intThree, 0, 8)
    End If
End If

这篇关于如何验证在Visual Basic文本框号范围数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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