Ddanbe has given the correct answer. When the key is pressed if you dont want it, you can do something about it then.
That would be "a" correct answer, If in program you achieve your the desired effect, it is correct. there is no "right" way to program.
although that would be one of the best ways to go about it in this programmers opinion.
one thing to remember, if you desire to use the ever popular solution to simply ask the system, if a the key is a particular character, don't do anything. its usually necessary to subscribe to the "keypress" event. not the "keyup" or "keydown" events. I ran into that problem when i first got started, some one on a forum not unlike this one helped me out because i was using the "keyup" event, and it would fail to handle the event entirely. the code would execute, but the text still appeared in the text box.
Google is your friend.
here is an example of a custom text box control that only allows digits
public class NumbersOnlyTextBox : TextBox
{
public NumbersOnlyTextBox()
{
KeyPress += new KeyPressEventHandler( HandleKeyPress );
}
private void HandleKeyPress( object sender, KeyPressEventArgs e )
{
if ( !( char.IsDigit( e.KeyChar ) || char.IsControl( e.KeyChar ) ) )
e.Handled = true;
}
}
its not necessary to use a custom control, you could simply just do the same on your form directly.
<rant>
it can't be stressed enough people. I think it should be a rule.
Google before thread!
I say all forums should set up to check for a google cookie, if it isn't there, then the submit button on the new thread page automatically opens a new browser window with the search results in google of whatever was entered in the subject text box of the forum.
</rant>