(1)新建一个windows应用程序,将其命名为HardlyEnableFigure
(2)在窗体中添加TextBox控件,用于输入要验证的数字
(3)主要代码:
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace HardlyEnableFigure
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((!char.IsDigit(e.KeyChar) && e.KeyChar != 8) && e.KeyChar != 13)
{
//弹出信息提示
MessageBox.Show("商品数量只能是数字", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Handled = true;//表示已经处理过keypress事件
}
}
}
}
//输入为负号时,只能输入一次且只能输入一次
if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true;
//允许输入负号
if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;
//允许输入小数点