Results 1 to 7 of 7

Thread: Passing Controls/Forms Objects to a Function

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    9

    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...........

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Passing Controls/Forms Objects to a Function

    Call SetTheme(ACtl), not SetTheme(ACtl)
    or... SetTheme ACtl

    How now?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    9

    Re: Passing Controls/Forms Objects to a Function

    LaVolpe,

    You got it in one..... thanks so much !

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    9

    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

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    9

    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
  •  



Click Here to Expand Forum to Full Width