Multiple instances of a non modal form

Started by Richard Kelly, December 03, 2010, 05:12:47 AM

Previous topic - Next topic

Richard Kelly

Let's say I have a non modal form where I can have several instances of it open at the same time. I usually use the FF HWND... names to reference controls on the form. If I have multiple copies of the same form open, is the FF HWND... name represent the active version, and if not, how to manage all the controls on the form?

Paul Squires

Hi Richard,

Excellent question, and one that I am pretty sure that I touch on in the Help file with regard to MDI child forms. When dealing with multiple instances of the same Form, you can not rely on the HWND variable to return the correct handle. You need to use the GetDlgItem api to get the correct handle. All that api does is search for the control's handle based on the Form handle and the Control's Id.


Global ghWndForm1 As Dword
Global ghWndForm2 As Dword

...
...
...

   Local hWndCurrentForm As Dword
   Local hWndCtrl        As Dword
   
   ' Save the Form handles to global variables
   ghWndForm1 = Form2_Show( hWndForm, %FALSE )
   ghWndForm2 = Form2_Show( hWndForm, %FALSE )
   
   ' Get the handle of the Command Button on the first instance of Form2
   hWndCtrl = GetDlgItem( ghWndForm1, IDC_FORM2_COMMAND1 )
   
   ? Str$(hWndCtrl)
   

   ' ...Or use a function like GetActiveWindow() to get the current active Form.
   hWndCurrentForm = GetActiveWindow()
   hWndCtrl        = GetDlgItem( hWndCurrentForm, IDC_FORM2_COMMAND1 )
   
   ? Str$(hWndCtrl)
   
   
   ' Lastly, you can depend on the hWndForm and/or hWndControl parameters to
   ' any message handler function to accurately reflect the correct handle for
   ' that instance of the Form.
   '   Function FORM2_COMMAND1_BN_CLICKED ( _
   '                                      ControlIndex     As Long,  _  ' index in Control Array
   '                                      hWndForm         As Dword, _  ' handle of Form
   '                                      hWndControl      As Dword, _  ' handle of Control
   '                                      idButtonControl  As Long   _  ' identifier of button
   '                                      ) As Long

Paul Squires
PlanetSquires Software

Richard Kelly

#2
I did not think to read through any of the MDI documentation;-(

In each forms CREATE function is the hWndForm provided the one to use in locating form controls?

If I want to have only one copy of a non modal form, does


Function FormActive (ByVal hWndForm As Dword) As Long

Local nReturn           As Long

   nReturn = %FALSE

   If IsWindow( hWndForm ) Then
   
      nReturn = %TRUE
     
      ShowWindow (hWndForm, %SW_RESTORE)
     
      PostMessage (hWndForm, %WM_SETFOCUS, 0, 0)
         
   End If

   Function = nReturn

End Function


do the trick?

Paul Squires

Quote from: Richard Kelly on December 04, 2010, 06:45:17 AM
In each forms CREATE function is the hWndForm provided the one to use in locating form controls?
Yes, you can use that hWndForm. It would be unique for each Form that is created.

Quote
If I want to have only one copy of a non modal form, does.... do the trick?

Yes, it looks okay. Not sure that you need the PostMessage/WM_SETFOCUS because WM_SETFOCUS normally sets the keyboard focus and a Form itself does not have keyboard focus (you would WM_SETFOCUS to a TextBox, Command Button, etc..).
Paul Squires
PlanetSquires Software