How do I use C# to ignore a delete key being pressed in a windows program? Specific code examples, please! Thanks!
How do I ignore a delete key being pressed in C# .NET 2.0 program?
You didn't give enough info, really. Is it a Windows Forms app, or a console app?
I'm going to assume a Windows Forms app, and that the key presses you are want to ignore are when the user is typing into a Textbox control. Because, of course, all key presses are "ignored' by default by the app unless you hook up an event for the form to handle key presses.
Anyway, for the Windows Forms app with a textbox (textbox1), add code for the KeyDown event as follows:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
{
e.Handled = true;
e.SuppressKeyPress = true;
}
}
I've included a supression for both the Delete and Backspace keys, but you could do either or both. Whatever.
Key suppression for a .NET console application (for example) during user input is more involved. Read this article on the subject, if that is your aim:
http://www.devsource.com/article2/0,1895...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment