-
Feb 25th, 2021, 07:30 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Prevent Form_Activate
With form1 active/visible, I show form2. Is there a way to prevent form1_activate from firing after form2 is closed? Form2 is used this way frequently, with various different other forms, so I’m trying to find an easy way to ensure this behaviour each time form2 is used.
Last edited by Jimboat; Feb 26th, 2021 at 07:10 AM.
/Jimboat
-
Feb 25th, 2021, 08:06 PM
#2
Re: Prevent Form_Activate
Do you want to prevent the Form_Activate event or prevent Form1 from focusing
???????
-
Feb 25th, 2021, 08:22 PM
#3
Re: Prevent Form_Activate
Perhaps a solution is to use a global variable like form2Closed where you would set it to true if form2 is closed then check its value at the activate event of form1? I'm wondering why you have to show form2 in the activate event of form1.
-
Feb 25th, 2021, 08:32 PM
#4
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by Episcopal
Do you want to prevent the Form_Activate event or prevent Form1 from focusing???????
Episcopal - Sorry for not being clear. I want to prevent the Form_Activate event from executing, when Form1 regains focus, when ever form2 is shown and closed.
Originally Posted by dee-u
Perhaps a solution is to use a global variable like form2Closed where you would set it to true if form2 is closed then check its value at the activate event of form1? I'm wondering why you have to show form2 in the activate event of form1.
dee-u - Yes, i thought of going this way of a global variable, which works fine, but form2 is called by many forms, so there's alot of maintenance to add the 'check value' lines to each form_activate. (form2 is not shown from the form1.activate, but rather called in one of other ways, such as cmdbutton on form1 or on other forms.)
i'm wondering if there is a clever way of preventing form_activate from executing after form2 closes, without having to add the code to check and reset globalvar on each form_activate? maybe not possible? Your global var may be best approach. thanks for your help.
Last edited by Jimboat; Feb 28th, 2021 at 12:43 PM.
/Jimboat
-
Feb 25th, 2021, 08:56 PM
#5
Re: Prevent Form_Activate
I'll guess that you are using form activate to show form2 because you want to show it when form1 is already on the screen.
Question: do you want to show form2 every time that form1 activates or just once?
If it is just once, then you can have a static variable in the form activate event:
Code:
Private Sub Form_Activate()
Static sDone As Boolean
If Not sDone Then
sDone = True
form2.Show
End If
End Sub
Or put a timer in form1 with 1 millisecond:
Code:
Private Sub tmrShowForm2_Timer()
tmrShowForm2.Enabled = False
form2.Show
End Sub
If instead you want to show form2 every time that form1 activates, it would be something else.
-
Feb 25th, 2021, 08:57 PM
#6
Re: Prevent Form_Activate
Sounds like you are trying to solve the wrong problem. If you have code in Form_Activate that you don't want to run when that event happens, then it probably doesn't belong in Form_Activate.
As described, it sounds like you have a mess on your hands. Without seeing actual code, I think the only valid suggestion is for you to rework your code.
Good luck.
-
Feb 25th, 2021, 08:58 PM
#7
Re: Prevent Form_Activate
I am not sure there is a way to circumvent it from firing. I am thinking perhaps you could just hide form2 and prevent it from being closed, but I am not sure it will not fire the activate event.
-
Feb 25th, 2021, 09:56 PM
#8
Re: Prevent Form_Activate
I often have code that runs when a form loads.
Some code does not like running in Form Load (as the program is not loaded enough)
THUS I always put my code in the Form_Activate event.
I too became aware that Form_Activate can run more than once during the 'life' of the form.
SO I have a flag (at Form level) that my code sets to indicate - I have already run this code.
The next time the Form_Activate fires, my code first checks the flag.
If it sees that the code has already been run, it skips running the code.
-
Feb 26th, 2021, 01:53 AM
#9
Re: Prevent Form_Activate
Yep a flag in the activate event that changes state the first time the activate event fires and just exits afterward seems like a quick and easy solution.
There may be other, better solutions but without knowing what the code is doing or why it is there not much could be offered.
-
Feb 26th, 2021, 02:13 AM
#10
Re: Prevent Form_Activate
IIRC, the Activate-Event fires when a Form gets the Focus (back).
So as the others said: you need a flag preventing the Code in the Event to run.
I don't like global variables, so another approach might be to define a Public Boolean variable "DontFireActivate" in your Form1, which gets set to true before Form2 (or any other Form) closes.
In Form1 inside the Activate-Event the last line should reset the variable (in my example to False).
But this way is considered "bad practice", since a child would update a parent.
Well, damned if i do, damned if i don't
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Feb 26th, 2021, 06:36 AM
#11
Re: Prevent Form_Activate
you could avoid using a global by using a static within the activate event
on first run change the static, then never run again, with out reloading the form
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 26th, 2021, 07:13 AM
#12
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by Bobbles
I often have code that runs when a form loads.
Some code does not like running in Form Load (as the program is not loaded enough)
THUS I always put my code in the Form_Activate event.
I too became aware that Form_Activate can run more than once during the 'life' of the form.
SO I have a flag (at Form level) that my code sets to indicate - I have already run this code.
The next time the Form_Activate fires, my code first checks the flag.
If it sees that the code has already been run, it skips running the code.
Bobbles - yes, this is the issue. Seems like the global var flag is the easiest approach. thanks for your experience.
-
Feb 26th, 2021, 07:16 AM
#13
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by Eduardo-
I'll guess that you are using form activate to show form2 because you want to show it when form1 is already on the screen.
thx Eduardo. not using form_activate to show form2 (sorry if i gave that impression, but clarified in post #4). to be more clear, i want form_activate to execute under all normal situations of getting focus except for after form2 closes. seems like the global flag will be easiest approach.
Last edited by Jimboat; Feb 26th, 2021 at 07:20 AM.
/Jimboat
-
Feb 26th, 2021, 07:18 AM
#14
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by dee-u
Perhaps a solution is to use a global variable like form2Closed where you would set it to true if form2 is closed then check its value at the activate event of form1? I'm wondering why you have to show form2 in the activate event of form1.
dee-u - thanks again for your thoughts. seems like your suggestion of global variable flag is easiest approach. thanks also to Bobbles.
-
Feb 26th, 2021, 10:39 AM
#15
Re: Prevent Form_Activate
If the issue is to perform actions one time on load that require control siting (things like invoking the SetFocus method) there is a way to do that.
In the Form_Load handler you can defer those operations after other initialization, then invoke the Show method. I some cases right after Show you might have to call the DoEvents() function once to let everything settle further if your initialization tweaked properties at run time. Right after that you should be able to perform initializing operations that require siting.
It's a lot cleaner than the juggling act described above that perverts the Activate event. It also carries less baggage and run time performance penalties, is easier to maintain over time, and less confusing to future readers.
-
Feb 26th, 2021, 12:20 PM
#16
Re: Prevent Form_Activate
Originally Posted by Jimboat
to be more clear, i want form_activate to execute under all normal situations of getting focus except for after form2 closes.
Just curious what possible task needs to be executed each time the form gets activated from a form within current application but *not* when being activated from a dialog in an external application (e.g. Alt+Tabbing)?
Most everyone else here needs to implement something on first activation only -- show a dependant form, start a task in a loop and update progress status on current form, etc. exotic opearations and considers Form_Activate one of the most useless events for much anything else.
cheers,
</wqw>
-
Feb 26th, 2021, 12:37 PM
#17
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by dilettante
If the issue is to perform actions one time on load that require control siting (things like invoking the SetFocus method) there is a way to do that.
In the Form_Load handler you can defer those operations after other initialization, then invoke the Show method. I some cases right after Show you might have to call the DoEvents() function once to let everything settle further if your initialization tweaked properties at run time. Right after that you should be able to perform initializing operations that require siting.
It's a lot cleaner than the juggling act described above that perverts the Activate event. It also carries less baggage and run time performance penalties, is easier to maintain over time, and less confusing to future readers.
dilettante - when you say 'initialization', do you refer to 'Form_Initialize'? Or are you suggesting putting all the actions into Form_Load with a DoEvents(), and not using Form_Activate at all?
-
Feb 26th, 2021, 12:48 PM
#18
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by wqweto
Just curious what possible task needs to be executed each time the form gets activated from a form within current application but *not* when being activated from a dialog in an external application (e.g. Alt+Tabbing)?
Most everyone else here needs to implement something on first activation only -- show a dependant form, start a task in a loop and update progress status on current form, etc. exotic opearations and considers Form_Activate one of the most useless events for much anything else.
cheers,
</wqw>
wqweto - thanks for explanation. All same application. Some of the first 'initializing' setups needed don't work well when they're in the Form_Load (due to some controls not set up yet), but work fine if they're in Form_Activate. The Form_Activate does it's job well when the Form regains focus at some time. But after calling Form2 (somewhat like a custom dialog), i don't want many of these setup initializations resetting again. Hence desire to not Load_Activate in that case only.
Last edited by Jimboat; Feb 26th, 2021 at 12:56 PM.
/Jimboat
-
Feb 26th, 2021, 01:01 PM
#19
Re: Prevent Form_Activate
If you closed Form2, then opened Form3, then closed Form3, why would you need to show Form2 again while returning to Form1 in that case?
-
Feb 26th, 2021, 01:06 PM
#20
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by Eduardo-
If you closed Form2, then opened Form3, then closed Form3, why would you need to show Form2 again while returning to Form1 in that case?
Don't need to do that.
'Form2' is just a simple (custom) dialog, and don't want the other forms re-initializing (Form_Activate) just because the Form2 dialog was shown.
-
Feb 26th, 2021, 01:15 PM
#21
Re: Prevent Form_Activate
Originally Posted by Jimboat
dilettante - when you say 'initialization', do you refer to 'Form_Initialize'? Or are you suggesting putting all the actions into Form_Load with a DoEvents(), and not using Form_Activate at all?
Don't use Form_Activate unless your program needs to for a legitimate purpose.
Much of what a Form needs to do to initialize can be done in Form_Initialize or in Form_Load. However a few things require the Form and its controls to be fully sited first. Do that near the very end of Form_Load by calling the Show method first.
It's rare that you might need to call DoEvents after Show, though if you have twiddled certain properties you might have to.
-
Feb 26th, 2021, 01:26 PM
#22
Re: Prevent Form_Activate
Originally Posted by Jimboat
Don't need to do that.
'Form2' is just a simple (custom) dialog, and don't want the other forms re-initializing (Form_Activate) just because the Form2 dialog was shown.
That's what I and everyone else guessed. You need to only show Form2 automatically from Form1 at start-up, and no more.
You don't need the Form_Activate for that, but if you insist, you can use it.
The two alternatives are in my #post 5, I prefer the second one (a timer).
Of course there can be many more ways to do it. But for such a simple task a timer works.
PS: there is a third option proposed here by dilettante, Show in the Form_Load.
Last edited by Eduardo-; Feb 26th, 2021 at 01:32 PM.
-
Feb 26th, 2021, 01:56 PM
#23
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by Eduardo-
That's what I and everyone else guessed. You need to only show Form2 automatically from Form1 at start-up, and no more.
You don't need the Form_Activate for that, but if you insist, you can use it.
The two alternatives are in my #post 5, I prefer the second one (a timer).
Of course there can be many more ways to do it. But for such a simple task a timer works.
PS: there is a third option proposed here by dilettante, Show in the Form_Load.
Eduardo-thanks. Why do I need to automatically show form2 from Form1.Form_Load? That’s not my intention, but don’t think I understand why that might help me? Currently Form2 is called manually when needed.
-
Feb 26th, 2021, 02:21 PM
#24
Re: Prevent Form_Activate
Originally Posted by Jimboat
Eduardo-thanks. Why do I need to automatically show form2 from Form1.Form_Load? That’s not my intention, but don’t think I understand why that might help me? Currently Form2 is called manually when needed.
OK, I think that nobody understands now.
If you can't explain what you need to do (clearly, obviously. What you should had done in the OP BTW) nobody can help.
Good luck.
-
Feb 26th, 2021, 03:14 PM
#25
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by Eduardo-
OK, I think that nobody understands now.
If you can't explain what you need to do (clearly, obviously. What you should had done in the OP BTW) nobody can help.
Good luck.
Eduardo - thanks for trying. got a few good suggestions that are working now. so thanks all. Sorry that i must have confused the explanation of my problem.
for clarity...
With Form1 loaded and now active, I can call to show Form2, which is a just a kind of custom dialog form. Form2 is called manually by a command button or menu item. When Form2 is then closed, the Form1.Form_Activate event then executes, which resets a bunch of controls unnecessarily. My desire is to be able to load Form2 (the custom dialog) and ultimately close Form2 without having Form1's Form_Activate event firing.
The Form2 (custom dialog) is always called manually, not from Form1.Form_Load and not from Form1.Form_Activate. Form2 (the custom dialog) is used in this way frequently, called (manually) from various different other forms, so I’m trying to find an easy way to ensure this behavior (no Form_Activate firing) each time form2 is used.
The suggestions such as in Post#3 work OK. So, I'm fine. Was just wondering if there were other ways of doing it.
thanks for your help.
Last edited by Jimboat; Feb 26th, 2021 at 03:19 PM.
/Jimboat
-
Feb 26th, 2021, 03:23 PM
#26
Re: Prevent Form_Activate
Originally Posted by Jimboat
The Form2 (custom dialog) is always called manually, not from Form1.Form_Load and not from Form1.Form_Activate. Form2 (the custom dialog) is used in this way frequently, called (manually) from various different other forms, so I’m trying to find an easy way to ensure this behavior (no Form_Activate firing) each time form2 is used.
Then the question is why is it shown also in the Form_Activate event if it has code only to show in manually.
It still makes no sense.
-
Feb 26th, 2021, 03:26 PM
#27
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by Eduardo-
Then the question is why is it shown also in the Form_Activate event if it has code only to show in manually.
It still makes no sense.
Form2 is NOT shown in the Form_Activate event. (have i mistakenly said this somewhere??)
-
Feb 26th, 2021, 03:36 PM
#28
Re: Prevent Form_Activate
Originally Posted by Jimboat
Form2 is NOT shown in the Form_Activate event. (have i mistakenly said this somewhere??)
Ah, Ok. You have other code that you are running in the form activate.
It is the same, run that code on the Form_Load instead.
If there is something that can't be done in the Form_Load, like a SetFocus to a control, you can do it only once with the ways stated on post #5 or the other way in the Form_Load like dilettante said. It is really a simple thing.
BTW you will have that problem not only returning from Form2 but from any other form.
-
Feb 26th, 2021, 03:40 PM
#29
Re: Prevent Form_Activate
Also worth to mention that if it is a SetFocus, the proper way to handle it is to set that control with TabIndex = 0 (no SetFocus needed).
-
Feb 26th, 2021, 03:49 PM
#30
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by Eduardo-
Ah, Ok. You have other code that you are running in the form activate.
YES.
Originally Posted by Eduardo-
BTW you will have that problem not only returning from Form2 but from any other form.
In these cases it is either desired or OK for Form_Activate event to run.
Eduardo - thanks again for your help.
Last edited by Jimboat; Feb 26th, 2021 at 03:52 PM.
/Jimboat
-
Feb 26th, 2021, 03:54 PM
#31
Re: Prevent Form_Activate
Originally Posted by Jimboat
In these cases it is desired for Form_Activate event to run.
It is desired by you or by someone else?
What code is the one that needs to run from there?
Anyway it is extremely simple the solution, put a variable, as many already repeated here:
Code:
Private Sub Form_Activate()
Static sDone As Boolean
If Not sDone Then
sDone = True
code
code
End If
End Sub
-
Feb 26th, 2021, 04:05 PM
#32
Thread Starter
Addicted Member
Re: Prevent Form_Activate
Originally Posted by Eduardo-
Anyway it is extremely simple the solution, put a variable, as many already repeated here:
Code:
Private Sub Form_Activate()
Static sDone As Boolean
If Not sDone Then
sDone = True
code
code
End If
End Sub
Got it! Works fine. Thx again.
-
Feb 26th, 2021, 07:40 PM
#33
Re: [RESOLVED] Prevent Form_Activate
Thanks for the praises,
Rob
PS I declare my Boolean flag at form level (not a global)
-
Feb 26th, 2021, 08:06 PM
#34
Re: [RESOLVED] Prevent Form_Activate
Originally Posted by Bobbles
Thanks for the praises,
Rob
PS I declare my Boolean flag at form level (not a global)
That variable is not needed to be at form level, just a static in the procedure is enough. Since it is not used anywhere else.
PS: or perhaps you are talking about something else.
-
Feb 26th, 2021, 10:11 PM
#35
Re: [RESOLVED] Prevent Form_Activate
In these cases it is either desired or OK for Form_Activate event to run.
in this case a static would not be suitable
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 26th, 2021, 10:20 PM
#36
Re: [RESOLVED] Prevent Form_Activate
Originally Posted by westconn1
in this case a static would not be suitable
Why you say so?
(we are talking that he needs the code in Form_Activate to run only once, at start-up).
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
|