Results 1 to 6 of 6

Thread: How to add Keydown under all circumstances

  1. #1

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Angry 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:

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    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..

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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
  •  



Click Here to Expand Forum to Full Width