我建立一个textbox控件,准备用来接收用户输入的年份值,4位数字,该怎么做才能阻挡用户输入其他非数字字符.c#语言描述

解决方案 »

  1.   

    使用验证控件或用JavaScript判断
      

  2.   

    //using System.Runtime.InteropServices;
    [DllImport("User32", CharSet=CharSet.Auto)] private
    static extern int SetWindowLong(IntPtr hWnd, int Index, int Value);
    [DllImport("user32", EntryPoint="GetWindowLong")] public
    static extern int GetWindowLongA(IntPtr hwnd, int nIndex);
    const int GWL_STYLE = -16;
    const int ES_NUMBER = 0x2000; private void Form1_Load(object sender, System.EventArgs e)
    {
    SetWindowLong(this.textBox1.Handle,GWL_STYLE,
    GetWindowLongA(this.textBox1.Handle,GWL_STYLE) | ES_NUMBER);
    }
      

  3.   

    用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"四位的你自己改改就行了
      

  4.   

    string regex = @"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578]
    )|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[4
    69])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\
    s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([1
    3579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((
    0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((
    0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9]
    )|(2[0-8]))))))"; 这个是日期的正则
    你把月日去掉就可以了不知道你为什么不用下拉框
    这样省去好多麻烦
      

  5.   

    能不能用Keypress 事件来处理
      

  6.   

    在killfocus函数中:
    try
    {
         textbox.Text转化为整型
    }
    catch
    {
         MessageBox.Show("请输入数字");
         textbox.Text = "";
         textbox.focusd = true;
    }我怕麻烦,都是这么做的^_^,虽然不严谨,但还是很好用的
      

  7.   

    设置TextBox1.Numeric = true,即可限制只能输入数字。
      

  8.   

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;class Test : Form
    {
      const int GWL_STYLE = -16;
      const int ES_NUMBER = 0x2000;  [DllImport("User32", CharSet=CharSet.Auto)]
      static extern int SetWindowLong(IntPtr hWnd, int Index, int Value);
      [DllImport("user32", EntryPoint="GetWindowLong")]
      static extern int GetWindowLongA(IntPtr hwnd, int Index);  Test()
      {
        TextBox tbx   = new TextBox();
        tbx.Parent    = this;
        tbx.MaxLength = 4;
        SetWindowLong(tbx.Handle, GWL_STYLE, GetWindowLongA(tbx.Handle,GWL_STYLE) | ES_NUMBER);
      }  static void Main()
      {
        Application.Run(new Test());
      }
    }
      

  9.   

    有个比较变态的方法,引用VB.NET 的dll。就可以用IsNumeric函数了
      

  10.   

    对不起,错了,设置TextBox1.Numeric = true只能用于移动控件。
      

  11.   

    没必要怎么麻烦吧!
    直接在keypress事件里添加个判断就行了!
      

  12.   

    你可以用VS自带的验证控件来进行验证或者,你就像楼上各位说的那样用DropDownList控件绑定年份,这样就不会输入其它的值了!
    就用验证控件吧。怪容易实现的。。
      

  13.   

    private void txtPrice_KeyPress(object sender,System.Windows.Forms.KeyPressEventArgs e)
    {
    if (char.IsNumber(e.KeyChar)==false) 
    {
    if (e.KeyChar!=(char)46 && e.KeyChar!=(char)8) 
    e.Handled =true; 
    }
    }
    方法2
    //处理消息
    //
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
    int WM_CHAR = 0x0102;
    if(m.Msg == WM_CHAR)
    {
    if(((char)m.WParam >= '0') && ((char)m.WParam <= '9') ||
    (int)m.WParam == (int)Keys.Back || (int)m.WParam == (int)Keys.Delete)
    {
    base.WndProc (ref m);
    }
    }
    else
    {
    base.WndProc (ref m);
    }
    }
    //文本默认为空
    protected override void OnCreateControl()
    {
    base.OnCreateControl ();
    this.Clear();
    }
      

  14.   

    /// <summary>
    /// 判断字符串中含有指定的字符
    /// </summary>
    /// <param name="text">指定的字符串</param>
    /// <param name="findChar">指定的字符</param>
    /// <returns></returns>
    public static bool ContainChar(string text, char findChar)
    {
    return text.IndexOf(findChar) > -1;
    }
    /// <summary>
    /// 判断当前输入的字符是否是合法数字字符
    /// </summary>
    /// <param name="text">已经输入的数字内容</param>
    /// <param name="selectStart">光标在编辑控件中的起始位置</param>
    /// <param name="isSelectAll">判断当前编辑控件内容是否全选</param>
    /// <param name="e">按键时激活的事件类</param>
    public static void HandleNumKeyPress(string text, int selectStart, bool isSelectAll, KeyPressEventArgs e)
    {
    if (char.IsNumber(e.KeyChar)==false) 
    {
    if ((!isSelectAll && (ContainChar(text, (char)46)) || e.KeyChar!=(char)46) 
    && ((!isSelectAll && text != "" && selectStart != 0) || e.KeyChar!=(char)45) 
    &&   e.KeyChar!=(char)8)
    {
    e.Handled =true; 
    }
    }
    }
    然后在keypress事件中挂接PrjFundCommon.HandleNumKeyPress(((TextEdit)sender).Text,((TextEdit)sender).SelectionStart, ((TextEdit)sender).Text==((TextEdit)sender).SelectedText, e);
      

  15.   

    我这个是WEb页面上的textbox  客户端可以验证吗 
      

  16.   

    使用JS比较好,直接客户端验证。
    function NumberOnly()
    {
    /*
    Examples:
    <input type=text onkeydown=NumberOnly()>
    */
    if (!((event.keyCode>=48 && event.keyCode<=57) || (event.keyCode>=96 && event.keyCode<=105) || event.keyCode==8 || event.keyCode==9 || event.keyCode==37 || event.keyCode==39 || event.keyCode==46))
    event.returnValue=false
    if(event.shiftKey && (event.keyCode>=48 && event.keyCode<=57)) event.returnValue=false
    }
      

  17.   

    这个正则表达式匹配四位十进制数:
    "^\d{4}$"
    在C#里这样用:
    Regex r = new Regex(@"^\d{4}$");
    if (r.IsMatch(textBox1.Text))
      ...