-
May 4th, 2009, 11:21 AM
#1
passing control array name to perform a functions
Hello:
I have several arrays of option (radio) buttons say optBank1 with 10 buttons and optBank2 with 20 buttons and so on. I want to be able to set all the background color the same using a common rouuine...seems easy, but no luck so far...trying to do something like:
Code:
call SetBcolor(optBank1, VbGreen) 'or something similar
call SetBcolor(optBank2, VbRed)
call SetBcolor(optFish, VbGreen)
'etc
The following does not work, tried several variations:
Code:
Private Sub SetBcolor(Ctrl As Controls, Color As Long)
Dim jj As Control
For Each jj In Ctrl
jj.BackColor = Color
Next jj
End Sub
Last edited by WizBang; May 7th, 2009 at 08:17 AM.
Reason: Added [code] tags
-
May 4th, 2009, 11:44 AM
#2
Re: passing control array name to perform a functions
You nee to pass the Control Array as a variant. I've also changed the long to OLE_COLOR, just for better reading, but Long still works as well. Here is your modified code, and it works :
Code:
Option Explicit
Private Sub Command1_Click()
call SetBcolor(optBank1, VbGreen) 'Set backcolor of first array to green
call SetBcolor(optBank2, VbRed)
End Sub
Private Sub SetBcolor(CtrlArr As Variant, Color As OLE_COLOR)
Dim jj As OptionButton 'declare optbut object
For Each jj In CtrlArr 'loop through array
jj.BackColor = Color 'set color
Next jj
End Sub
-
May 4th, 2009, 11:47 AM
#3
Re: passing control array name to perform a functions
You have to pass each element of an indexed control.
So you best write a function which detects if a control is an array and proceed like that:
Code:
Private Function IsCtlArray(Cntrl As Control) As Boolean
Dim i%
On Error GoTo NoArray
i = Cntrl.Index
IsCtlArray = True
NoArray:
End Function
Sub ChangeAll()
Dim ctl As Control
For Each ctl In Me.Controls
If IsCtlArray(ctl) Then
For i = ctl.lbound To ctl.ubound
setbackcolor ctl(i)
Next
Else
setbackcolor ctl
End If
Next
End Sub
If a control is an array, a for loop will pass each individual element of the array to the setcolor routine.
-
May 4th, 2009, 11:49 AM
#4
Re: passing control array name to perform a functions
Hi Hannes, didn't see your post before.
Good solution. Would it also work if you pass it a non-array control, or would it complain?
-
May 4th, 2009, 11:57 AM
#5
Re: passing control array name to perform a functions
I tried. If you pass a normal control the For each jj in CtlArr will fail.
It seems you need a function to decide whether a passed control is an array or not.
You could do that in the SetBCol() routine, too.
Code:
Sub SetBCol(Cntrl As Control) 'or possibly Cntrl as Object or Variant
On Error GoTo NoArray
Dim i%
For i = Cntrl.LBound To Cntrl.UBound
Cntrl(i).BackColor = theColor
Next
Exit Sub
NoArray:
Cntrl.BackColor = theColor
End Sub
Also the For loop can be a For Each version like Hannes showed.
The code
Code:
For each ctl in Me.Controls
SetBCol ctl
next
should then work
-
May 4th, 2009, 04:18 PM
#6
Re: passing control array name to perform a functions
WOF does not use: Dim jj As OptionButton anywhere in his program, but Hannes does
what are the tradeoffs? This always gets me a bit confused!!
For Each jj In CtrlArr <<<==== what does this do, exactly?
I know its a lot of questions, but this issue seems to come up a lot!
-
May 4th, 2009, 04:41 PM
#7
Re: passing control array name to perform a functions
For Each jj In CtrlArr would invariably pass each item in the CtrlArr as jj to the instructions of this For loop.
Look at the SetBCol as I described in the las post. I rewrite it, just as you had it with the color given as parameter
Code:
Sub SetBCol(Cntrl As Control, theColor as long) 'or possibly Cntrl as Object or Variant
On Error GoTo NoArray
Dim i%
For i = Cntrl.LBound To Cntrl.UBound
Cntrl(i).BackColor = theColor
Next
Exit Sub
NoArray:
Cntrl.BackColor = theColor
End Sub
Just note how the sub implements the For loop to go through all the elements of a control array. It counts the indexes from LBound to UBound.
Instead of doing so, you can use a loop which goes:
Code:
dim ctl as Control
For each ctl in Cntrl
cntl.BackColor= theColor
Next
There is no drawback using one or the other. Both ways are legitimate and possible of doing so.
It's just a way of decision.
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
|