Mara
Mara

Reputation: 365

Method for comparing text boxes in vb.net

I need help, so I can compare the text from the text booths that are in these 3 groupboxes.

This is what my form looks like:

![enter image description here

Private Sub frmCompareAdress_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim firstGroupBoxFields() As String = {TextBoxVorname1.Text, TextBoxName1.Text, TextBoxStrasse1.Text, TextBoxPLZ1.Text, TextBoxOrt1.Text, TextBoxTelefon1.Text}
    Dim secondGroupBoxFields() As String = {TextBoxVorname2.Text, TextBoxName2.Text, TextBoxStrasse2.Text, TextBoxPLZ2.Text, TextBoxOrt2.Text, TextBoxTelefon2.Text}
    Dim thirdGroupBoxFields() As String = {TextBoxVorname3.Text, TextBoxName3.Text, TextBoxStrasse3.Text,TextBoxNr3.Text, TextBoxPLZ3.Text, TextBoxOrt3.Text, TextBoxTelefon3.Text}

    ComparisonFieldsfirstGroupBoxFields, secondGroupBoxFields, thirdGroupBoxFields)
End Sub

Public Sub ComparisonFields(ByVal firstGBFields() As String, secondGBFields() As String, thirdGBFields() As String)
    Dim notCompareFileds = String.Join(", ", firstGBFields.Except(secondGBFields))

    'what to do next? I'm a little confused if I'm on the right track
End Sub

Now if, for example, the textBox1Vorname.Text is different from the textBox2Vorname.Text or textBox3Vorname.Text, I want to mark them in red. I imagined it in a way, to compare as an array, and I put it in 3 array values from textboxes.

Can anyone help me further with this function?

Thank you for your help.

enter image description here

Upvotes: 1

Views: 569

Answers (1)

GSerg
GSerg

Reputation: 78175

Public Shared Function HaveSameText(ParamArray controls() As Control) As Boolean
    Return controls.All(Function(c) c.Text = controls(0).Text)
End Function

Public Shared Sub ColorAllRed(ParamArray controls() As Control)
    For Each c In controls
        c.ForeColor = Color.Red
    Next
End Sub

Public Shared Sub ColorAllRedIfNotSameText(ParamArray controls() As Control)
    If Not HaveSameText(controls) Then ColorAllRed(controls)
End Sub
Private Sub frmCompareAdress_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ColorAllRedIfNotSameText(TextBoxVorname1, TextBoxVorname2, TextBoxVorname3)
    ColorAllRedIfNotSameText(TextBoxName1, TextBoxName2, TextBoxName3)
    ColorAllRedIfNotSameText(TextBoxStrasse1, TextBoxStrasse2, TextBoxStrasse3)
    ColorAllRedIfNotSameText(TextBoxPLZ1, TextBoxPLZ2, TextBoxPLZ3)
    ColorAllRedIfNotSameText(TextBoxOrt1, TextBoxOrt2, TextBoxOrt3)
    ColorAllRedIfNotSameText(TextBoxTelefon1, TextBoxTelefon2, TextBoxTelefon3)
End Sub

Upvotes: 3

Related Questions