By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,978 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,978 IT Pros & Developers. It's quick & easy.

How to catch event KeyDown,KeyUp and KeyPress

P: n/a
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,KeyPress and KeyUp.
I have double click for each of these so these three eventhandler was
created.
private void ctlLabelTextbox1_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("In ctlLabelTextbox1_KeyDown");
}

private void ctlLabelTextbox1_KeyPress(object sender, KeyPressEventArgs e)
{
Console.WriteLine("In ctlLabelTextbox1_KeyDown");
}

private void ctlLabelTextbox1_KeyUp(object sender, KeyEventArgs e)
{
Console.WriteLine("In ctlLabelTextbox1_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

Jul 6 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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,KeyPress and KeyUp.
I have double click for each of these so these three eventhandler was
created.
private void ctlLabelTextbox1_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("In ctlLabelTextbox1_KeyDown");
}

private void ctlLabelTextbox1_KeyPress(object sender, KeyPressEventArgs e)
{
Console.WriteLine("In ctlLabelTextbox1_KeyDown");
}

private void ctlLabelTextbox1_KeyUp(object sender, KeyEventArgs e)
{
Console.WriteLine("In ctlLabelTextbox1_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 KeyPressEventHandler TextBoxKeyPress;
public event KeyEventHandler TextBoxKeyDown;
public event KeyEventHandler TextBoxKeyUp;

public ctlLabelTextbox()
{
// Propagate KeyPress
textBox.KeyPress +=
delegate(object sender, KeyPressEventArgs e)
{
if (TextBoxKeyPress != 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(ControlStyles.Selectable, 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.
Jul 6 '08 #2

P: n/a
On Jul 6, 2:58*am, "Tony Johansson" <johansson.anders...@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;

Jul 29 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.