How can I limit the TextBox control to only allow the values 0 and 1?
Thanks. And I have one more question: How can I disable put text from clipboard in my textbox c
By using the event KeyPress
private void NumericOnlyKeyBox_KeyPress(object sender, KeyPressEventArgs e)
{
var validKeys = new[] { Keys.Back, Keys.D0, Keys.D1 };
e.Handled = !validKeys.Contains((Keys)e.KeyChar);
}
Setting e.Handled
to true / false indicates if the character should be accepted to the box or not.
You can read more about KeyPressEventArgs on MSDN.
Note
Keys.Delete should cover Keys.Delete, Keys.Backspace and other "Back" buttons.