怎样在文本框中只能输入汉字?

解决方案 »

  1.   

    下面的代码是判断数字(是汉字和数字时,把判断条件改一下就可以。)
    代码来自http://bingning.net/VB/SOURCE/control/numerictextbox.htmlpublic class MyTextBox : TextBox
     {
         const int WM_PASTE = 0x302;     protected override void WndProc(ref Message m)
         {
             if (m.Msg == WM_PASTE)
             {
                 IDataObject iData = Clipboard.GetDataObject();
                 //检测剪贴板是否有字符串
                 if (iData.GetDataPresent(DataFormats.Text))
                 {
                     string clipStr = (string) iData.GetData(DataFormats.Text);
                     //检测剪贴板的字符串是文字还是数字
                     if (!System.Text.RegularExpressions.Regex.IsMatch(
                         clipStr,
                         @"^\d+$"))
                         return;
                 }
             }
             base.WndProc(ref m);
         }
     }
      

  2.   

     /// <summary>
            /// 判断是否是中文字符
            /// </summary>
            /// <param name="poChar">传入的字符</param>
            /// <returns></returns>
            public static bool IsChinese(char poChar)
            {
                if ((poChar > '\u4e00') && (poChar < '\u9fa5'))
                {
                    return true;
                }
                return false;
            }
      

  3.   

      /[^\u4E00-\u9FA5]/g  这个
      

  4.   

    正解:将文本框的ImeMode属性设置为Disabled
      

  5.   

     if (Regex.IsMatch(Temp,@"[\u4e00-\u9fa5]"))
                    {
                        TextBox1.Text+=Temp;
                    }
      

  6.   

    public class MyTextBox : TextBox
     {
         const int WM_PASTE = 0x302;     protected override void WndProc(ref Message m)
         {
             if (m.Msg == WM_PASTE)
             {
                 IDataObject iData = Clipboard.GetDataObject();
                 //检测剪贴板是否有字符串
                 if (iData.GetDataPresent(DataFormats.Text))
                 {
                     string clipStr = (string) iData.GetData(DataFormats.Text);
                     //检测剪贴板的字符串是文字还是数字
                     if (!System.Text.RegularExpressions.Regex.IsMatch(
                         clipStr,
                         @"^[\u4e00-\u9fa5]*$"))   //改了  ^[\u4e00-\u9fa5]*$
                         return;
                 }
             }
             base.WndProc(ref m);
         }
     }
      

  7.   

    将文本框的ImeMode属性设置为Disabled 
    这个只是屏蔽你不能输入汉字,我的目的是只允许输入汉字!
    如果用正则表达式的话,可以, 
      if(m.Msg == WM_PASTE) 这是什么意思啊?各位大哥,还有没有简单一点的方法
      

  8.   

    我想提醒一句,一般用的判定中文unicode范围只是个大致,因为这个范围包含了其它东亚国家字体,如果说只要区分出中英文,用上面说的正则表达^[\u4e00-\u9fa5]*即可。
      

  9.   

            public static bool IsChinese(char poChar) 
            { 
                 //判断是否是中文的字符
                if ((poChar > '\u4e00') && (poChar < '\u9fa5')) 
                { 
                    return true; 
                } 
                return false; 
            }