By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,944 Members | 1,633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,944 IT Pros & Developers. It's quick & easy.

Loop through all controls on tab control and change colours

P: 21
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.ActiveControl 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!
Mar 19 '10 #1

✓ answered by NeoPa

I would have thought something more like the following is required (unless I misunderstand the question) :
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2.  
  3. For Each ctl In Me.Controls
  4.   If ctl.Parent.Name = "Tab1" Then
  5.     ctl.BackColor = QBColor(IIf(ctl.Name = Screen.ActiveControl.Name, 7, 6))
  6.   End If
  7. Next

Share this Question
Share on Google+
7 Replies


ADezii
Expert 5K+
P: 8,705
Here's a good start, assuming your Tab Control is named Tab1
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2. Dim ctl2 As Control
  3.  
  4. For Each ctl In Me.Controls
  5.   If ctl.Parent.Name = "Tab1" Then
  6.     For Each ctl2 In Me![Tab1].Pages(ctl.Name).Controls
  7.       ctl2.BackColor = QBColor(4)
  8.     Next
  9.   End If
  10. Next
Mar 19 '10 #2

P: 21
Thanks for your help ADezii. Here is the code I have entered in the KeyPress Event of the tab control.
Expand|Select|Wrap|Line Numbers
  1. Private Sub TabCtl_KeyPress(KeyAscii As Integer)
  2. Dim Ctl As Control
  3. Dim Ctl2 As Control
  4. For Each Ctl In Me.Controls
  5.   If Ctl.Parent.Name = "TabCtl" Then
  6.     For Each Ctl2 In Me![TabCtl].Pages(Ctl.Name).Controls
  7.       Ctl2.BackColor = QBColor(4)
  8.     Next
  9.   End If
  10. Next
  11. MsgBox "If this appears, code has run!"
  12. 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?
Mar 19 '10 #3

ADezii
Expert 5K+
P: 8,705
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.
Mar 20 '10 #4

NeoPa
Expert Mod 15k+
P: 31,769
I would have thought something more like the following is required (unless I misunderstand the question) :
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2.  
  3. For Each ctl In Me.Controls
  4.   If ctl.Parent.Name = "Tab1" Then
  5.     ctl.BackColor = QBColor(IIf(ctl.Name = Screen.ActiveControl.Name, 7, 6))
  6.   End If
  7. Next
Mar 20 '10 #5

P: 21
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub sBackColour()
  2. Dim Ctl As Control
  3. For Each Ctl In Me![TabCtl].Pages(iTabPage).Controls
  4.     If Left(Ctl.Name, 3) = "txt" Or Left(Ctl.Name, 3) = "cbo" Then
  5.         Ctl.BackColor = IIf(Ctl.Name = Screen.ActiveControl.Name, 8454143, 16777215)
  6.     End If
  7. Next
  8. Exit Sub
Mar 23 '10 #6

NeoPa
Expert Mod 15k+
P: 31,769
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 (retrospectively of course) to put what it should have been.
Mar 23 '10 #7

NeoPa
Expert Mod 15k+
P: 31,769
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).
Mar 23 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.