How can I search texts inside a lot of specific labels and the color of the searched texts would appear in red

Wilfred Eghenedji 326 Reputation points
2021-07-26T12:05:24.717+00:00

I have the following code (credit to Chetan Ranpariya) but is not working. It gives the following error:

Additional information: Object reference not set to an instance of an object.

Inherits Form  
Private labels As List(Of Label)  
    Public Sub New2()  
        InitializeComponent()  
        labels = New List(Of Label)()  

        labels.Add(Label29)  
        labels.Add(Label30)  
        labels.Add(Label31)  
        labels.Add(Label34)  
        labels.Add(Label35)  
        labels.Add(Label36)  
        labels.Add(Label37)  

        labels.Add(Label38)  
        labels.Add(Label39)  
        labels.Add(Label44)  
        labels.Add(Label45)  
        labels.Add(Label47)  
        labels.Add(Label51)  
        labels.Add(Label65)  

        labels.Add(Label66)  
        labels.Add(Label67)  
        labels.Add(Label68)  
        labels.Add(Label69)  
        labels.Add(Label70)  
        labels.Add(Label83)  
        labels.Add(Label85)  

        labels.Add(Label86)  
        labels.Add(Label87)  
        labels.Add(Label88)  
        labels.Add(Label92)  
        labels.Add(Label93)  
        labels.Add(Label94)  

    End Sub  

    Private Sub Button30_Click(sender As Object, e As EventArgs) Handles Button30.Click  
        For Each var As Label In labels  
            If TextBox1.Text = var.Text Then  
                var.ForeColor = Color.Red  
            End If  
        Next  
    End Sub  

Somebody please help. Thank you.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,891 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,727 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,421 Reputation points
    2021-07-26T19:00:40.137+00:00

    Here is another option and is case insensitive

    117995-figure1.png

    Public Class Form1  
        Private ReadOnly LabelList As New List(Of Label)  
      
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown  
      
            LabelList.AddRange(New Label() _  
                {  
                    Label1,  
                    Label2,  
                    Label3,  
                    Label4,  
                    Label5,  
                    Label6  
                })  
      
        End Sub  
      
        Private Sub WorkButton_Click(sender As Object, e As EventArgs) Handles WorkButton.Click  
      
            If LabelList.Any(Function(currentLabel) ItemTextBox.Text.Equals(currentLabel.Text, StringComparison.CurrentCultureIgnoreCase)) Then  
                LabelList.ForEach(  
                    Sub(label)  
                        If label.Text.Equals(ItemTextBox.Text, StringComparison.CurrentCultureIgnoreCase) Then  
                            label.ForeColor = Color.Red  
                        End If  
                    End Sub)  
            End If  
        End Sub  
    End Class  
      
    

    Update

    Using case insensitive compare

    Public Module StringExtensions  
        <Runtime.CompilerServices.Extension>  
        Public Function ContainsInsensitive(source As String, toCheck As String) As Boolean  
            Return source.IndexOf(toCheck, StringComparison.OrdinalIgnoreCase) >= 0  
        End Function  
    End Module  
    

    Form code

    Public Class Form1  
        Private ReadOnly LabelList As New List(Of Label)  
      
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown  
      
            LabelList.AddRange(New Label() _  
                {  
                    Label1,  
                    Label2,  
                    Label3,  
                    Label4,  
                    Label5,  
                    Label6  
                })  
      
        End Sub  
      
        Private Sub WorkButton_Click(sender As Object, e As EventArgs) Handles WorkButton.Click  
      
            If LabelList.Any(Function(currentLabel) currentLabel.Text.ContainsInsensitive(ItemTextBox.Text)) Then  
                LabelList.ForEach(  
                    Sub(label)  
                        If label.Text.ContainsInsensitive(ItemTextBox.Text) Then  
                            label.ForeColor = Color.Red  
                        End If  
                    End Sub)  
            End If  
      
        End Sub  
    End Class  
    

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,421 Reputation points
    2021-07-29T18:31:01.083+00:00

    Okay, as mentioned before you should make an attempt at changing the code.

    For instance, you are right, when there are multiple words, nothing, zilch but if we think about it the change is clear, we need to split the words from the search string. This I've done and works as shown below but there are other requirements such as case sensitivity which may need modifications.

    New code sample is here. I don't have time to fully test so I suggest you take time and make this a learning exercise :-)

    https://github.com/karenpayneoregon/windows-forms-csharp/tree/master/Labels

    119172-figure1.png

    1 person found this answer helpful.

  2. Viorel 117.3K Reputation points
    2021-07-26T12:11:49.337+00:00

    Try renaming New2 to New.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.