Skip to content

Instantly share code, notes, and snippets.

@ksummerlin
Created May 10, 2011 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ksummerlin/963841 to your computer and use it in GitHub Desktop.
Save ksummerlin/963841 to your computer and use it in GitHub Desktop.
Here is a LinqPad example of KeyPressed e.Handled
//You'll have to run this example in LinqPad as a C# program.
//Or put a class around the two global methods and change .Dump() calls to Console.WriteLine
void Main()
{
var frm = new Form1();
frm.Controls[0].KeyPress += keypressed_inMain;
frm.ShowDialog();
}
//React to the Enter keypress
private void keypressed_inMain(Object o, KeyPressEventArgs e)
{
if (e.Handled) {
"Not Handled by Main".Dump();
return;
}
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
}
"Handled by Main".Dump();
}
// Define other methods and classes here
public class Form1: Form
{
public Form1()
{
// Create a TextBox control.
TextBox tb = new TextBox();
this.Controls.Add(tb);
tb.KeyPress += new KeyPressEventHandler(keypressed);
}
//react to the Enter keypress
private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.Handled) {
"Not Handled by Form".Dump();
return;
}
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
}
"Handled by Form".Dump();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment