Results 1 to 7 of 7

Thread: Waiting for form to unload

  1. #1

    Thread Starter
    Junior Member Cakkie's Avatar
    Join Date
    May 2002
    Location
    Olen, Belgium
    Posts
    24

    Waiting for form to unload

    I have a little problem.

    I have a form that needs some input. At some point, a user can press a button which shows another form modal.
    That form on turn, has a button that needs to show another form. That form however, is a MDIChild, and is also used elsewhere in the program.
    This results in me not beeing able to show that form modal, nor can I show the second form non-modal since the first is already modal.
    What I'm looking for is a solution that doesn't require me to redesing half the application, so I'm looking for a way to wait for a form until it unloads. Currently I'm looping through the forms collection, to see if it's still there, but this uses quite some resources. I can't implement Sleep, because that will freeze up the other form.
    I need something like WaitForSingleObject, something that will wait without using much resources. It tried WFSO, using the hWnd of my form, but that doesn't quite seem to work.

    Anyone have any suggestions?
    Tom Cannaerts
    www.tom.be

  2. #2
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Try copying the form's hwnd to a local variable and have a doevents loop that uses the IsWindow API call:-

    VB Code:
    1. Declare Function IsWindow Lib "user32" Alias "IsWindow" (ByVal hwnd As Long) As Long
    2.  
    3. Public Sub WaitForForm(Byval fIn As Form)
    4.  
    5. Dim lHwnd As Long
    6.  
    7. lWhd = fIn.hwnd
    8.  
    9. fIn.Show
    10.  
    11. While IsWindow(lHwnd)
    12.    DoEvents
    13. Wend
    14.  
    15. End Sub

    You will need to unload the form rather than just hide it. If you just want to check that the form is visible replace isWindow with IsWindowVisible i.e.:
    VB Code:
    1. Declare Function IsWindowVisible Lib "user32" Alias "IsWindowVisible" (ByVal hwnd As Long) As Long

    Hope this helps,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  3. #3

    Thread Starter
    Junior Member Cakkie's Avatar
    Join Date
    May 2002
    Location
    Olen, Belgium
    Posts
    24
    Didn't know about these, but they still leave me with a loop. What I'm looking for is some sort of way to mimic the behaviour of showing a form modal, without the form having to be a true modal (note limitations I'm facing in first post). Something like this
    Code:
    frm.Show
    WaitUntilFormUnloads frm
    DoSomeOtherStuffHere
    Tom Cannaerts
    www.tom.be

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    VB Code:
    1. 'in some module:
    2. Global WaitForQuit As Boolean
    3.  
    4. 'in somewhere in the code
    5.  
    6. frm.Show
    7. Enabled = False
    8. Do Until WaitForQuit
    9. DoEvents
    10. Loop
    11. Enabled = True
    12. WaitForQuit = False
    13.  
    14. 'in the frm_Unload
    15. WaitForQuit = True


    This doesn't solve the child window problem...but I hope this is something useful, though I highly doubt it myself

  5. #5
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    The only way to avoid the loop, imo, is to install a WH_GETMESSAGE hook and discard any messages that are sent to a window that is not a child of the window you have designated as "modal". This will probably be fraught with difficulty, however...
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  6. #6

    Thread Starter
    Junior Member Cakkie's Avatar
    Join Date
    May 2002
    Location
    Olen, Belgium
    Posts
    24
    Oh well, in that case I guess I can start redesigning a bunch of forms so they implement events which I can catch on the calling form.
    Tom Cannaerts
    www.tom.be

  7. #7
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Sure - for example:

    VB Code:
    1. '\\ Form1
    2. Option Explicit
    3.  
    4. Private WithEvents F2 As Form2
    5.  
    6. Private Sub F2_Unloaded()
    7.  
    8. Me.Enabled = True
    9.  
    10. End Sub
    11.  
    12. Private Sub Form_Load()
    13.  
    14. Set F2 = New Form2
    15. F2.Show
    16.  
    17.  
    18. Me.Enabled = False
    19.  
    20. End Sub
    and have form2 raise an unloaded event:
    VB Code:
    1. '\\ Form2
    2. Option Explicit
    3.  
    4. Public Event Unloaded()
    5.  
    6. Private Sub Form_Activate()
    7.  
    8. Me.ZOrder vbBringToFront
    9.  
    10. End Sub
    11.  
    12. Private Sub Form_Unload(Cancel As Integer)
    13.  
    14. RaiseEvent Unloaded
    15.  
    16. End Sub

    HTH,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

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