Close the current form in the CUSTOM handler and return a value ?

Started by Jean-pierre Leroy, November 24, 2010, 10:48:55 AM

Previous topic - Next topic

Jean-pierre Leroy

Hi Paul,

In my application I open a second FORM called SEARCHAPP; in this second form I have a ListView called APPS; the end user has to select a row in the ListView and then click on the ok button to validate his choice.

The end user would like to validate his choice by hitting the Enter Key; it is the reason why I use the CUSTOM handler to test %VK_RETURN; at this moment the current FORM SEARCHAPP should be closed automatically and the RowID returned to the main application.

At first I use this code ... but the FF_CloseForm gpf the application :


'--------------------------------------------------------------------------------
Function SEARCHANAPP_APPS_CUSTOM ( _
                                 ControlIndex  As Long,  _  ' index in Control Array
                                 hWndForm      As Dword, _  ' handle of Form
                                 hWndControl   As Dword, _  ' handle of Control
                                 wMsg          As Long,  _  ' type of message
                                 wParam        As Dword, _  ' first message parameter
                                 lParam        As Long   _  ' second message parameter
                                 ) As Long                                 
   Select Case wMsg

      Case %WM_GETDLGCODE
         Function = %DLGC_WANTALLKEYS   
         Exit Function
     
      Case %WM_CHAR
         If wParam = %VK_RETURN Then         
         
            Local lIndex As Long
            Local lRowId As Long                                     
                                           
            ' to get the current selection
            lIndex = FF_ListView_GetSelectedItem (hWndControl)
                                                   
            ' if there is no line selected
            If lIndex = -1 Then
           
                ' warning
                MessageBox(hWndControl, "There is no application selected" & $CrLf, APP.ProductName, %MB_OK Or %MB_DEFBUTTON1 Or %MB_ICONWARNING Or %MB_APPLMODAL)
       
            Else
           
                ' to get the the RowID associated to the current Index
                lRowID = FF_ListView_GetItemlParam(hWndControl, lIndex, 0)
               
                FF_CloseForm (hWndForm, lRowID)
               
            End If             

         End If

   End Select
   
End Function 


I replace the FF_CloseForm (hWndForm, lRowID) by these lines of codes:


                ' Set the value that will be returned to the calling program.
                App.ReturnValue = lRowID
               
                ' close the current form
                PostMessage (hWndForm, %WM_CLOSE, 0, 0)     


It seems to work fine; is-it safe to do that ? is-there any other way ?

Thanks for your help.
Jean-Pierre

Paul Squires

Not sure.... let me look at this one more closely tomorrow when I get home (finally!)
Paul Squires
PlanetSquires Software

Jean-pierre Leroy

Paul,

I will wait for your final answer to know if it is safe or not to use this workaround.

Thanks,
Jean-Pierre

Paul Squires

Hi Jean-Pierre,

The reason for the GPF is because the Form is ending midway through the execution of the %WM_CHAR message. Using the PostMessage allows the WM_CHAR message to fully complete before the Form destroys.

In your case, the only problem that I could see using your new approach is that your popup second Form may not reset the proper Form once is closes. FF_CloseForm has code to reset to the correct Form that was used to popup the second form (the SEARCHAPP form in your case).

Maybe creating a user defined message and posting to that message to initiate the FF_CloseForm may be even better?


%MSG_USER_CLOSEFORM = %WM_USER + 1

PostMessage hWndForm, %MSG_USER_CLOSEFORM, 0, 0

    %MSG_USER_CLOSEFORM
       ' to get the the RowID associated to the current Index                                 
      lRowID = FF_ListView_GetItemlParam(hWndControl, lIndex, 0)                               
      FF_CloseForm (hWndForm, lRowID)

Paul Squires
PlanetSquires Software

Jean-pierre Leroy

Paul,

Thank you for your help; it works perfectly.

Regards,
Jean-Pierre