PC Review


Reply
Thread Tools Rate Thread

How to catch event KeyDown,KeyUp and KeyPress

 
 
Tony Johansson
Guest
Posts: n/a
 
      6th Jul 2008
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



 
Reply With Quote
 
 
 
 
Pavel Minaev
Guest
Posts: n/a
 
      6th Jul 2008
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.
 
Reply With Quote
 
raylopez99
Guest
Posts: n/a
 
      29th Jul 2008
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;

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Keypress,Keyup,keydown or similar events in Mydocument.cs jayrm100@yahoo.com Microsoft C# .NET 1 16th Aug 2005 08:10 PM
KeyDown / KeyUp / KeyPress Keith Smith Microsoft C# .NET 2 10th Feb 2005 07:20 PM
How to catch KeyUp or KeyPress event in a modal window (ShowDialog) bill Microsoft Dot NET Framework Forms 1 16th Oct 2004 04:23 PM
Keyup, KeyDown, and KeyPress events for a datagrid do not work. URGENT!!!! Chris Calhoun Microsoft C# .NET 2 10th Oct 2003 09:21 PM
KeyPress, KeyDown, KeyUp, SP2, Combobox Robert Lalouche Microsoft Dot NET Compact Framework 4 22nd Sep 2003 05:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:00 PM.