Stephen Mitchell
4/17/2018 - 4:58 PM

Here is a LinqPad example of KeyPressed e.Handled

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();
	}
}