|
-
Apr 4th, 2017, 06:53 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Apr 4th, 2017, 07:40 PM
#2
Re: UserControl
Code:
Private Sub UserControl_Initialize()
Static sFirst As Boolean
If Not sFirst Then
' code...
sFirst = True
End If
End Sub
-
Apr 4th, 2017, 09:15 PM
#3
Thread Starter
Hyperactive Member
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.
-
Apr 5th, 2017, 12:58 AM
#4
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
-
Apr 5th, 2017, 04:09 AM
#5
Re: UserControl
 Originally Posted by Ordinary Guy
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
-
Apr 5th, 2017, 06:45 AM
#6
Re: UserControl
 Originally Posted by Ordinary Guy
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?
-
Apr 5th, 2017, 04:10 PM
#7
Thread Starter
Hyperactive Member
Re: UserControl
 Originally Posted by dreammanor
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
 Originally Posted by Eduardo-
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
 Originally Posted by SamOscarBrown
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.
-
Apr 5th, 2017, 05:40 PM
#8
PowerPoster
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...
-
Apr 5th, 2017, 08:05 PM
#9
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.
-
Apr 5th, 2017, 08:13 PM
#10
PowerPoster
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...
-
Apr 5th, 2017, 08:25 PM
#11
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?
-
Apr 5th, 2017, 08:29 PM
#12
PowerPoster
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...
-
Apr 5th, 2017, 08:38 PM
#13
Re: UserControl
 Originally Posted by ThEiMp
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.
-
Apr 5th, 2017, 08:42 PM
#14
PowerPoster
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...
-
Apr 6th, 2017, 11:14 AM
#15
Thread Starter
Hyperactive Member
Re: UserControl
To what advantage would it be to put the app in the resource file. Would it make it load any faster.
-
Apr 6th, 2017, 11:34 AM
#16
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.
-
Apr 6th, 2017, 11:37 AM
#17
Thread Starter
Hyperactive Member
Re: UserControl
 Originally Posted by Eduardo-
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
 Originally Posted by Eduardo-
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
-
Apr 6th, 2017, 12:34 PM
#18
Re: UserControl
 Originally Posted by Ordinary Guy
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.
 Originally Posted by Ordinary Guy
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.
-
Apr 6th, 2017, 04:38 PM
#19
Thread Starter
Hyperactive Member
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
-
Apr 6th, 2017, 04:47 PM
#20
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).
-
Apr 6th, 2017, 06:12 PM
#21
Thread Starter
Hyperactive Member
Re: [RESOLVED] UserControl
Good suggestion. I changed it to use name only
-
Apr 6th, 2017, 07:32 PM
#22
PowerPoster
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...
-
Apr 6th, 2017, 07:35 PM
#23
PowerPoster
Re: [RESOLVED] UserControl
 Originally Posted by Eduardo-
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...
-
Apr 7th, 2017, 05:16 AM
#24
PowerPoster
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...
-
Apr 7th, 2017, 10:20 AM
#25
Thread Starter
Hyperactive Member
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
-
Apr 7th, 2017, 11:40 AM
#26
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|