Question Compare value of 6 different textbox's to 6 numbers

inkedgfx

Well-known member
Joined
Sep 29, 2012
Messages
139
Location
USA
Programming Experience
Beginner
I am having some diffuculties ....

I need to compare the value of textboxes to the value of the winning numbers....I have the winning numbers inside global variables.....the value of the "yournumber" textbox's will be added by the user

so I need to compare txtyournumber1.text and txtyournumber2.text and txtyournumber3.text and txtyournumber4.text and txtyournumber5.text and txtyournumber6.text to
winningnumberone
winningnumbertwo
winningnumberthree
winningnumberfour
winningnumberfive
winningnumbersix

I need to compare each winningnumber to each textbox.text in case the numbers the user enters arent in order...

I have tried string.equals
string.compare and .contains

not sure how to acomplish this...

as always any help is appreciated.

InkedGFX
 
There are multiple ways to do this. Here is one possibility, assuming the input and winning numbers are integers (change to double or whatever you need).

Method 1 - Function CheckForWinningNumber(yourNumber as Integer) As Boolean
Inside this method you can write a loop over the six winning numbers, comparing the passed in user number against each of the winning numbers. If a match is found return true, otherwise false.

Method 2 - Sub CheckUserNumbers()
This method would consider each of the values input by the user into the textboxes, passing them into the method defined above and using the result to do whatever you need for your particular scenario. Assuming you are working with integers, you would use CInt(TextBox.Text) to convert the user input value to an integer before passing it to the method above. You could always move this conversion inside the above method if you are ok with changing the parameter in the above method to a string.

Good luck!
 
 Public Function CheckWinningNumbers(ByVal winNumber As Integer) As Boolean
        Dim winningnumbers As New ArrayList
        Dim found As Boolean
        winningnumbers.Add(WinningNumberOne)
        winningnumbers.Add(WinningNumberTwo)
        winningnumbers.Add(WinningNumberThree)
        winningnumbers.Add(WinningNumberFour)
        winningnumbers.Add(WinningNumberFive)
        winningnumbers.Add(WinningNumberSix)
        For Each num In winningnumbers
            If num = winNumber Then
                found = True
            Else : found = False
            End If
        Next
        Return found
    End Function


thank you for the help.... I have written this function which does check the numbers and if there is a match then returns true if not returns false.

now how would i use this to check all the texboxes where the user enters there numbers...

I have tried

 If CheckWinningNumbers(numbers(0)) = True Then
            txtYourNumber1.BackColor = Color.Green
        Else
            txtYourNumber1.BackColor = Color.Red
        End If
        If CheckWinningNumbers(numbers(1)) = True Then
            txtYourNumber2.BackColor = Color.Green
        Else
            txtYourNumber2.BackColor = Color.Red
        End If
        If CheckWinningNumbers(numbers(2)) = True Then
            txtYourNumber3.BackColor = Color.Green
        Else
            txtYourNumber3.BackColor = Color.Red
        End If
        If CheckWinningNumbers(numbers(3)) = True Then
            txtYourNumber4.BackColor = Color.Green
        Else
            txtYourNumber4.BackColor = Color.Red
        End If
        If CheckWinningNumbers(numbers(4)) = True Then
            txtYourNumber5.BackColor = Color.Green
        Else
            txtYourNumber5.BackColor = Color.Red
        End If
        If CheckWinningNumbers(numbers(5)) = True Then
            txtYourNumber6.BackColor = Color.Green
        Else
            txtYourNumber6.BackColor = Color.Red
        End If


this turns each of the back color to red

can I do

if checkwinningnumbers(numbers(0)) or check winningnumbers(2)) or checkwinningnumber(numbers(3)) or ... ect... then
dowork here'
end if


thanks for the help

InkedGFX
 
Hi,

It sounds like you have forgotten to save the entered numbers from your TextBox controls in your Numbers array? i.e. you should have somewhere:-

numbers(0)= Integer.Parse(txtTextBoxNumber1.Text)
numbers(1)= Integer.Parse(txtTextBoxNumber2.Text)
numbers(2)= Integer.Parse(txtTextBoxNumber3.Text)
numbers(3)= Integer.Parse(txtTextBoxNumber4.Text)
numbers(4)= Integer.Parse(txtTextBoxNumber5.Text)
numbers(5)= Integer.Parse(txtTextBoxNumber6.Text)

If you add this then the code you have supplied will begin to work. You could also just change your supplied code to check the TextBoxes directly and do away with your numbers array.

Hope that helps.

Cheers,

Ian
 
There is a logical error in your CheckWinningNumbers method, you continue the loop even if a match is found and thereby set found=False.
Anyway, here's how I would do it:
        Dim wins = {winningnumberone, winningnumbertwo, winningnumberthree, winningnumberfour, winningnumberfive, winningnumbersix}
        Dim boxes = {txtyournumber1, txtyournumber2, txtyournumber3, txtyournumber4, txtyournumber5, txtyournumber6}

        Array.ForEach(boxes, Sub(box) box.BackColor = If(wins.Contains(CInt(box.Text)), Color.Green, Color.Red))

Note that this requires the TextBox text to represent a number that can be converted to Integer with CInt. It would be more appropriate to use a NumericUpDown control.
 
Many thanks JohnH,

I missed that point in my initial review.

Cheers,

Ian

N.B. I take it your solution is another example of a Lambder expression? Never done it that way before.
 
Last edited:
IanRyder said:
I take it your solution is another example of a Lambder expression?
Yes, the inline Sub used as argument for the action parameter of ForEach method is a lambda, it evaluates as a Action(Of T) delegate.
 
it's ok....I learned to exit a loop if the condition is meet...so thank you for the help....

this fixed the code

Public Function CheckWinningNumbers(ByVal winNumber As Integer) As Boolean
        Dim winningnumbers As New ArrayList
        Dim found As Boolean
        winningnumbers.Add(WinningNumberOne)
        winningnumbers.Add(WinningNumberTwo)
        winningnumbers.Add(WinningNumberThree)
        winningnumbers.Add(WinningNumberFour)
        winningnumbers.Add(WinningNumberFive)
        winningnumbers.Add(WinningNumberSix)
        For Each num In winningnumbers
            If num = winNumber Then
                found = True
                If found = True Then
                    Exit For
                End If
            Else : found = False
            End If
        Next
        Return found
    End Function


after I added the "if found = true then exit for" the code works properly.....

also I had the winningnumberone, winningnumbertwo,winningnumberthree ..ect.. declared as integer in a global variable.

thank you again...

InkedGFX
 
Back
Top