HOW TO INSTANTIANTE A MDI CHILD FORM FROM A MODAL DIALOG BOX, WITH CONTROL RETURNING TO DIALOG BOX WHEN MDI CHILD FORM IS CLOSED

(To view this document in Notepad, turn on Word Wrap)

When designing the user interface for my Windows applications, I try to allow the user to examine or edit any important piece of data from wherever in the software he may need to.  For example, in a MDI project, I have a common editing child form (grid format) that has several menu items.  I also have modal dialog boxes that initiate different types of simulations.  Some portions of my data are global items, ie all simulations use in one way or another.  On the other hand, some of the data is totally dedicated to each individual simulation.  All are edited with the common child form.

I had a design dilemma.  I wanted menu items to edit the global type data from the MDI parent menu, but I didn't really want each of the simulation-specific data editors also listed on the MDI parent menu.  Users would pull up a simulation dialog box to set certain control switches, forgetting to check the simulation-data, and have to cancel the dialog box, edit the sim-data from another menu item, and re-start the dialog box.

What I wanted to do was put a button on the dialog box that would pull up the editing form.  However, as most of you know, modal forms cannot instantiate MDI child forms.  

The code snips that follow show how you can emulate the desired behavior.  The routine that initiates the dialog box responds to which button the user selects.  It will start the child form and WILL WAIT until child form is unloaded, and will then re-show the dialog box.

I hope someone can use this technique in their own applications.

Thomas Rutledge
72223,1637


############################
Sub UseDialog1 ()
'## This routine brings up the dialog box and handles instantiation of the child form ##
'Dialog1 is the modal dialog box
'ChildForm is the desired child form

Dim RoutineDoneFlag%
'RoutineDoneFlag% -   	False - initial starting of form & returning from child form
'			True - OK button selected 

    'instantiate dialog box as modal
    Dim g As New Dialog1
    
    Do while  Not RoutineDoneFlag%
        g.Show modal		'show modal dialog box.  Dialog box will return which button was selected in its Tag property
	'dialog box hides itself with Me.Hide instead of unloading itself to return control to this routine
        
        Select Case g.Tag
          Case "2"      'cancel button
             'unload dialog1 form and get out
	    Unload g
            Exit Sub
          Case "3"      'child form button
            'instantiate child form and wait for it to unload
            Dim p As New ChildForm
            p.Show

	'with an MDI project, after ChildForm is loaded, Forms.Count=3 (MDI parent form, g Dialog box, and p ChildForm) 
	
            Do While Forms.Count > 2    'wait until p ChildForm is unloaded, which reduces Form.Count back to 2 (MDI parent form, g Dialog box (remember, Dialog box is hidden, not unloaded))
                DoEvents
            Loop

            REM SPECIAL CASE!! - check for quitting entire program by closing MDI parent while childForm has focus - otherwise, MDI parent form will close, but dialog box will come back!!

            If Forms.Count = 1 Then
                'MDI parent form has been unloaded - unload dialog box and get out of routine
                RoutineDoneFlag% = True
                Unload g
            End If

          Case "1"      'OK
            'can run simulation
            RoutineDoneFlag% = True
        End Select

    Loop

 'whatever action required by selecting OK -remember, cancel button Exits Sub before getting here
'retrieve any needed switch settings from dialog box

'unload dialog1 form	
Unload g

 Call DoCalc()       

End Sub 


#### CODE IN Dialog1.FRM ####

Sub CmdCancel_Click ()
    Me.Tag = "2"    'flag for cancel button
    Me.Hide
End Sub

Sub CmdOK_Click ()
    Me.Tag = "1"        'flag for OK button
    Me.Hide
End Sub

Sub CmdGetChildForm_Click ()
    Me.Tag = "3"
    Me.Hide
End Sub


