Created
May 10, 2011 03:07
-
-
Save ksummerlin/963841 to your computer and use it in GitHub Desktop.
Here is a LinqPad example of KeyPressed e.Handled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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