Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I prevent a control from getting a particular keystroke?

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 Enter key.

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

Contributed from George Shepherd's Windows Forms FAQ



Page view counter