i was wondering if there is any way to change all the forms background colors and label colors (inside one program) when somebody hits a command button?
Printable View
i was wondering if there is any way to change all the forms background colors and label colors (inside one program) when somebody hits a command button?
Dim ctl As Object
On Error Resume Next
For Each ctl In Controls
ctl.BackColor = vbRed
Next
iknow there is a controls collection does the same apply with forms as part of the program? if this is possible then the code is small but if not then depending on how many controls,forms and properties you want to manipulate it could take a lot of code, basically yes is the answer
Add 3 forms to a project, 2 labels on each form, and a command button on form1.
Good Luck :)Code:
Private Sub Form_Load()
' Display all forms
Load Form2
Form2.Show
Load Form3
Form3.Show
End Sub
Public Sub Command1_Click()
Dim f As Form, c As Control
' Get Form Object from Collection of Forms
For Each f In Forms
' Change Backcolor as form to a Random Color
f.BackColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255)
' Will loop through every kind of control currently on form
For Each c In f.Controls
' Determine is current control is a Label
If TypeOf c Is Label Then
'Change BackColor property to a Random Color
c.BackColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255)
End If
Next c
Next f
End Sub
Is this what you need?
You can put similar code for controls in the form's paint event, but it would be better suited for its own subroutine you could call after changing the color....Code:Dim Form as Form
For Each Form in Forms
Form.BackColor = vbRed
Next