none
How to make one Textbox be equal to multiple other textboxes and show a message RRS feed

  • Question

  • If TextBox10.Text = TextBox11.Text And TextBox12.Text And TextBox13.Text And TextBox14.Text And TextBox15.Text And TextBox16.Text And TextBox17.Text Then
                Label4.Text = "Good Work"
            Else
                Label4.Text = "Try Again"
            End If

    This code does not work for me because even is Textbox10.Text does not equal Textbox12.text but is equal to Textbox11.Text then it will still display "Good job" instead of "Try again" but I need it so that Textbox10.Text equals all of the other Textboxes in order to show good work otherwise show try again.



    • Edited by shauan Saturday, January 13, 2018 5:15 PM
    Saturday, January 13, 2018 5:12 PM

All replies

  • Try using equals for all.

    Event then there may be problem cases with numbers and invalids.

    ie TextBox10.Text = TextBox12.Text

    Ps if you make it up with individual statements for each, you can debug single step through them as see what one is not working.

    ie if TextBox10.Text = TextBox11.Text then

            if TextBox10.Text = TextBox12.Text

    now you can look at each line to see what is going on. Once it works how you want combine them one at a time and check each one.

    I think when you don't use the equals it evaluates to Boolean true or false or something?

    Saturday, January 13, 2018 5:19 PM
  • This code does not work for me because even is Textbox10.Text does not equal Textbox12.text but is equal to Textbox11.Text then it will still display "Good job" instead of "Try again" but I need it so that Textbox10.Text equals all of the other Textboxes in order to show good work otherwise show try again.

    There are two different usages for And.  You can avoid the confusion by always sticking with AndAlso when you are stringing separate tests together.
    If TextBox10.Text = TextBox11.Text AndAlso TextBox10.Text = TextBox12.Text AndAlso TextBox10.Text = TextBox13.Text AndAlso TextBox10.Text = TextBox14.Text AndAlso TextBox10.Text = TextBox15.Text AndAlso TextBox10.Text = TextBox16.Text AndAlso TextBox10.Text = TextBox17.Text Then ...

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/andalso-operator
    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/and-operator

    Saturday, January 13, 2018 8:06 PM
  • In this code sample I declare a List(Of TextBox) as private then populate it with the Text boxes you want to evaluate in form load.

    In button1 click event check all text boxes to see if they match TextBox10 as lower case.

    Option Strict On
    Public Class Form1
        Private TextBoxList As List(Of TextBox)
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Label4.Text = If(TextBoxList.
                Select(Function(tb) tb.Text.ToLowerInvariant).
                All(Function(item) item = TextBox10.Text.ToLowerInvariant),
                "Good work",
                "Try again")
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBoxList = New List(Of TextBox) From
                {
                    TextBox11,
                    TextBox12,
                    TextBox13,
                    TextBox14,
                    TextBox15,
                    TextBox16,
                    TextBox17
                }
        End Sub
    End Class
    

    This version is case sensitive

    Option Strict On
    Public Class Form1
        Private TextBoxList As List(Of TextBox)
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Label4.Text = If(TextBoxList.
                Select(Function(tb) tb.Text).
                All(Function(item) item = TextBox10.Text),
                "Good work",
                "Try again")
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBoxList = New List(Of TextBox) From
                {
                    TextBox11,
                    TextBox12,
                    TextBox13,
                    TextBox14,
                    TextBox15,
                    TextBox16,
                    TextBox17
                }
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, January 13, 2018 8:51 PM
    Moderator
  • Hi shauan,

    By your code, you just compare TextBox10.Text and TextBox11.Text. If you want to If...Else to do this, you can refer to Acamar's post. You can also take a look the following code.

     Dim mytextBoxs As New List(Of TextBox)
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            mytextBoxs.Add(TextBox1)
            mytextBoxs.Add(TextBox2)
            mytextBoxs.Add(TextBox3)
            mytextBoxs.Add(TextBox4)
            mytextBoxs.Add(TextBox5)
    
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim distinctboxs = mytextBoxs.GroupBy(Function(t) t.Text)
            If distinctboxs.Count = 1 Then
                MessageBox.Show("Good Work!")
            Else
                MessageBox.Show("Try again")
            End If
        End Sub

    I use GroupBy to count the different value numbers, if the number is 1, I can consider there are the same value in these TextBox.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Cor Ligthert Tuesday, January 16, 2018 8:12 AM
    Tuesday, January 16, 2018 6:01 AM
    Moderator