Problem Closing Form

Started by RhodyRich, August 13, 2009, 01:38:07 PM

Previous topic - Next topic

RhodyRich

I have a form with controls including a "Cancel" button that works fine. In the CREATE procedure for the form I am doing some data checking. In a few rare cases there is a problem with the data and I need to prematurely exit the form and return to the main calling form for the application. If there is a data problem I want to exit the form with

FF_CLOSEFORM hWndForm

This is the same statement used when the "Cancel" button is clicked which fine.

What happens is that the form closes and the main calling form is visible. However nothing on the main form is active. Menus are inactive. The close box is inactive. The only thing that I can do is to use the Windows Task Manager to shut down the application.

What do I need to do to exit this form cleanly?

TechSupport

You can PostMessage in your WM_CREATE message handler to fire the Cancel button. This may cause the Form to "flash" on the screen. I can not find a reliable way to prevent that flashing because FireFly shows the Form based on design time settings so even hiding the Form during WM_CREATE will not help.


PostMessage hWndForm, %WM_COMMAND, MakLng(IDC_FORM2_CMDCANCEL, 0), HWND_FORM2_CMDCANCEL


A better approach would be to move your date processing code out of the WM_CREATE and process it before making the decision to show the Form2 in the first place. That way you beforehand whether or not to show the Form.


TechSupport

As an aside, in FireFly 3 all you would have to do is return -1 from the WM_CREATE message handler to prevent the Form from continuing to load.

Function = -1


TechSupport

Here is a solution to closing the Form during the WM_CREATE for FireFly 2. Instead of using FF_CloseForm, use this code in your WM_CREATE:


   Local ff As FLY_DATA Ptr
   ff = GetProp(hWndForm, "FLY_PTR")
   
   EnableWindow @ff.hWndParent, %TRUE
   SetActiveWindow @ff.hWndParent
   DestroyWindow hWndForm


RhodyRich

I tried the solution in your last message but it does not work for me. I get an error message that says

LOCAL ff AS FLY_DATA PTR

has an undefined type.

I developed a work around where I set an error variable due to the error, display an error message and remedy for the user, and then close the form when any control on the form is activated by the user. Not as elegant but it works.

TechSupport

ooops, my bad. In FF2 the variable is named FF_DATA rather than FLY_DATA.


RhodyRich

Okay but now I get "Invalid member/name definition" for the second line below.

    ff = GETPROP(HWND_WEEKLYRESULTSFORM, "FLY_PTR")
    ENABLEWINDOW @ff.HWND_MAINFORM, %TRUE
    SETACTIVEWINDOW @HWND_MAINFORM
    DESTROYWINDOW HWND_WEEKLYRESULTSFORM

TechSupport

Sorry. I made the sample program in FF3. I didn't realize that I had made so many changes to the code generation.

Try this:

EnableWindow @ff.ModalParent, %TRUE
SetActiveWindow @ff.ModalParent
DestroyWindow hWndForm

RhodyRich

The form closed with that change but the result is the same as with FF_CLOSEFORM. The main screen comes up but I cannot click on anything and I have to use Task Manager to shut it down. I guess I need to use my work around and wait for Firefly 3.

Roger Garstang

#9
Quote from: TechSupport on August 13, 2009, 03:09:58 PM
As an aside, in FireFly 3 all you would have to do is return -1 from the WM_CREATE message handler to prevent the Form from continuing to load.

Function = -1

Cool, any chance on getting a special handler for QueryEndSession too?


For the original question...perhaps a hybrid of what Paul suggested with the first 2/3 of the last, but instead of destroy window, Post a WM_Close [PostMessage(hWndForm, %WM_CLOSE, 0, 0)] or the cancel message above...whatever works.

Is there special code that fires in the Cancel button, or does it just close the form?  And, is it set to be the Cancel button where pressing Esc also fires it?

Roger Garstang

Reason I ask if it is set to the cancel button which assigns it %ID_CANCEL is that it behaves weird at times.  I use it a lot in my apps, and had an issue once where every time I closed my window it would beep.  Turns out it was because my Cancel button was disabled.  It was something mentioned in like one line of a buried Microsoft documentation.  I don't know all the details, but somehow it is linked to closing.  And, that it beeped to me would indicate that on Close it fires...especially on Dialog based Windows in DDT and FireFly even has kind of a hybrid that uses Dialog functionality.  Just to be safe you may try changing it to not be cancel and see what happens.  Depending on the code in there it could be doing something that won't work while the window closes...or maybe it closing causes the window to get closed twice...one on the close which then perhaps fires ID_CANCEL which again tries to close, etc.