don't understand the Handle property in KeyPressEventArgs object

T

Tony Johansson

Below is an example of some code. What I don't understand is the Handle
propery for the KeyPressEventArgs object.
The docs says the following.
"If the event is not handled, it will be sent to the operating system for
default processing. Set Handled to true to cancel the KeyPress event."
I mean when I do this
tb.KeyPress += new KeyPressEventHandler(keypressed);
I say that I subscribe to the event.
So I just can't understand what the docs mean with the text If the event is
not handled, it will be sent to the operating system for default processing.
But I mean that I have already subscribe to the event by using
tb.KeyPress += new KeyPressEventHandler(keypressed);
so the event is consumed.

So I hope to get a explanation to this strange text from the docs



using System;
using System.Windows.Forms;

public class Form1: Form
{
public Form1()
{
// Create a TextBox control.
TextBox tb = new TextBox();
this.Controls.Add(tb);
tb.KeyPress += new KeyPressEventHandler(keypressed);
}

private void keypressed(Object o, KeyPressEventArgs e)
{
// The keypressed method uses the KeyChar property to check
// whether the ENTER key is pressed.

// If the ENTER key is pressed, the Handled property is set to true,
// to indicate the event is handled.
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
}
}

public static void Main()
{
Application.Run(new Form1());
}
}

//Tony
 
J

Jeff Johnson

"If the event is not handled, it will be sent to the operating system for
default processing. Set Handled to true to cancel the KeyPress event."

Perhaps if the Handled property had been called Consumed instead it might be
a little less confusing to some.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top