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.
|