-
May 1st, 2009, 11:53 AM
#1
Passing control name to sub
I need to pass the name of a control to a procedure to change the color...but this does not seem to work...any thoughts?
Dim CTRL As Control
For Each CTRL In Controls
If TypeOf CTRL Is Label Then CTRL.BackColor = TheColor
If TypeOf CTRL Is Frame Then CTRL.BackColor = TheColor
If TypeOf CTRL Is OptionButton then ReviseColor (CTRL)
If TypeOf CTRL Is Slider Then CTRL.BackColor = TheColor
If TypeOf CTRL Is CheckBox Then CTRL.BackColor = TheColor
Next CTRL
.......
ReviseColor (CTRL as Control)
CTRL.BackColor = VbRED
...more processing
....more processing
end sub
this revise color subroutine does nothing...what's wrong?
-
May 1st, 2009, 12:28 PM
#2
Re: Passing control name to sub
Do you intend to change colors on the fly? If not, I would suggest just using Form_Load and name the control and its background color.
-
May 1st, 2009, 12:36 PM
#3
Re: Passing control name to sub
no, I need to have ReviseColor working as it willl be called from many places....also I want it generic, so I can pass the names of several other controls to it---aso I can get as much use from it as possible...it also does other processing things that I didn't list
-
May 1st, 2009, 10:24 PM
#4
Re: Passing control name to sub
Do you need to set up a speical class to do this type of thing? Seems like it should be easy...but so far no luck
-
May 1st, 2009, 11:15 PM
#5
Re: Passing control name to sub
I can't tell what the problem may be from the little piece of code you posted. I assume that the latter part is a sub but I see no call to it, nor do I know what parameter may be passed to it as the CTRL and can not tell if there may be another line in that same sub that cancels out the color change.
You should be able to pass a control to a sub and work with it there even if the sub is in a different module but you have not provided enough info to point to the error.
Next time use [ code ] [ /code ] {without the spaces} tags around your code and indent it for readability
-
May 2nd, 2009, 02:13 PM
#6
Re: Passing control name to sub
this works...
Code:
Option Explicit
Private Sub Command1_Click()
Dim FC As Control
For Each FC In Me.Controls
If TypeOf FC Is OptionButton Then ChangeColor FC
Next
End Sub
Private Sub ChangeColor(C As Control)
C.BackColor = vbRed
End Sub
however, an error is raised if I do...
Code:
Option Explicit
Private Sub Command1_Click()
Dim FC As Control
For Each FC In Me.Controls
If TypeOf FC Is OptionButton Then ChangeColor (FC)
Next
End Sub
Private Sub ChangeColor(C As Control)
C.BackColor = vbRed
End Sub
the only difference is the parens around my variable FC. So perhaps if you remove them from your code, your code may work...
Good Luck
-
May 2nd, 2009, 03:51 PM
#7
Re: Passing control name to sub
() in vb6 denotes a function call that will return a result.
Been a long time since I used it but I think Call MySub() is supported BUT MySub() is not and will generate an error.
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
|