开发者

Binary numbers only TextBox

I want to validate the input of a TextBox to be a binary number.

I know I can do this with RegEx but I wanted a more 'inmed开发者_开发知识库iate' validation, like allowing just 1's and 0's to be entered.

I thought of using MaskedTextBox but I don't know how to just allow those two characters.


Implement the KeyPress event. Set e.Handled = true if you don't like the key:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
        // Allow backspace, 0 and 1
        e.Handled = !("\b01".Contains(e.KeyChar));
    }


There's no way of getting it out of the box with MaskedTextBox.

This answer shows you a way of achieving this (just adapt the code to parse only 0s and 1s):

How to make the MaskedTextBox only accept HEX value?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜