Passing control name to sub
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Passing control name to sub

  1. #1
    Join Date
    Nov 2005
    Posts
    95

    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?

  2. #2
    Join Date
    May 2005
    Posts
    49

    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.

  3. #3
    Join Date
    Nov 2005
    Posts
    95

    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

  4. #4
    Join Date
    Nov 2005
    Posts
    95

    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

  5. #5
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,833

    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

  6. #6
    Join Date
    Apr 2009
    Posts
    394

    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

  7. #7
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,833

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center