How to catch event KeyDown,KeyUp and KeyPress

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    How to catch event KeyDown,KeyUp and KeyPress

    Hello!

    I have created a Control that consist of a label and a textbox.I have called
    this class ctlLabelTextbox .
    public partial class ctlLabelTextbox : UserControl
    {
    ....
    }
    The class that I have created for this purpose is derived from class
    UserControl.

    I can then drag this control from the toolbox into the form. This works
    good.

    Now to my question I want to be able to fetch KeyDown, KeyUp and KeyPress
    in the form so when somebody write something in my created ctlLabelTextbox
    textbox
    an event handler should be called to process this.

    This is what I have done.
    Drag my created control from the toolbox into the form.
    Focus the control and list the all the event by using properies for this
    control and here I have the three event that I'm looking for
    KeyDown,KeyPres s and KeyUp.
    I have double click for each of these so these three eventhandler was
    created.
    private void ctlLabelTextbox 1_KeyDown(objec t sender, KeyEventArgs e)
    {
    Console.WriteLi ne("In ctlLabelTextbox 1_KeyDown");
    }

    private void ctlLabelTextbox 1_KeyPress(obje ct sender, KeyPressEventAr gs e)
    {
    Console.WriteLi ne("In ctlLabelTextbox 1_KeyDown");
    }

    private void ctlLabelTextbox 1_KeyUp(object sender, KeyEventArgs e)
    {
    Console.WriteLi ne("In ctlLabelTextbox 1_KeyDown");
    }


    I have set a breakpoint in these eventhandler and write something in the
    ctlLabelTextbox textbox
    but there is no call to any of these event handler.

    So can somebody tell me why is not these handler called?
    What is it that I must add because something is missing?


    //Tony



  • Pavel Minaev

    #2
    Re: How to catch event KeyDown,KeyUp and KeyPress

    Tony Johansson wrote:
    Hello!
    >
    I have created a Control that consist of a label and a textbox.I have called
    this class ctlLabelTextbox .
    public partial class ctlLabelTextbox : UserControl
    {
    ...
    }
    The class that I have created for this purpose is derived from class
    UserControl.
    >
    I can then drag this control from the toolbox into the form. This works
    good.
    >
    Now to my question I want to be able to fetch KeyDown, KeyUp and KeyPress
    in the form so when somebody write something in my created ctlLabelTextbox
    textbox
    an event handler should be called to process this.
    >
    This is what I have done.
    Drag my created control from the toolbox into the form.
    Focus the control and list the all the event by using properies for this
    control and here I have the three event that I'm looking for
    KeyDown,KeyPres s and KeyUp.
    I have double click for each of these so these three eventhandler was
    created.
    private void ctlLabelTextbox 1_KeyDown(objec t sender, KeyEventArgs e)
    {
    Console.WriteLi ne("In ctlLabelTextbox 1_KeyDown");
    }
    >
    private void ctlLabelTextbox 1_KeyPress(obje ct sender, KeyPressEventAr gs e)
    {
    Console.WriteLi ne("In ctlLabelTextbox 1_KeyDown");
    }
    >
    private void ctlLabelTextbox 1_KeyUp(object sender, KeyEventArgs e)
    {
    Console.WriteLi ne("In ctlLabelTextbox 1_KeyDown");
    }
    >
    >
    I have set a breakpoint in these eventhandler and write something in the
    ctlLabelTextbox textbox
    but there is no call to any of these event handler.
    >
    So can somebody tell me why is not these handler called?
    What is it that I must add because something is missing?
    I'm not sure what you want here. Do you want to detect keypress events
    only on the textbox within your user control, on both textbox and
    checkbox, or on the control itself?

    If you want the form to be able to subscribe to events on the textbox,
    then you'll have expose them via events on your control, and propagate
    them. Like this:

    public partial class ctlLabelTextbox : UserControl
    {
    ...

    public event KeyPressEventHa ndler TextBoxKeyPress ;
    public event KeyEventHandler TextBoxKeyDown;
    public event KeyEventHandler TextBoxKeyUp;

    public ctlLabelTextbox ()
    {
    // Propagate KeyPress
    textBox.KeyPres s +=
    delegate(object sender, KeyPressEventAr gs e)
    {
    if (TextBoxKeyPres s != null) TextBoxKeyPress (this, e);
    }
    // Propagate KeyDown
    ...
    // Propagate KeyUp
    ...
    }
    }

    Then form can subscribe to TextBoxKeyPress and other events of your
    user control. If you want to catch events on the checkbox, you can do
    the same for it, too.


    On the other hand, if you want to receive key events on the
    UserControl itself, then you have to deal with keyboard focus. A
    control only receives keyboard event when it is focused, and
    UserControl is not focusable by default. To make it focusable, in your
    class which extends UserControl, in the constructor, call:

    this.SetStyle(C ontrolStyles.Se lectable, true);

    Then it can be focused (by tabbing to it, or clicking on it), and it
    will receive keyboard events as long as it is.

    Comment

    • raylopez99

      #3
      Re: How to catch event KeyDown,KeyUp and KeyPress

      On Jul 6, 2:58 am, "Tony Johansson" <johansson.ande rs...@telia.com >
      wrote:
      Hello!
      >
      I have created a Control that consist of a label and a textbox.I have called
      this class ctlLabelTextbox .
      public partial class ctlLabelTextbox : UserControl
      >
      So can somebody tell me why is not these handler called?
      What is it that I must add because something is missing?
      >
      //Tony
      OnChar Key Press event Key event keyup keydown key input KeyEventArgs
      won't work

      You must set the "KeyPreview " property of your Control to "true" when
      you construct it. You can do this from the "properties " tab of the
      Wizard, or, programmically.

      RL

      //must set KeyPreview "key preview" property of form to True!
      // this.KeyPreview = true;

      Comment

      Working...