Following the answer provided by @Chetan_Ranpariya
and translating the code to VB.Net
(with a small tweak) it would be something like this:
Imports System.Collections.Generic
Public Class Form1
Private lstControls As List(Of Tuple(Of TextBox, Label))
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstControls = New List(Of Tuple(Of TextBox, Label))
lstControls.Add(Tuple.Create(TextBox1, Label1))
lstControls.Add(Tuple.Create(TextBox2, Label2))
lstControls.Add(Tuple.Create(TextBox3, Label3))
lstControls.Add(Tuple.Create(TextBox4, Label4))
lstControls.Add(Tuple.Create(TextBox5, Label5))
lstControls.Add(Tuple.Create(TextBox6, Label6))
lstControls.Add(Tuple.Create(TextBox7, Label7))
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each tuplePair As Tuple(Of TextBox, Label) In lstControls
If tuplePair.Item1.Text = tuplePair.Item2.Text Then
tuplePair.Item2.ForeColor = Color.Red
End If
Next
End Sub
End Class
Basically for this you need a List of Tuple
which is more efficient since every control has a pair. Inside the For Each
statement you can include an Else
clause to change the color to another one (I think this is to indicate the user the required fields on a form)
Give it a try and let me know your comments
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…