7

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 control?

Alexry
  • 507
  • 2
  • 6
  • 10
  • I would be interested to know your answer to Sean's question, as I frankly can't think of a use case my self. – Shravan May 21 '10 at 13:26
  • I presumed he was asking the user for a binary string, eg. 1001001 – Pretzel May 21 '10 at 13:28
  • 01010111 01101000 01100001 01110100 00100000 01101001 01100110 00100000 01111001 01101111 01110101 00100000 01110111 01100001 01101110 01110100 00100000 01110100 01101111 00100000 01100100 01101111 00100000 01110100 01101000 01101001 01110011 00111111 – Inisheer May 21 '10 at 13:29
  • "What if you want to do this?" - Well that'd be a perfect case. – djdd87 May 21 '10 at 13:30
  • I don't know about GenericTypeTea, but I used LeetKey -- a lovely add-on for Firefox. :-) – Pretzel May 21 '10 at 13:33
  • 01010111 01101000 01100101 01100101 00100001 00100000 01001111 01101110 01100101 01110011 00100000 01100001 01101110 01100100 00100000 01011010 01100101 01110010 01101111 01110011 00100000 01100111 01100001 01101100 01101111 01110010 01100101 00100001 00100000 00111010 00101101 00101001 – Pretzel May 21 '10 at 13:35

3 Answers3

12

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
  • 7
    Better this: `e.Handled = (e.KeyChar == Keys.D0) || (e.KeyChar == Keys.D1);` – abatishchev May 21 '10 at 13:29
  • @abatishchev -- haha, clever. :-D – Pretzel May 21 '10 at 13:30
  • 3
    @abatischev, Don't forget the numpad. :) – Filip Ekberg May 21 '10 at 13:30
  • You should also take care of backspace and delete – RvdK May 21 '10 at 13:35
  • arrow keys? tab? what if they want to paste using Ctrl+V? – adam0101 May 21 '10 at 13:48
  • @adam0101: Try before ask ;) `KeyPress` will not be fired on pressing delete, arrows or tab. Yes, Ctrl+C/Ctrl+V will bot work but Cntrl+Ins/Shift+Ins will. – abatishchev May 21 '10 at 14:02
  • @adam0101, refactored a bit. You can now use Delete key, Backspace, 0 , 1 numpad 0 and 1. – Filip Ekberg May 21 '10 at 14:07
  • Right-click the text box and select Paste. – Hans Passant May 21 '10 at 14:16
  • 1
    @Hans, Actually one solution would be to use `protected override void WndProc(ref Message m)` and check for paste/copy messages. But then you would have to override the TextBox and do that. Another solution is to override the TextChanged event and just add a loop that checks each character and removes the incorrect ones. To make the example a bit simple, I wont add that though, there's enough information in this last comment. :) – Filip Ekberg May 21 '10 at 14:44
2

If you do want to show the numbers rather than a check or something but only want to allow 0 or 1 you could use a NumericUpDown control and set min to 0 max to 1 and step to 1.

If you do actually need a textbox I'd use Filip's answer but I'd set the MaxLength of it to 1 to avoid having to worry about 00, 11 or 01 or similar values.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • +1 -- I personally think that this is the best answer, really. And if you want it as a string, well then use .ToString() -- And you're done. (and so little code, too!) – Pretzel May 21 '10 at 13:38
1

You may want to see the answer to this related question:

C# Input validation for a Textbox: float

Community
  • 1
  • 1
Arnaud Leymet
  • 5,995
  • 4
  • 35
  • 51