-
Feb 20th, 2013, 04:51 PM
#1
Thread Starter
Addicted Member
How to add Keydown under all circumstances
In order for me to avoid writing the
Code:
If e.keycode = keys.control + keys.B then me.show()
and writing that under ever keydown event. May I just have it under form.activated or how can it work whenever the form is activated on the screen. I know we show put it under the Form1.Activated but how will I do that. e.keycode isnt valid under that field then what is thanks so much D:
-
Feb 20th, 2013, 04:57 PM
#2
Re: How to add Keydown under all circumstances
Set the KeyPreview property of the Form to True, then you can handle just the Form keydown events and get any keydowns for any of the controls, as well. That may be all you are looking for.
My usual boring signature: Nothing
-
Feb 20th, 2013, 06:20 PM
#3
Re: How to add Keydown under all circumstances
The syntax is wrong as well, you should avoid + and use e.Control for the control key:
Code:
If e.Control AndAlso e.KeyCode = Keys.B Then
'Code
End If
-
Feb 20th, 2013, 06:52 PM
#4
Thread Starter
Addicted Member
Re: How to add Keydown under all circumstances
I don't believe the syntax was wrong I was merely asking how does a key event control all controls essentially like
Code:
Under the Textbox_keydown event
If e.KeyCode = Keys.Control + Keys.Z Then
TxtNavigate.Undo()
End If
' There is no problem with that code..
-
Feb 20th, 2013, 08:02 PM
#5
Re: How to add Keydown under all circumstances
That code will perform, yes. However, I think you should use the method I showed above. As for the key event being fired every time, as Shaggy suggested you would set form's keypreview to true. You can set it in design time under the form's properties or in code like this:
Code:
Me.KeyPreview = True
that would more likely go in the Form's load event. Then in the form's keydown event you would call the:
Code:
If e.Control AndAlso e.KeyCode = Keys.Z Then
End If
furthermore, if you wanted the selected textbox that's on the form to undo, then you would iterate through all of the textbox's in Me.Controls and check if the textbox is focused or not. The end result should look something like this:
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.Control AndAlso e.KeyCode = Keys.Z Then
For Each txtbox As TextBox In Me.Controls.OfType(Of TextBox)()
If txtbox.Focused Then
txtbox.Undo()
End If
Next
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
End Class
-
Feb 21st, 2013, 11:15 AM
#6
Re: How to add Keydown under all circumstances
The + is a risky operator. How it works depends on a few factors. If you have Option Strict ON, as you should, the behavior should always be predictable, but if you have Option Strict OFF, the behavior becomes situational. Aside from that AndAlso is going to have better performance in all circumstances, regardless of whether + works as you want it to. The reason for this is that AndAlso allows the code to short-circuit the whole test. + will concatenate things, which is not fast to begin with, but both pieces have to be evaluated. The way dday wrote it, if e.Control is false, it doesn't even bother evaluating the rest of the condition, since the truth of the whole condition is already known.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|