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!
Loop through all controls on tab control and change colours
Collapse
X
-
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 -
Thanks for your help ADezii. Here is the code I have entered in the KeyPress Event of the tab control.
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?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 SubComment
-
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.and I am moving from control to control pressing keys, not pressing keys on the actual tab control?Comment
-
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 NextComment
-
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 SubComment
-
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
-
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
Comment