Results 1 to 26 of 26

Thread: [RESOLVED] UserControl

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Resolved [RESOLVED] UserControl

    How to prevent executing code in Initialize event more than one time

    I want to execute the code on first time run only. If closing app or changing from UserControl to parent Form while in design mode I do not want the code executed again. I tried Ambient.UserMode but that causes error
    Last edited by Ordinary Guy; Apr 4th, 2017 at 07:16 PM.

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,667

    Re: UserControl

    Code:
    Private Sub UserControl_Initialize()
        Static sFirst As Boolean
        
        If Not sFirst Then
            
            ' code...
            
            sFirst = True
        End If
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: UserControl

    I already tried that and it doesn't seem to work. What happens is when I close the app it cycles back to the Initialize and the boolean is again False so it enters the code again which is what I don't want.

  4. #4
    PowerPoster
    Join Date
    Sep 2012
    Posts
    2,083

    Re: UserControl

    Code:
    Option Explicit
    
    Private m_Initialized As Boolean
    
    Private Sub DoInitialize()
        
        If m_Initialized = False Then
            m_Initialized = True
            
            '=====================================
            ' place some initialize code
            '=====================================
        
            Debug.Print "Initialized " & Now
            
        End If
        
    End Sub
    
    Private Sub UserControl_Initialize()
        Call DoInitialize
    End Sub

  5. #5
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,667

    Re: UserControl

    Quote Originally Posted by Ordinary Guy View Post
    I already tried that and it doesn't seem to work. What happens is when I close the app it cycles back to the Initialize and the boolean is again False so it enters the code again which is what I don't want.
    Add a bas module and put a public variable:

    Code:
    Public gInitialized as Boolean
    Code:
    Private Sub UserControl_Initialize()
        If Not gInitialized Then
            
            ' code...
            
            gInitialized = True
        End If
    End Sub

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: UserControl

    Quote Originally Posted by Ordinary Guy View Post
    I already tried that and it doesn't seem to work. What happens is when I close the app it cycles back to the Initialize and the boolean is again False so it enters the code again which is what I don't want.
    Are you saying WHEN you are closing it, or when you re-run the app?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: UserControl

    Quote Originally Posted by dreammanor View Post
    Code:
    Option Explicit
    
    Private m_Initialized As Boolean
    
    Private Sub DoInitialize()
        
        If m_Initialized = False Then
            m_Initialized = True
            
            '=====================================
            ' place some initialize code
            '=====================================
        
            Debug.Print "Initialized " & Now
            
        End If
        
    End Sub
    
    Private Sub UserControl_Initialize()
        Call DoInitialize
    End Sub
    No, no, no. That's the same as previously posted except you just put it in another sub but the effect is the same

    Quote Originally Posted by Eduardo- View Post
    Add a bas module and put a public variable:

    Code:
    Public gInitialized as Boolean
    Code:
    Private Sub UserControl_Initialize()
        If Not gInitialized Then
            
            ' code...
            
            gInitialized = True
        End If
    End Sub
    Nope, using a .BAS doesn't change the situation

    Quote Originally Posted by SamOscarBrown View Post
    Are you saying WHEN you are closing it, or when you re-run the app?
    I'm saying when I close (closing) the running app (press on X button or a Command button)

    There are 4 time the Initialized event is entered (and thus executes the code in the event)

    1) When project is launched - double clicking on .vbp file - that's bad

    2) When in Design Mode I move from viewing UserControl to parent Form - that's bad

    3) When I run the app - this is the only time I want it to enter the Initialize event

    4) When I close the app (still in running mode) it enters the event before it returns to Design Mode - that's bad

    This wouldn't be a problem if all I was doing was to initialize a few variables but that is not what I am doing. In the Initialize event (and I'm guessing that I shouldn't be doing this here) I Shell another application because I want that other app up and running without any user intervention. If not the Initialize event where else can I do this - can't use the Show event, that's even worse

    What's happening is when the Initialize event is entered these 4 times all variables have been set back to the design mode values. This means I shell the other app 4 times and I can see 4 entries in the Task Manager

    The only way I am able to prevent this is to call a public sub in the UC from the Form Load of the Parent Form to run these first time instructions but this then means that the UC is dependant on the parent Form which I don't want
    Last edited by Ordinary Guy; Apr 5th, 2017 at 04:53 PM.

  8. #8
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: UserControl

    When I use a UserControl, when I open, save, compile my ActiveX Controls, they execute with the UserControl_Initalize Handler, too. So then I am perplex into what do to here, too. So then Bump
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Love Shack | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University | Ambra Cheese |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  9. #9
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: UserControl

    Initialize is for basic, static initialization.

    Many things should be initialized within InitProperties for a newly created instance.

    For created instances use ReadProperties, when Ambient.UserMode is valid because at that point the control has been sited on its container.

  10. #10
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: UserControl

    Yeah, I know that you can setup the ReadProperties and WriteProperties. But then really UserControl_Initalize is only for setting the values of Variables and nothing more. For setting properties that are bundled into the ActiveX Control for instance like Enabled, you can use the handler ReadProperties. But then that is only for the properties that are able to be changed by the user and also the IDE, even. Just about any kind of property that is inside a Let Public or even a Get Public. I have also used Set Public, even they can be changed. anything outside of these aren't required to be changed for any reason, something like (Name) and (Custom) and also (About) for instance, can't be changed inside the ReadProperties handler, even
    Last edited by ThEiMp; Apr 5th, 2017 at 08:18 PM.
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Love Shack | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University | Ambra Cheese |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  11. #11
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,667

    Re: UserControl

    In a bas module:

    Code:
    Public gAppShelled As Boolean
    In the usercontrol:

    Code:
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        If Not gAppShelled Then
            If Ambient.UserMode Then
                gAppShelled = True
                Shell "mspaint.exe", vbHide
            End If
        End If
    End Sub
    This would shell the app only once in the project, as you said.

    But, what if there are more than one usercontrol placed on the form, or in several forms?
    Shouldn't you shell one instance of the app for each usercontrol?

  12. #12
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: UserControl

    I guess the OP wants the MsPaint.exe to be added to the Resource file, like a built into App, hey OP
    Code:
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        If gAppShelled = True Then Shell "mspaint.exe", vbHide
    End Sub
    Last edited by ThEiMp; Apr 5th, 2017 at 08:32 PM.
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Love Shack | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University | Ambra Cheese |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  13. #13
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: UserControl

    Quote Originally Posted by ThEiMp View Post
    I guess the OP wants the MsPaint.exe to be added to the Resource file, like a built into App, ...
    Er, that would probably be considered piracy. And in any case Paint.exe is changed to match each version of Windows. There is no guarantee an old version would run properly on a newer version of Windows.

    This would be a very poor idea.

  14. #14
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: UserControl

    Well I know IT IS piracy, but then I thought that he made his version of Paint.exe, which now reading back on the notes, here then it says that he didn't make the Paint.exe, app as per say. I am making my own apps and deploying them as virus free apps, and from a resource file, embedded into the build. but then that is totally invalid here, because they are what I have written and is a manifestation of my own thoughts, after four long years of asking the programmers what they want to see in the apps, even
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Love Shack | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University | Ambra Cheese |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: UserControl

    To what advantage would it be to put the app in the resource file. Would it make it load any faster.

  16. #16
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: UserControl

    Say Ordinary Guy,

    It sounds like you're struggling with the idea of the scope and lifetime of a variable (or boolean flag). For any VB6 program, I could think of many scope and lifetime levels. I'll outline these various levels and the ways I often use them.

    • Dim in procedure. Scope: the procedure. Lifetime: that single call into the procedure.
    • Static in procedure. Scope: the procedure. Lifetime: single execution of the whole app (although if it's a COM module, it'll be that instantiation of the COM module. See below.)
    • Dim at module level for COM (class, form, user control) module. Scope: anywhere in that instance of the module. Lifetime: For as long as that instance of the module is in existence.
    • Dim at module level for BAS module. Scope: anywhere in the module. Lifetime: single execution of the whole app.
    • Public at module level for BAS module. Scope: anywhere in the app, including other COM modules. Lifetime: single execution of the whole app.
    • Registry (HKCU area) (SaveSetting, GetSetting). Scope: for the particular user logged into the computer. Also, when it's read into a variable, the above applies. Lifetime: forever, unless the computer is setup such that the HKCU area is purged when the user logs off.
    • Registry (HKLM area) (API calls). Scope: the whole computer. Also, same as HKCU, depends on what variable it's read into. Lifetime: typically forever.
    • Some file. Scope: whoever can read the file. And yet again, it depends on what variables the file's info is read into. Lifetime: typically forever.
    • File on a file server accessible by anyone using the app. Scope: whoever can read the file on the entire LAN (or WAN). Lifetime: typically forever. This is the one case where I might consider an INI file.


    Maybe that'll help you get your head around some of this stuff.

    All The Best,
    Elroy
    Last edited by Elroy; Apr 17th, 2017 at 05:38 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: UserControl

    Quote Originally Posted by Eduardo- View Post
    In a bas module:

    Code:
    Public gAppShelled As Boolean
    In the usercontrol:

    Code:
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        If Not gAppShelled Then
            If Ambient.UserMode Then
                gAppShelled = True
                Shell "mspaint.exe", vbHide
            End If
        End If
    End Sub
    This would shell the app only once in the project, as you said.
    It appears that I don't need the .bas module nor the checking the variable. So far I have it working with only this code:

    Code:
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
     If Ambient.UserMode Then
       Shell "mspaint.exe", vbHide
     End If
    End Sub

    Quote Originally Posted by Eduardo- View Post
    But, what if there are more than one usercontrol placed on the form, or in several forms?
    Shouldn't you shell one instance of the app for each usercontrol?
    MSPaint would have to be shelled for each occurance of the control

  18. #18
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,667

    Re: UserControl

    Quote Originally Posted by Ordinary Guy View Post
    It appears that I don't need the .bas module nor the checking the variable. So far I have it working with only this code:

    Code:
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
     If Ambient.UserMode Then
       Shell "mspaint.exe", vbHide
     End If
    End Sub
    Yes, but as you said it should be shelled only once....

    But you are right, you need to shell it only once per usercontrol. Hence my following question.

    Quote Originally Posted by Ordinary Guy View Post
    MSPaint would have to be shelled for each occurance of the control
    OK, and did you think that the user may be with one instance of Paint already open? (and doing something)

    You also need a way to differentiate each instance that you run, because just looking for the window with FindWindow doesn't do that.

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: [RESOLVED] UserControl

    If I use the get handle by PID that wont happen

    Code:
    Private Sub ShellMSPaint()
     PID = Shell("C:\windows\system32\mspaint.exe", vbHide)
    End Sub
    
    Private Sub EmbedMSPaint()
     '
     ' Embed MSPaint
     '
     If PID > 0 Then
       Do
         MSPaintHwnd = GetHwnd(PID)
         If MSPaintHwnd > 0 Then Exit Do
       Loop
       
      SetParent MSPaintHwnd, UserControl.hWnd
     Else
       MsgBox "Unable To Embed MSPaint"
     End If
    End Sub
    
    Private Function GetHwnd(ByVal ProcessID As Long) As Long
     '
     ' Get Hwnd from Process ID
     '
     Dim lHwnd As Long, RetHwnd As Long, RetPID As Long
        
     lHwnd = GetDesktopWindow()
     RetHwnd = GetWindow(lHwnd, GW_CHILD)
        
     Do While RetHwnd
       If IsWindowVisible(RetHwnd) Then
         GetWindowThreadProcessId RetHwnd, RetPID
                
         If RetPID = ProcessID Then
           Exit Do
         End If
       End If
       RetHwnd = GetWindow(RetHwnd, GW_HWNDNEXT)
     Loop
        
     GetHwnd = RetHwnd
    End Function

  20. #20
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,667

    Re: [RESOLVED] UserControl

    Ok, another suggestion: do not shell mspaint.exe with the full path, not every Windows is in C:\Windows (mine is not)

    mspaint.exe is in the system path, so you can shell it with just the filename (or get the Windows\system32 path with some API call).

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: [RESOLVED] UserControl

    Good suggestion. I changed it to use name only

  22. #22
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: [RESOLVED] UserControl

    the only reason for to add an exe into a resource is to deploy there, it will be a smaller file size and also load that much slower, because it has to inflate the file size of each of the exe files
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Love Shack | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University | Ambra Cheese |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  23. #23
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: [RESOLVED] UserControl

    Quote Originally Posted by Eduardo- View Post
    Ok, another suggestion: do not shell mspaint.exe with the full path, not every Windows is in C:\Windows (mine is not)

    mspaint.exe is in the system path, so you can shell it with just the filename (or get the Windows\system32 path with some API call).
    Code:
    Shell (App.Path & "\Paint.exe"), vbNormal
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Love Shack | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University | Ambra Cheese |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  24. #24
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: [RESOLVED] UserControl

    Post to the OP: How can this thread be resolved, because we haven't covered any ground work here on in the thread, here right about now, even. please post your results so that we can be able to figure out if the thread can be resolved properly, even +<
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Love Shack | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University | Ambra Cheese |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: [RESOLVED] UserControl

    Resolved at post #17

    Code:
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
     If Ambient.UserMode Then
       Shell "mspaint.exe", vbHide
     End If
    End Sub

  26. #26
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: [RESOLVED] UserControl

    -- That was what the OP was looking for, gee sorry!!
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Love Shack | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University | Ambra Cheese |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

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