none
Key Down Event in UserControl RRS feed

  • Question

  • Hey

    I use VB.NET 2010´
    I have created a user control which holds a few textboxes, where the user can find articles.
    This user control is added to several Forms (where ever the search is needed).

    I have to handle the key down event of the user control, but it isn't working...

    The usercontrols's Key_Down Event is never fired. - I have set KeyPreview of the form to true, then the Key_Down of the Form is fired.
    How to do that?

    • Moved by Thorsten Dörfler Sunday, December 19, 2010 2:41 PM Language (From:Visual Basic (ab Version 2002 / .NET))
    Sunday, December 19, 2010 10:08 AM

Answers

  • I believe you should be handling the KeyDown event of all the child controls (textboxes) within the UC and then internally call the protected method OnKeyDown on the UC itself from with those handlers.  So essentially you are bubbling up the event to the parent control.  Remember that the form has no clue of the existence of the UCs child controls so that's why this is necessary.

    You may be able to just set KeyPreviewDown on the UC itself to achieve the same effect...  Unfortunately I can't test it right now to tell you for sure.

    • Proposed as answer by Kee PoppyModerator Friday, December 24, 2010 9:12 AM
    • Marked as answer by Zero-G. _ Sunday, December 26, 2010 10:08 AM
    Sunday, December 19, 2010 2:55 PM

All replies

  • I believe you should be handling the KeyDown event of all the child controls (textboxes) within the UC and then internally call the protected method OnKeyDown on the UC itself from with those handlers.  So essentially you are bubbling up the event to the parent control.  Remember that the form has no clue of the existence of the UCs child controls so that's why this is necessary.

    You may be able to just set KeyPreviewDown on the UC itself to achieve the same effect...  Unfortunately I can't test it right now to tell you for sure.

    • Proposed as answer by Kee PoppyModerator Friday, December 24, 2010 9:12 AM
    • Marked as answer by Zero-G. _ Sunday, December 26, 2010 10:08 AM
    Sunday, December 19, 2010 2:55 PM
  •  

    Once a control that can get the focus has been added as a child to a UserControl, the userControl only act as a container and dont take the keyboard focus anymore.

    And without the keyboard focus, the Keys events dont fire.

     

    Sunday, December 19, 2010 7:08 PM
  • Hi Zero-G,

    Have you solved this issue?

    If you have any further questions about this, please let me know.

     

    Best Regards,


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 24, 2010 9:12 AM
    Moderator
  • Did you ask yourself what you want to solve.

    In my opinion you can get key down events for all your textboxes and for situations outside that, but you tell only you want to handle the Key down a little bit like saying, I want to type, but not telling what you want to type.

    The answers on those are in both the same as your question, you need a keyboard for that, but further your question is to vague to help you.

     

     


    Merry Christmas
    Cor
    Friday, December 24, 2010 1:42 PM
  • Hey

     

    Thanks for this explaination! - Ths sounds logical for me. I didn't wan't to do it that way, because I thought, that this would be redundant code.

    Thanks! - I will do it this way and handle all texboxes.

    Nice hollidays!

    Sunday, December 26, 2010 10:07 AM

  • Hey

     

    Thanks for this explaination! - Ths sounds logical for me. I didn't wan't to do it that way , because I thought, that this would be redundant code.

    Thanks! - I will do it this way and handle all texboxes.

    Nice hollidays!


    Hi Zero-G. _,

    You could declare a PUBLIC event in the code for the UserControl, RAISE the event that you declared

    within the code for the UserControl and then handle the event in your Form code if you wish.

    I'm not sure if this is what from user Dig-Boy was suggesting but try the following steps;

    • From the PROJECT menu select Add User Control...
    • Add a TextBox from the ToolBox
    • Select VIEW CODE  from the view menu and PASTE this code for the UserControl.>>

     

     

     

    Public Class UserControl1
    
      Public Event MyKeyDownEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    
      Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    
        RaiseEvent MyKeyDownEvent(sender, e)
    
      End Sub
    End Class

    1. Select BUILD from the BUILD menu.
    2. Add a UserControl to your Form from the ToolBox
    3. If you PASTE this as your Form code you will see the USerControl KeyDown event show a MessageBox message "Hi there!! "  >>

    .

    Public Class Form1
    
      Private Sub UserControl11_MyKeyDownEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles UserControl11.MyKeyDownEvent
    
        MessageBox.Show("Hi there!!")
    
      End Sub
    
    End Class

     

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Sunday, December 26, 2010 12:25 PM