Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 40 textboxes inside panels, spread over 4 tabpages in a tabcontrol, I need to be able to click my KennelStatus button and if the textbox has been changed to orange by the user, then it changes to CadetBlue.

I have tried the attached code, which I thought should work, but nothing happens. Is it because the textboxes are in panels maybe??

I know that I can do each textbox seperately, and it works, but there has to be an easier way than writing it out separately for 40 textboxes.
Any help would be much appreciated

Diane

What I have tried:

VB
Private Sub KennelStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KennelStatus.Click

Dim a As Control

        For Each a In Me.TabControl1.Controls

            If TypeOf a Is TextBox Then
                If a.BackColor = Color.Orange Then
                    a.BackColor = SystemColors.CadetBlue
                End If

            End If


        Next

End Sub

I know the following works for each textbox separately:
VB
Private Sub KennelStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KennelStatus.Click

If Kennel1StatusTbox.BackColor = Color.Orange Then
            Kennel1StatusTbox.BackColor = SystemColors.CadetBlue
        End If
End sub
Posted
Updated 16-Apr-18 22:07pm
v2

1 solution

If your textboxes are inside any other control - and a Panel counts - then the "outer control" acts as a Container, and has it's own separate Controls collection that you need to search as well.
The simplest solution is just to recurse:
Private Sub ScanContainer(ByVal controls As Control.ControlCollection)
    For Each c As Control In controls
        If TypeOf c Is TextBox Then
            ...
        End If

        If c.Controls IsNot Nothing AndAlso c.Controls.Count > 0 Then
            ScanContainer(c.Controls)
        End If
    Next
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900