|
-
Nov 18th, 2002, 03:39 AM
#1
Thread Starter
Junior Member
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?
-
Nov 18th, 2002, 03:57 AM
#2
Frenzied Member
Try copying the form's hwnd to a local variable and have a doevents loop that uses the IsWindow API call:-
VB Code:
Declare Function IsWindow Lib "user32" Alias "IsWindow" (ByVal hwnd As Long) As Long
Public Sub WaitForForm(Byval fIn As Form)
Dim lHwnd As Long
lWhd = fIn.hwnd
fIn.Show
While IsWindow(lHwnd)
DoEvents
Wend
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:
Declare Function IsWindowVisible Lib "user32" Alias "IsWindowVisible" (ByVal hwnd As Long) As Long
Hope this helps,
Duncan
-
Nov 18th, 2002, 04:21 AM
#3
Thread Starter
Junior Member
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
-
Nov 18th, 2002, 04:27 AM
#4
VB Code:
'in some module:
Global WaitForQuit As Boolean
'in somewhere in the code
frm.Show
Enabled = False
Do Until WaitForQuit
DoEvents
Loop
Enabled = True
WaitForQuit = False
'in the frm_Unload
WaitForQuit = True
This doesn't solve the child window problem...but I hope this is something useful, though I highly doubt it myself
-
Nov 18th, 2002, 04:51 AM
#5
Frenzied Member
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...
-
Nov 18th, 2002, 05:00 AM
#6
Thread Starter
Junior Member
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.
-
Nov 18th, 2002, 05:11 AM
#7
Frenzied Member
Sure - for example:
VB Code:
'\\ Form1
Option Explicit
Private WithEvents F2 As Form2
Private Sub F2_Unloaded()
Me.Enabled = True
End Sub
Private Sub Form_Load()
Set F2 = New Form2
F2.Show
Me.Enabled = False
End Sub
and have form2 raise an unloaded event:
VB Code:
'\\ Form2
Option Explicit
Public Event Unloaded()
Private Sub Form_Activate()
Me.ZOrder vbBringToFront
End Sub
Private Sub Form_Unload(Cancel As Integer)
RaiseEvent Unloaded
End Sub
HTH,
Duncan
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
|