473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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,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
2 19261
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lachlan Hunt | last post by:
Hi, I've been looking up lots of documentation and trying to work out a cross-platform method of capturing a keyboard event and working out which key was pressed. From what I can find, there...
8
by: William Ortenberg | last post by:
I'm trying to capture when a user presses a particular key (escape), so I'm coding in the KeyPress event. Yet the code seems be bypassed (breakpoint is never encountered). Should I be coding in a...
4
by: john sutor | last post by:
When moving up and down with the arrow keys on my DataGrid, I cannot get either the Keypress, KeyUp or KeyDown events to respond. I am putting breakpoints in to capture the event but they never...
2
by: ZS | last post by:
Hi, On a form , I'm trying to trap when a shift key is pressed. Can someone explain how the KeyUp,KeyDown and Key Press event works for Forms. Thanks -ZS
0
by: Gene Hubert | last post by:
I'm trying to catch the KeyUp event in textbox of a DataGrid. I'm picking up the keydown and keypress events ok, but not keyup. Can anyone see what is wrong with this code. I been fighting with...
3
by: Just Me | last post by:
I noticed that I don't get KeyDown nor KetPress events if NumLock is not on. I can probably find a workaround but it would be nice to know what the rules are. Can anyone tell me when KeyPress...
3
by: C-Services Holland b.v. | last post by:
I've got a problem with the order of events in a textbox. The order of events seems to occur like this when I press esc: tb1.leave tb1.validate tb1.lostfocus tb2.gotfocus tb1.keyup tb1 and...
3
by: Luc The Perverse | last post by:
Hey - I am making my first C# form application! (More a teach myself app than any pratical application) I have a listbox that always has the control so I made an event handler for KeyDown. I...
13
by: andypb123 | last post by:
Hello, The onchange event fires in IE6 in a SELECT element when scrolling through the list with the up and down arrows on the keyboard. In Firefox it only fires after you hit the enter key, which...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.