-
Mar 27th, 2015, 12:35 PM
#1
Thread Starter
New Member
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...........
-
Mar 27th, 2015, 12:42 PM
#2
Re: Passing Controls/Forms Objects to a Function
Call SetTheme(ACtl), not SetTheme(ACtl)
or... SetTheme ACtl
How now?
-
Mar 27th, 2015, 12:48 PM
#3
Thread Starter
New Member
Re: Passing Controls/Forms Objects to a Function
LaVolpe,
You got it in one..... thanks so much !
-
Mar 27th, 2015, 12:56 PM
#4
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
-
Mar 27th, 2015, 01:01 PM
#5
Thread Starter
New Member
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
-
Mar 27th, 2015, 01:20 PM
#6
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
-
Mar 27th, 2015, 01:24 PM
#7
Thread Starter
New Member
Re: Passing Controls/Forms Objects to a Function
Great thanks, makes perfect sense, I'm trying it right now.....
Thanks again for your help
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|