Results 1 to 36 of 36

Thread: [RESOLVED] Prevent Form_Activate

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Resolved [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

  2. #2
    Fanatic Member Episcopal's Avatar
    Join Date
    Mar 2019
    Location
    Brazil
    Posts
    547

    Re: Prevent Form_Activate

    Do you want to prevent the Form_Activate event or prevent Form1 from focusing

    ???????

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

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


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by Episcopal View Post
    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.


    Quote Originally Posted by dee-u View Post
    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

  5. #5
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    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.

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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.

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

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


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    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.

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    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

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by Bobbles View Post
    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.
    /Jimboat

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by Eduardo- View Post
    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

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by dee-u View Post
    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.
    /Jimboat

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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.

  16. #16
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Prevent Form_Activate

    Quote Originally Posted by Jimboat View Post
    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>

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by dilettante View Post
    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?
    /Jimboat

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by wqweto View Post
    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

  19. #19
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    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?

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by Eduardo- View Post
    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.
    /Jimboat

  21. #21
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Prevent Form_Activate

    Quote Originally Posted by Jimboat View Post
    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.

  22. #22
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Prevent Form_Activate

    Quote Originally Posted by Jimboat View Post
    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.

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by Eduardo- View Post
    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.
    /Jimboat

  24. #24
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Prevent Form_Activate

    Quote Originally Posted by Jimboat View Post
    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.

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by Eduardo- View Post
    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

  26. #26
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Prevent Form_Activate

    Quote Originally Posted by Jimboat View Post
    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.

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by Eduardo- View Post
    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??)
    /Jimboat

  28. #28
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Prevent Form_Activate

    Quote Originally Posted by Jimboat View Post
    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.

  29. #29
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

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

  30. #30

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by Eduardo- View Post
    Ah, Ok. You have other code that you are running in the form activate.
    YES.

    Quote Originally Posted by Eduardo- View Post
    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

  31. #31
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Prevent Form_Activate

    Quote Originally Posted by Jimboat View Post
    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

  32. #32

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Prevent Form_Activate

    Quote Originally Posted by Eduardo- View Post
    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.
    /Jimboat

  33. #33
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: [RESOLVED] Prevent Form_Activate

    Thanks for the praises,
    Rob
    PS I declare my Boolean flag at form level (not a global)

  34. #34
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: [RESOLVED] Prevent Form_Activate

    Quote Originally Posted by Bobbles View Post
    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.

  35. #35
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  36. #36
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: [RESOLVED] Prevent Form_Activate

    Quote Originally Posted by westconn1 View Post
    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
  •  



Click Here to Expand Forum to Full Width