Creating a UserControl

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    #1

    Creating a UserControl

    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_Ke yDown(object sender, KeyEventArgs e)
    {
    OnKeyDown(e);
    }

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

    private void txtLabelText_Ke yPress(object sender, KeyPressEventAr gs e)
    {
    OnKeyPress(e);
    }

    //Tony


  • Peter Duniho

    #2
    Re: Creating a UserControl

    On Sat, 05 Jul 2008 07:10:55 -0700, Tony Johansson
    <johansson.ande rsson@telia.com wrote:
    [...]
    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 "UserContro l". 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 ProcessKeyPrevi ew() 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 ProcessKeyPrevi ew(), 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

    Comment

    Working...