Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

vb.net - how i can search inside a lot of labels text and the label color would change to red

Quick question how can I search text inside 60 labels.text and the label color will change. Thanks in advance and I appreciate the help last time

dim Nc as color = color.RED
dim Oc as color = Color.Black

'' i need to search in multiple labels.text using txt1.text if possible
if txt1.text = label1.text then
    laebl1.forecolor = Nc
End IF

Is there an easier way ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...