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

Creating a UserControl

P: n/a
Hello!

I'm reading in a book and here they says.
"Now it is time to begin thinking about which events the control should
provide. Because the control is
derived from userControl class, it has inherited a lot of functionality that
you don't need to worry about.
There are, however, a number of events that you don't want to hand to the
user in the standard way.
Examples of this include the KeyDown, KeyPress and KeyUp events. The reason
you need to change
these events is that users will expect them to be sent when they press a key
in the textbox.
As they are now, the events are only sent when the control itself has focus
and the user presses a key.
To change this behavior, you must handle the events sent by the text box,
and pass them on to the user.
Add the KeDown, KeyUp, and KeyPress events for the text box and enter the
following code."

Now to my question what do they mean with saying this in the text above
"There are, however, a number of events that you don't want to hand to the
user in the standard way.
Examples of this include the KeyDown, KeyPress and KeyUp events. The reason
you need to change
these events is that users will expect them to be sent when they press a key
in the textbox.
As they are now, the events are only sent when the control itself has focus
and the user presses a key.
To change this behavior, you must handle the events sent by the text box,
and pass them on to the user. "

Can somebody explain that

private void txtLabelText_KeyDown(object sender, KeyEventArgs e)
{
OnKeyDown(e);
}

private void txtLabelText_KeyUp(object sender, KeyEventArgs e)
{
OnKeyUp(e);
}

private void txtLabelText_KeyPress(object sender, KeyPressEventArgs e)
{
OnKeyPress(e);
}

//Tony
Jul 5 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sat, 05 Jul 2008 07:10:55 -0700, Tony Johansson
<jo*****************@telia.comwrote:
[...]
As they are now, the events are only sent when the control itself has
focus
and the user presses a key.
To change this behavior, you must handle the events sent by the text box,
and pass them on to the user. "

Can somebody explain that
With the usual caveat that since we don't have the book, we have very
little context with which to answer questions about the book...

I don't understand why the book writes "user". Everywhere I see the word
"user" is almost seems like the book meant to write "UserControl". Users
are the people who are pushing the keys on the keyboard; how would
keyboard input be sent to them?

As for the "why" of the technique being suggested: the issue is that the
UserControl containing a TextBox (for example) may want to override the
behavior of particular key presses that would otherwise be consumed by the
TextBox.

The code example you posted doesn't make a lot of sense to me though,
because it allows both the UserControl and the TextBox to process the same
keyboard input. This is almost never what you actually want. Typically,
you'll have some concept of precedence for keyboard input. For input that
the TextBox should get first, you don't need to do anything; it'll see and
consume that input first.

For input that the containing UserControl should get first, a different
way to deal with that is to override the ProcessKeyPreview() in
UserControl; this allows the UserControl to see keyboard input before the
TextBox actually makes a decision to process or not. By returning "true"
from ProcessKeyPreview(), the UserControl can consume the keyboard input
first, taking precedence. Of course, you'd only want to return "true" for
input that the UserControl really wants to take precedence for. :)

Pete
Jul 5 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.