Disable ALT+F4 key in a form

.net , csharp Sri Lanka
  • 13 years ago
    i want to disable ALT+F4 in a form that me can't close the page

    can anyone send me source code todo it in C#.net?????



  • 13 years ago

    Below code is for VB6.  You may upgrade the code to .NET too.

    Put the below function in a module level.

    Public Function RemoveXButton(frmWindow as Form)

    dim hMenu as Long

    dim nCount as Long

    hMenu=GetSystemMenu(frmWindow.Hwnd,0)

    nCount=GetMenuItemCount(hMenu)

    'Remove the last two menu items which are a seperator and Alt+F4

    Call RemoveMenu(hMenu ,nCount-1, MF_REMOVE OR MF_BYPOSITION)

    Call RemoveMenu(hMenu, nCount-1, MF_REMOVE OR MF_BYPOSITION)

    DrawMenuBar frmWindow.hWnd

    End Function

     

    'Declarations

    Public Declare Function GetSystemMenu Lib "User32" (byval hwnd as Long, byval bRevert as Long) as Long

    ...

    Public Const MF_BYPOSITION = &H400&

    Public Const MF_REMOVE = &H1000&

    Similar to GetSystemMenu, search over internet and get declarations for GetMenuItemCount, RemoveMenu and DrawMenuBar.

    Thanks and Good luck

  • 13 years ago

    Perhaps an easier way is to just stop the form from closing.

    ALT-F4 is a request to close, rather than a force-close.

     

    Check out the .Closing Event of the form. It has a parameter which is read/write.

    The help says 'The Closing event occurs as the form is being closed.... If you cancel this event, the form remains opened. To cancel the closure of a form, set the Cancel property of the CancelEventArgs passed to your event handler to true.'

    If this means the form does not close when you want it to, try having a member property like    m_bYesIReallyMeanIt  which you can set and check...

     

     

  • 13 years ago

    Thnx 4 ur help.......

  • 12 years ago

    OK, you're idea is good that we should express clearly, but some times you should search by yourself, for what you don't know and get some knowledge some where else.

    Anyway, about 'm_bYesIReallyMeanIt' : I think it's about this : when you cancel closing a form by setting 'Cancel' member of 'FormClosingEventArgs' parameter of the form's 'FormClosing' event, you may want [for example] when user press 'Alt + F4' don't close that, but if he pressed the close button of your form, close it. Then you should distinguish these two situations, then you can declare 'm_bYesIReallyMeanIt' bool variable as default value of 'false' and when the user pressed 'Alt+F4' set that to 'true', then in 'FormClosing' event, you can assign 'm_bYesIReallyMeanIt' to 'e.Cancel' . Then we assumed 'm_bYesIReallyMeanIt' means : I really want to not close the form. I don't know what I said though!! Confused

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Every language has an optimization operator. In C++ that operator is ‘//’”