-1

My application is parent, child application. Child forms shows then press cntrl + F4 the child form is closed. How to block the action and the same time if i press cntrl + F4 the child form have submit button that event is invoked.

How can i do that?

I am using below coding is block the control + F4

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        If keyData = Keys.Control Or Keys.F4 Then Return True
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
Sathish
  • 4,419
  • 4
  • 30
  • 59

2 Answers2

1

This event already exists, the FormClosing event fires. You can cancel the close by setting e.Cancel = True in your event handler for the event. Be sure to check the e.CloseReason before you do that.

Do avoid breaking standard Windows shortcut keystrokes, there is no point. The user can also close the window by clicking the child window's Close button. Ctrl+F4 is just a helpful shortcut to do the same thing without using the mouse.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

You have to catch the form closing event, then test if it's done by key-press.

I assume you mean ALT-F4?

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
          If My.Computer.Keyboard.AltKeyDown Then e.Cancel = True
    End Sub

Or even shorter;

    e.Cancel=My.Computer.Keyboard.AltKeyDown
Dennis
  • 1,528
  • 2
  • 16
  • 31
  • No, he means Ctrl+F4, a standard shortcut keystroke for MDI child windows. – Hans Passant Aug 19 '13 at 12:56
  • It irks me to use the current keyboard state to make decisions about an event. As one argument against this, I suspect that if a user hits Ctrl+F4 on a machine experiencing severe performance problems, the message might queued way before the form closing event, causing `My.Computer.Keyboard.CtrlKeyDown` not to match the state at the time the user tried to close the form. – Brian Aug 20 '13 at 20:10