Passing Controls/Forms Objects to a Function
Hello All,
I've been head scratching, googling and experimenting to try to find the right way of doing this but to no avail.
Here's what I want to do:
Apply a common 'theme' to each of the controls on each of my forms by looping through all the controls on each form when it loads (or should it be initialise?) and applying common colours\styles etc, according to the type of control. To save code I was hoping to have this theme code in a module so it could be called from any one of the forms
So here's what I thought would work:
In each of the Form Load subs:
Code:
Dim ACtl As Control
For Each ACtl In Me.Controls
SetTheme(ACtl)
Next
and in a module:
Code:
Public Sub SetTheme(ACtl As Control)
If (TypeOf ACtl Is DTPicker) Then
ACtrl.BackColor = RGB(255, 255, 255) 'white
ACtrl.ForeColor = RGB(255, 128, 0) 'orange
ElseIf TypeOf ACtl Is TextBox Then
ACtl.ForeColor = vbRed
'etc
'etc
End If
End Sub
but it doesnt work......
Any ideas ?
Thanks, I've got a lot learn on this subject...........
Re: Passing Controls/Forms Objects to a Function
Call SetTheme(ACtl), not SetTheme(ACtl)
or... SetTheme ACtl
How now?
Re: Passing Controls/Forms Objects to a Function
LaVolpe,
You got it in one..... thanks so much !
Re: Passing Controls/Forms Objects to a Function
When calling subroutines, don't enclose your parameters in parentheses or if you do, precede the line with the keyword Call. Using parentheses in that specific case forces VB to evaluate what's enclosed as an expression and then VB passes the the value evaluated to, not the control, itself, as you obviously wanted to do.
When calling functions, same rule applies unless you are assigning the function's return value to a variable
Re: Passing Controls/Forms Objects to a Function
Ah OK, Got it.
So would it be more efficient (or possible) to pass the form and do the loop in the module? Something like:
Call SetTheme (Me)
and then...
Public Sub SetTheme(AForm As Form)
Dim ACtl as Control
For Each ACtl in AForm
If (TypeOf ACtl Is DTPicker) Then
ACtrl.BackColor = RGB(255, 255, 255) 'white
ACtrl.ForeColor = RGB(255, 128, 0) 'orange
ElseIf TypeOf ACtl Is TextBox Then
ACtl.ForeColor = vbRed
'etc
'etc
End If
Next
End Sub
Re: Passing Controls/Forms Objects to a Function
More efficient? Yes, simply because you are calling an outside method only once vs. calling it once for each control
Re: Passing Controls/Forms Objects to a Function
Great thanks, makes perfect sense, I'm trying it right now.....
Thanks again for your help