How can I prevent a control from getting a particular keystroke

Platform: WinForms| Category: Keyboard Handling

You can handle the control’s KeyPress event and indicate the key has been handled. Below is code that prevents a TextBox from getting an ’A’ and the return key.

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
	if(e.KeyChar == (char)13 || e.KeyChar == ’A’)
		e.Handled = true;
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.