Loop through all controls on tab control and change colours

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bigukfan
    New Member
    • Mar 2010
    • 21

    Loop through all controls on tab control and change colours

    I have a single form with a Tab Control containing 15 tabs. Various Text Boxes and Combo Boxes on each Tab. The Form is unbound. I want to use the KeyPress Event on the Tab Control to loop through all the controls on all tabs and change the background colour to white for all controls except the active control, where I want the background colour to be yellow. I can identify the Screen.ActiveCo ntrol but am not sure how to incorporate this into some code that loops through every control. Any bright ideas? Thanks for any help, as I'm new to this!
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Here's a good start, assuming your Tab Control is named Tab1
    Code:
    Dim ctl As Control
    Dim ctl2 As Control
    
    For Each ctl In Me.Controls
      If ctl.Parent.Name = "Tab1" Then
        For Each ctl2 In Me![Tab1].Pages(ctl.Name).Controls
          ctl2.BackColor = QBColor(4)
        Next
      End If
    Next

    Comment

    • bigukfan
      New Member
      • Mar 2010
      • 21

      #3
      Thanks for your help ADezii. Here is the code I have entered in the KeyPress Event of the tab control.
      Code:
      Private Sub TabCtl_KeyPress(KeyAscii As Integer)
      Dim Ctl As Control
      Dim Ctl2 As Control
      For Each Ctl In Me.Controls
        If Ctl.Parent.Name = "TabCtl" Then
          For Each Ctl2 In Me![TabCtl].Pages(Ctl.Name).Controls
            Ctl2.BackColor = QBColor(4)
          Next
        End If
      Next
      MsgBox "If this appears, code has run!"
      End Sub
      Strangely, the code does not run. I'm wondering if this is because it's in the Tab Control KeyPress event, and I am moving from control to control pressing keys, not pressing keys on the actual tab control?
      Last edited by NeoPa; Mar 20 '10, 04:21 PM. Reason: Please use the [CODE] tags provided

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        and I am moving from control to control pressing keys, not pressing keys on the actual tab control?
        That's the problem, the KeyPress Events for the individual Controls on the Tab recognize this Event and not the Tab Control itself. Set the Focus to the Control immediately before the Tab Control, then press the TAB Key. You will see that the KeyPress Event for the TAB Control is then fired. You can place a Call to a Sub-Routine in the KeyPress Event of each Control on the Tab Control if you like.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32645

          #5
          I would have thought something more like the following is required (unless I misunderstand the question) :
          Code:
          Dim ctl As Control
          
          For Each ctl In Me.Controls
            If ctl.Parent.Name = "Tab1" Then
              ctl.BackColor = QBColor(IIf(ctl.Name = Screen.ActiveControl.Name, 7, 6))
            End If
          Next
          Last edited by NeoPa; Mar 23 '10, 05:13 PM. Reason: Retrospective fix

          Comment

          • bigukfan
            New Member
            • Mar 2010
            • 21

            #6
            Looping through all controls on a page of Tab Control

            This seems to work, thanks to help from NeoPa. The variable iTabPage refers to the active tab, so I don't loop through every page, and I only want text boxes and combo boxes, so I look at the first three letters of the control name. Thanks for your help with this.

            Code:
            Private Sub sBackColour()
            Dim Ctl As Control
            For Each Ctl In Me![TabCtl].Pages([B]iTabPage[/B]).Controls
                If Left(Ctl.Name, 3) = "txt" Or Left(Ctl.Name, 3) = "cbo" Then
                    Ctl.BackColor = IIf(Ctl.Name = Screen.ActiveControl.Name, 8454143, 16777215)
                End If
            Next
            Exit Sub

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32645

              #7
              No worries.

              By the way, the 7 & 6 values were wrong in my post as you've clearly seen. They should have been parameters to QBColor(). If you want to use RGB() to set a value, that works quite well (and makes for easily readable code).

              PS. I will edit the earlier post (retrospectivel y of course) to put what it should have been.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32645

                #8
                PPS. You can use the .ControlType property of a control to determine exactly what type of control it is. This would be more reliable and portable in code and not depend on good discipline when naming controls (Good discipline is still recommended of course).

                Comment

                Working...