Application.AllowQuit Property

cjohnson

Well-known member
Joined
Sep 21, 2006
Messages
63
Location
WV
Programming Experience
1-3
Can anyone tell me how to stop the user from exiting a program. I found the Application.AllowQuit property, but it is read-only. Is there a way to set this?
Thanks,
Chris
 
I thought maybe I should clarify what I need. It is not just that I don't want the user to be able to close a form, I don't want the user to be able to leave the form (deactivate). This program will run on a public computer, and the user should not have access to any computer functions other than the program. Closing the program must require a password. Any ideas?
Thanks,
Chris
 
you can stop the form closing by setting e.Cancel on the form_onClosing event.

as for the not allowing switches to other apps, maybe intercepting the windows messages and not passing them on might work.
 
Thank you very much. I tried the e.Cancel and it worked great. Can you tell me how to intercept windows messages?
Thanks,
Chris
 
You'll need to get a bit more low level for this. The most common way to read windows messages in VB.Net is to override the Forms WndProc sub..

VB.NET:
Protected Overrides Sub WndProc(ByRef m as Message)
 
end Sub

Ok, so in here is where every message that is passed to your forms message loop is processed, the Handle property is the window that it is ment for.

VB.NET:
m.Hwnd

and then there is

VB.NET:
m.Message

That is the message that is being sen to the window identified by the m.hwnd property. Confused yet? :) by doing a 'If' statement on the m parameter to can determine what message is being sent to that particular window.


VB.NET:
If m.Message = 15 then
'this was a wm_paint message
end if

So to determine if your user has switched application you will be looking for a WM_ACTIVATEAPP message and then check the wparam for a value of zero

VB.NET:
If m.Message = WM_ACTIVATEAPP AndAlso m.wparam = 0 then
'The user has deactivated your application
end if

A list of all the constants such as WM_ACTIVATEAPP and others are available on a program call API viewer. Do a google search and you'll find it. If you need any more help post back.
 
Thanks for the help. I tried, but ran into a problem. I used the code

VB.NET:
Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_ACTIVATEAPP AndAlso m.WParam = 0 Then
            MsgBox("Test")
        End If
End Sub

and when I build, I get the error
Win32Exception was unhandled.
Error creating window handle.

Any idea?

Thanks,
Chris
 
Gotcha. Now it runs, and when I click off the form, I get a messagebox. Since I have to pass it on, is there a way I can change the message so the form stays active or a vb function I can call so that when this message is detected (the form has been deactivated) then the form will activate so the user never really leaves the form? I have tried Me.Activate and Me.Focus, but no effect.
Thanks for all your help.
Chris
 
Back
Top