创建获取0或1字符串的文本框 [英] create textbox that gets 0 or 1strings
本文介绍了创建获取0或1字符串的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建仅获取0或1个字符串的文本框?
请帮帮我.
谢谢
How can I create a textbox that just gets 0 or 1 strings?
Please help me.
Thanks
推荐答案
你好,亲爱的
首先,必须将TextBox的MaxLength属性设置为1,这确定用户不能在TextBox中插入超过1个字符,然后在TextBox的KeyPress事件的事件处理程序中编写此代码
Hello Dear
first you must set MaxLength property of TextBox to 1 which determines user can not insert more than 1 character in TextBox then in event handler of KeyPress event of TextBox write this code
if (e.KeyChar == '0' || e.KeyChar == '1')
return;
else
e.Handled = true;
使用文本框的错误提示.使用两个单选按钮或一个组合框或仅包含两个值的列表框.选择(或在单选按钮的情况下进行检查)将表示0,第二个为1;否则为0.使用适当的标签或文本项向用户显示哪一个是什么.
—SA
Bad idea to use a text box for it. Use two radio buttons or a combo box or a list box with just two values. Selecting (or checking in case of radio button) will mean 0, second one 1; use appropriate label or text items to show to the user which one is what.
—SA
在构造函数中添加以下事件处理程序:
Add the following event-handler in the constructor:
textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
事件:
Event:
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar.Equals('0')) || (e.KeyChar.Equals('1')))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
这篇关于创建获取0或1字符串的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文