Solved

I don't really need it - but it's so fuuny...

Posted on 2000-05-04
10
125 Views
Last Modified: 2010-05-02
i wonder who can give a FULL explanation for this thing that i don't know if i should laught or cry at :

add 2 forms to new project and 1 Command button to Form1 :

paste this code to form1 code area :
----------------------------------------
Option Explicit
Private x As Boolean

Private Sub Command1_Click()
    x = False
    Form2.Show
    Form2.Hide
    MsgBox x
End Sub

Private Sub Form_Activate()
    If x = True Then Exit Sub
    x = True
    MsgBox "form1 activated"
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Unload Form2: Set Form2 = Nothing
End Sub

--------------------------------------
that's it.
compile to native code or p-code

running from VB IDE the command1_Click gives us 1 Msgbox :
1. "False"
that means that Form1's Activate event does not occure when it recieves the focus back.

runnung the EXE the command1_Click gives us 2 Msgboxes !!! :
1. "form1 activated"
2. "False"
that means that right after Form2.hide, when form1 recieve the focus
back - it's Activate event occures, and that's FINE, but on that event
the "x" Boolean is changes to True. right ??? So, why the next MsgBox
tells us that X=False ?????

-----------------------------------
One BIG question is why at all there's a diffrence between the EXE and
IDE behaviour ?????
SECOND BIG question is how the EXE pass the Activate event code and
somehow ignores the line : X=True ?????
------------------------------------






0
Comment
Question by:AnswerTheMan
  • 5
  • 4
10 Comments
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
Answer to Q2:

Timing, timing, timing!  A simple change to the code will show that both sections of code are actually running simultaneously.  

Modify the Command1_Click thusly:

    Form2.Hide
    DoEvents         ' Add this line
    MsgBox x

You will see that the messagebox has returned 'True'.

It appears that the value of x is passed to the MsgBox call prior to the assignment of x in the _Activate code.  Why the second messagebox pops up prior to the first one is a different issue (and I don't know the answer to that one).

As for the first condition - I don't KNOW that this is true, but I BELIEVE it is.  Here's a quote from a discussion about form_load, form_activate, and form_initialize events:

As was stated previously, the Activate event is called every time a form becomes the active window, as long as the focus has moved between forms in the application. ***The Activate event is not called if the previous window that had the focus was outside of the application.*** <emphasis mine>

I believe that focus is being passed momentarily to the IDE window, then to Form1.
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
I think it is timing as bhess suggests and that the Msgbox call is somehow linked to the problem.  Try setting Autoredraw to true on form1, and then use this code:

Option Explicit
Private x As Boolean

Private Sub Command1_Click()
    x = False
    Form2.Show
    Form2.Hide
   
    Print x
End Sub

Private Sub Form_Activate()
    If x = True Then Exit Sub
    x = True
    Print "form1 activated"
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Unload Form2: Set Form2 = Nothing
End Sub


Even without the DoEvents, the events occur in the order one expects in both the IDE and the exe.  In fact if you put in the Doevents you will see the value turned to true and the order of the print statements is reversed.  
0
 
LVL 5

Author Comment

by:AnswerTheMan
Comment Utility
tnx bhess1.
:) i'm not asking for a solution for a problem i have. i don't. i know what to do to bypass such things.

of course DoEvents makes it right, but there is a big CONTRADICTION between waht it does and between what it's supposed to do.
DoEvents meant to support what you call : "both sections of code are actually running simultaneously.".
BUT IT'S NOT DOING IT !!
actually, DoEvents pass the process to the Activate Event and the process return to its previous point ONLY AFTER the Activate Evnet code is completed.
That is not "both sections of code are actually running simultaneously."

more then that : such process has to be (logiclly) done without the DoEvents, as was actually happened in The EXE.
but there - it was really  "both sections of code are actually running simultaneously." as only PART of the Activate code was implemented.....

as for the IDE-EXE issue :
your explanation seems to me very logical. however - i've never bumped into any MS\MSDN documentation about it. if that's the case it's very serious defect in VB IDE. how can we debug our work if we can't *TRUST* the IDE to behave like the EXE ? we'll have to compile it every second to check how the EXE works. i'm working now on a project that takes 15 minutes to compile....


0
 
LVL 5

Author Comment

by:AnswerTheMan
Comment Utility
PaulHews : tnx.
the MsgBoxes point is intresting.
ommiting it as you suggest, really make the EXE and IDE behave the same (even without changing the AutoRedraw).

However - this question assume that there *IS* a need to display a MsgBox in those points.

regaring your "the events occur in the order one expects " - i CAN'T agree with you.
i expext my code to fire the Activate EVENT right as the Form2.Hide is passed.
Hiding a form is a Trigger for other form activate event. i expext that my code will show upon the button click :
1. "form1 activated"
2. "true"

your way i get :
1. "false"
2. "form1 activated"

that's not logical to me and not what i expext.
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
I think that the form.hide is performed asynchronously. (Not 100% sure! :) ) The method returns control to the calling code before the action is finished.  Thus when DoEvents is omitted, the form hide is not finished before the msgbox is popped up (False, since form1 is not active yet, then you get "form1 activated."  DoEvents allows the action to complete (it is designed to let background actions work when foreground code is executing) and so form1 is activated and the x var is toggled to True.  Only then does the foreground code pop-up the MsgBox.  Hope this explains it.  :)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Author Comment

by:AnswerTheMan
Comment Utility
hmmm... maybe... but still - this does not go along with the fact that running MY EXE - "form1 activated" came before
"false" - what means that right after the HIDE command - the execution moved to form1. that's a fact.
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
If you combine the print and the msgbox, you'll see that the order of the events is as expected.  There is something (???) going on with the msgboxes that pops the wrong one up first.  Maybe the Activate msgbox has some kind of priority.  Obviously Windows and VB are doing something in the background.

As for the IDE, bhess may be right about that, but I still think it has something to do with the actual msgbox call.  This is not the first time I've heard that IDE and Exe act differently with msgbox.  For example, in the IDE, all code execution stops with a msgbox, while in the exe, a timer will still function when a msgbox is popped up.


Option Explicit
Private x As Boolean

Private Sub Command1_Click()
    x = False
    Form2.Show
    Form2.Hide
    Print x
    MsgBox x
End Sub

Private Sub Form_Activate()
    If x = True Then Exit Sub
    x = True
    Print "form1 activated"
    MsgBox "form1 activated"
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Unload Form2: Set Form2 = Nothing
End Sub

0
 
LVL 38

Accepted Solution

by:
PaulHews earned 100 total points
Comment Utility
Here's an article that has some more info:  Q201741 on MS Knowledge base.
0
 
LVL 5

Author Comment

by:AnswerTheMan
Comment Utility
PaulHews :
tnx for this link.
the background for this Q is a heavy project i've sold to a distibuter, and after 100 sales he came back with complains about an error that was totally unexpected for me.
i've found out that it was a FOCUS problem that was not occured from inside VB IDE, but happened at all clients machines including my own when running the EXE. it's not that i've not tested my EXE before selling, but i did not tested that certain corner in the app with the EXE....
fixing it took less then 5 minutes - but
redistributing to 100 clients is something else....
now, the link you gave me (which i blame myself for not finding it myself) - says exactly what i've found myself - a FOCUS problem.
it was clear to me before posting the Q, but i was looking for MS official documentation.
since you gave it - you deserve the points and tnx again.

0
 
LVL 5

Author Comment

by:AnswerTheMan
Comment Utility
if i was selling an IDE - i was ashamed to sell a tool that supposed to act as an EXE and does not do it, and make people lose time and money because they trust it.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now