我如何在大量标签文本中搜索并且标签颜色会变为红色 [英] how i can search inside a lot of labels text and the label color would change to red

查看:24
本文介绍了我如何在大量标签文本中搜索并且标签颜色会变为红色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速问题如何在 60 个 labels.text 中搜索文本并且标签颜色会改变.提前致谢,感谢上次的帮助

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

有更简单的方法吗?

推荐答案

按照 @Chetan_Ranpariya 提供的答案,将代码翻译成 VB.Net(用一个小的调整)它会是这样的:

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

基本上为此,您需要一个 元组列表,因为每个控件都有一对,因此效率更高.在 For Each 语句中,您可以包含一个 Else 子句以将颜色更改为另一种颜色(我认为这是为了向用户指示表单上的必填字段)
试一试,让我知道你的意见

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

这篇关于我如何在大量标签文本中搜索并且标签颜色会变为红色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆