.Net UserControl Keypress event

Hi,

i created a .net usercontrol using VB 2005. I see that keypress event is not fired in the UserControl when any key is pressed. However the keypress event of the textbox in the UserControl alone gets fired.

In VB 6, this worked perfectly. Keypress events for forms are enabled using keypreview property.

I don't see any such property for a usercontrol.

Can some help, how to enable keypress event of a UserControl.

Any help in this regard is appreciated.

Thanks,
Sugan




Answer this question

.Net UserControl Keypress event

  • KarlH

    Sorry. I'm a newbie and don't understand this code.

    Could you explain what this code does and where this code needs to be inserted.

    Also i want to keypress events of both the UserControl and the textbox in the UserControl to be enabled.

    Thanks for spending time on this.

    Once againThanks,

    Sugan



  • Smurble

    protected override bool ProcessKeyPreview(ref Message m)

    {

    //Do custom processing here

    return base.ProcessKeyPreview(ref m);

    //You can use the parameters in m to process keys

    }


  • desperate108146

    Write this bit of code in the user control and then drag the control to the form.Put in a 
    debug statement in the line of code in the control and when you press a key on the user control
    the debug statement will be hit.
    Protected Overrides Function ProcessKeyPreview(ByRef m As Message) As Boolean
    //Process the message to look for the key which was pressed.You will find lots of
    //example by googling
       Return MyBase.ProcessKeyPreview(m)
    End Function
    

  • .Net UserControl Keypress event