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
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.
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
Related
I have downloaded a dll for a custom base Winforms as my base form, the developer of the custom form is no longer available and all of my forms inherits on this base form as it has some functionalities common to all of my other forms, my problem here is that the event when I press the ESC key exits or closes the active form immediately without a warning, I wanted to override the keydown event of the ESC key but it does not work. I tried this code below but they are not working, When I press ESC the form closes since the ESC Event which closes the form is embedded in the custom base form.
Private Sub frmMain_KeyPress(sender As Object, e As KeyPressEventArgs)
If e.KeyChar = Chr(27) Then
e.Handled = True
End If
End Sub
Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Escape Then
e.Handled = True
End If
End Sub
try to use overrides event,
Overriding Event Handlers with Visual Basic .NET
It seems that your base form has the property KeyPreview set to True. You need to set it to false, probably the best place will be at the end of your constructor.
'frmMain.vb
Public Sub New()
InitializeComponent()
MyBase.KeyPreview = False
End Sub
More information on KeyPreview property here
KeyDown event does not work, pressing escape the form does not close
Private Sub DataTable_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Escape Then
Me.Close()
End If
End Sub
Well, sure it works, the event is just not very like to fire. Keystrokes raise the KeyDown event on the control with the focus. That will only ever be your form when it has no controls that can get the focus. A fairly unlikely scenario.
If you already have a Button labeled "Cancel" that closes the form then set the form's CancelButton property.
If you don't have such a button then it gets to be pretty unlikely that the user will figure out by himself that the Escape key is useful. He will most likely use the Close button in the upper right corner. You can nevertheless make it work by overriding the ProcessCmdKey() method. Like this:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Escape Then
Me.Close()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
I have been trying to get this to work but it's doing my head in, wondered if you experts can help me out.
On my form I would like to set focus to a TextBox when I press F1 on the keyboard, I have the code written but somehow it does not work when I press F1. What am I doing wrong? I have also set keypreview to true.
The code here:
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyData = Keys.F1 Then
txtemployeeno.Focus()
End If
End Sub
The problem is that your KeyUp event isn't firing because the form doesn't technically have input focus (though it may be activated). If you wish to use the KeyPreview property, you need to use the KeyPress event instead of KeyUp.
Alternatively, you could always override the ProcessCmdKey function. Just add the following method to your form's code:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.F1 Then
txtemployeeno.Focus()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
How can I tell if the Tab key has been pressed inside a certain textbox.
I tried enabling the AcceptsTab property to true and creating a function that handles texstboxname.KeyPress event, but when I tab while inside it it just tabs out of the box, instead of hitting my event. (normal keys fire my event, but tab never even fires it)
The Multiline property needs to be true as well.
From MSDN (emphasis in bold)
Gets or sets a value indicating whether pressing the TAB key in a multiline text box control types a TAB character in the control instead of moving the focus to the next control in the tab order.
so set the Multiline property to true.
As MiniTech also pointed out, KeyDown is easier to handle since it provides you with an e.KeyCode property whereas the KeyPress event only provides an e.KeyChar property.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
'user pressed the Tab key...
End If
End Sub
It seems that you can either Additionally
Enable the multiline property on the TextBox
You can override IsInputKey before the Keydown or Keypress event first.
To do that you could derive a class from TextBox and ovverride it there, ( as Hans Passat suggested)
or you can also handle the PreviewKeyDown event and ovverride the IsInputKey to true in there.
I went with the second part of option two, so that I wouldn't have to worry about the multiline property being on and could Leave my code with the keypress event
Private Sub txtEntryBar_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles txtEntryBar.PreviewKeyDown
If e.KeyData = Keys.Tab Then
e.IsInputKey = True
End If
End Sub
Ref: http://www.vbforums.com/showthread.php?670904-Detecting-when-the-tab-key-is-pressed-in-a-textbox&p=4124911&viewfull=1#post4124911
I have a form with several controls. I want to run a specific sub on keydown event regardless any controls event.
I mean if user press Ctrl+S anywhere on form it execute a subroutine.
You should set the KeyPreview property on the form to True and handle the keydown event there
When this property is set to true, the form will receive all KeyPress,
KeyDown, and KeyUp events. After the form's event handlers have
completed processing the keystroke, the keystroke is then assigned to
the control with focus. .......... To handle keyboard
events only at the form level and not allow controls to receive
keyboard events, set the KeyPressEventArgs.Handled property in your
form's KeyPress event handler to true.
So, for example, to handle the Control+S key combination you could write this event handler for the form KeyDown event.
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
If e.Control AndAlso e.KeyCode = Keys.S then
' Call your sub method here .....
YourSubToCall()
' then prevent the key to reach the current control
e.Handled = False
End If
End Sub
I've used this code in my forms before and it seems to work pretty good.
Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) As Boolean
If m.Msg = &H100 Then 'WM_KEYDOWN
Dim key As Keys = m.WParam
If key = Keys.S And My.Computer.Keyboard.CtrlKeyDown Then
'DO stuff
Return True
End If
End If
Return MyBase.ProcessKeyPreview(m)
End Function