您的位置:首页 > 其它

用简单的方法验证在TextBox中输入二进制数

2013-05-23 18:52 204 查看
private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.D0 && e.KeyCode != Keys.D1 && e.KeyCode != Keys.NumPad0 && e.KeyCode != Keys.NumPad1)//只允许输入0、1包括数字键盘处0、1值和退格键。无法保证输入中文,可以在转换成整形时捕获异常
{
e.SuppressKeyPress = true;//不符合要求不显示在TextBox1中
}
}
private void Button1_Click(object sender, EventArgs e)
{
try
{
UInt16 value= Convert.ToUInt16(SolenoidValveStatusTb.Text.ToString(), 2);
if (value > 65535 || value < 0)
{
MessageBox.Show("电磁阀状态范围为0~65535");
}

}
catch (Exception err)
{
MessageBox.Show(err.ToString(), "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
TextBox1.Clear();
}
finally
{
PC_USBComunication.Produce(o);
}
}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: