Click here to Skip to main content
15,356,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I'm new to VB.NET. I got a basic app in mind which pretty much does comparison factors, for example, 4 or 5 textboxes in which we only type numbers. textbox1 wants the number range from 50 up to 100, If it's more than 100 or less than 50, it will send a msgbox "out of range".
The same thing applies to all other 3 textboxes they are in a certain range or bigger than or smaller than certain number, but all of them must be in a specific range if they are all good, then it will send a msgbox "correct" or whatsoever.

For example, my number must NOT be smaller than 1000 and NOT bigger than 1500, but any number in between is good to go.

I'm trying my best to explain my point, haha, but it's really hard.

At the same time, is it possible to make VB.NET read Excel column which has a wide range of different numbers and if that number is not in that column, then it won't work?

For example, let's say that I do have in Excel:
I do have textbox and a button so I add in the textbox let's say "50", then I click on the button, after that, it searches in the Excel column file for "50" since it's not there, it will pop-up msgbox "error" or anything like that.

Is it possible to apply it in this way using "And" because I want relations between all 4 textboxes?

If value1 >= 1400 And value2 >= 3 _
        And value3 >= 2 And etc..... Then
            Label17.Text = "VALID"
            Label17.ForeColor = Color.Green
            Label17.Text = "INVALID"
            Label17.ForeColor = Color.Red
        End If

What I have tried:

Private Sub Button1_Click(ByVal sender As System.Object, 
        ByVal e As System.EventArgs) Handles Button1.Click

        Dim value1 As Double
        Double.TryParse(TextBox4.Text, value1)
        Dim value2 As Double
        Double.TryParse(TextBox2.Text, value2)
        Dim value3 As Double
        Double.TryParse(TextBox3.Text, value3)
        Dim value4 As Double
        Double.TryParse(TextBox1.Text, value4)
        Dim count As Integer = (Form4.ListBox1.Items.Count - 1)
        Dim zip As String

            If value1 >= 800 Then
                Label18.Text = "VALID"
                Label18.ForeColor = Color.Green

                Label18.Text = "INVALID"
                Label18.ForeColor = Color.Red
            End If
            If value1 = 1150 Or value1 < 3500 Then
                Label19.Text = "VALID"
                Label19.ForeColor = Color.Green

                Label19.Text = "INVALID"
                Label19.ForeColor = Color.Red
            End If
            If value1 >= 900 Then
                Label20.Text = "VALID"
                Label20.ForeColor = Color.Green

                Label20.Text = "INVALID"
                Label20.ForeColor = Color.Red
            End If
            If value1 = 1000 Or value1 < 2500 Then
                Label21.Text = "VALID"
                Label21.ForeColor = Color.Green

                Label21.Text = "INVALID"
                Label21.ForeColor = Color.Red
            End If
            If value1 >= 900 Then
                Label23.Text = "VALID"
                Label23.ForeColor = Color.Green

                Label23.Text = "INVALID"
                Label23.ForeColor = Color.Red
            End If
            If value1 >= 1100 Then
                Label22.Text = "VALID"
                Label22.ForeColor = Color.Green

                Label22.Text = "INVALID"
                Label22.ForeColor = Color.Red
            End If
            If value1 >= 1000 Then
                Label24.Text = "VALID (Only FL)"
                Label24.ForeColor = Color.Green

                Label24.Text = "INVALID"
                Label24.ForeColor = Color.Red
            End If
            If value1 = 1000 Then
                Label25.Text = "VALID"
                Label25.ForeColor = Color.Green

                Label25.Text = "INVALID"
                Label25.ForeColor = Color.Red
            End If
            If value1 >= 1000 Then
                Label26.Text = "VALID"
                Label26.ForeColor = Color.Green

                Label26.Text = "INVALID"
                Label26.ForeColor = Color.Red
            End If
        If value1 >= 1400 And value2 >= 3 And value3 >= 2 Then
            Label17.Text = "VALID"
            Label17.ForeColor = Color.Green
            Label17.Text = "INVALID"
            Label17.ForeColor = Color.Red
        End If
    End Sub
Updated 23-Feb-22 21:39pm

Better than that, use a NumericUpDown control[^], and it will only allow numbers within a set range to be entered.

That way, you don't have to hassle with alpha characters in the user input, or out-of-range values.

The input is always available as a Decimal value as well, ready to use or cast to an integer if necessary.
Sam K. Lewis 23-Feb-22 15:38pm

Thank you so much for your reply, but the thing is they are different set of numbers like zip codes around 8K or maybe even more in different states

So pretty much checking if the zip codes are available for us or not, type zip code in the textbox and check in our list or excel if it's there or not if yes then it's a greenlight
I would prefer another way to go :
Customize your TextBox and add additional Properties (as max and min) and check the input during entering a Letter (by using the Event, fired by the TextBox). So YOUR TextBox will not accept other input than those which are allowed by you.

A logical control of the inputs of different TextBoxes must happen outside those TextBoxes but could allways happend if an input of one of those TextBoxes is finished.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900