Do anyone know how to close a form automatically using a timer? I am trying to close a form after a set amount of seconds (say 30secs.). The form has a "Close" command button that can also be used to close the form sooner if I wish.
Send it a WM_CLOSE to the window handle on the Timer Event. Might want to PostMessage it too just to be sure it is out of the message loop when sent. I always run into issues when things are closed from inside a loop and the loop ends up running after the window is closed, so the process is still going in TaskManager. Probably want to kill the timer and other cleanup stuff before posting the message too.
I tried that, and it closes the window ok. The problem is, I would still like to be able to close the window manually using a command button. Let's say the window is open/displayed, the 30secs. has not yet elapsed, but I want to close the window manually, for what ever reason. How do I code the timmer loop to do this, or do I put the code somewhere else? For some strange reason the "Close" command button does not work. It appears to be disabled.
Try the following code as a guideline... Let me know if it doesn't work for you.
Global gTimer As Long
'---------------------------------------------------
Function FORM1_CMDTIMER_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
gTimer = SetTimer( %Null, 0, 30000, CodePtr(TimerProc))
End Function
'---------------------------------------------------
Function FORM1_WM_DESTROY ( _
hWndForm As Dword _ ' handle of Form
) As Long
' Kill the timer in case it is still running
If gTimer Then KillTimer 0, gTimer
End Function
'---------------------------------------------------
Function TimerProc( ByVal hWnd As Dword, _
ByVal uMsg As Long, _
ByVal idEvent As Long, _
ByVal dwTime As Long _
) As Long
' timer has fired... close the main Form.
FF_CloseForm HWND_FORM1, 0
End Function
'---------------------------------------------------
Function FORM1_CMDCLOSE_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
' Close the Form.
FF_CloseForm HWND_FORM1
End Function
Thanks Paul
The code works!
Instead of starting the timer at the click of a button, I placed the code in the WM_Create message, so the timer starts when the form is created.
It works just fine. Thanks for your prompt response.
Andy Flowers
Specialty Software Solutions & Design
Nassau, Bahamas