请教高手,如何使文本框中只能输入半角的数字,包括小数,输入全角则自动转换成半角,输入其它则弹出提示框警告。谢谢帮忙啊!

解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.Text;namespace NoteHandleBLL
    {
        public class ConvertDBCAndSBC
        {        /// <summary>半角转成全角   
            /// 半角空格32,全角空格12288   
            /// 其他字符半角33~126,其他字符全角65281~65374,相差65248   
            /// </summary>   
            /// <param name="input"></param>   
            /// <returns></returns>   
            public string DBCToSBC(string input)
            {
                char[] cc = input.ToCharArray();
                for (int i = 0; i < cc.Length; i++)
                {
                    if (cc[i] == 32)
                    {
                        // 表示空格   
                        cc[i] = (char)12288;
                        continue;
                    }
                    if (cc[i] < 127 && cc[i] > 32)
                    {
                        cc[i] = (char)(cc[i] + 65248);
                    }
                }
                return new string(cc);
            }        /// <summary>全角转半角   
            /// 半角空格32,全角空格12288   
            /// 其他字符半角33~126,其他字符全角65281~65374,相差65248   
            /// </summary>   
            /// <param name="input"></param>   
            /// <returns></returns>   
            public string SBCToDBC(string input)
            {
                char[] cc = input.ToCharArray();
                for (int i = 0; i < cc.Length; i++)
                {
                    if (cc[i] == 12288)
                    {
                        // 表示空格   
                        cc[i] = (char)32;
                        continue;
                    }
                    if (cc[i] > 65280 && cc[i] < 65375)
                    {
                        cc[i] = (char)(cc[i] - 65248);
                    }            }
                return new string(cc);
            }
        }
    }
      

  2.   

    全角转半角
    public string NarrowToSmall(string inputString)

        char[] c = inputString.ToCharArray(); 
          for (int i = 0; i < c.Length; i++)
          { 
                byte[] b = System.Text.Encoding.Unicode.GetBytes(c,i,1); 
                 if (b.Length == 2) 
                { 
                      if (b[1] == 255)
                     { 
                            b[0] = (byte)(b[0] + 32); 
                            b[1] = 0; 
                              c[i] = System.Text.Encoding.Unicode.GetChars(b)[0]; 
                        } 
                 } 
            } 
            for(int i=0;i<c.Length;i++)
            {
                if(47<c[i]<58||c[i]==46)
                else
                {
                    MessageBox.Show("有非数字");
                    return null;
                }
            }
            string returnString = new string(c); 
             return returnString;   // 返回半角字符 
    }
      

  3.   

    加这个属性 ime-mode: disabled; 
    或者正则判断的时候 不要用 \d 用 [0-9]就只匹配半角的数字了
      

  4.   

    private void txt_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar != (char)Keys.Back && !char.IsDigit(e.KeyChar) || Regex.IsMatch(e.KeyChar.ToString(), "[.]"))
                {
                    e.Handled = true;
                }
                //byte[] array = System.Text.Encoding.Default.GetBytes(e.KeyChar.ToString());
                //if (!char.IsDigit(e.KeyChar) || array.LongLength == 2) e.Handled = true;
                //if (e.KeyChar == '\b' || e.KeyChar == '.') e.Handled = false;         }
      

  5.   

    已测……        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                byte[] array = System.Text.Encoding.Default.GetBytes(e.KeyChar.ToString());
                if (!char.IsDigit(e.KeyChar) || array.LongLength == 2) e.Handled = true;
                if (e.KeyChar == '\b' || e.KeyChar == '.') e.Handled = false; 
            }   
      

  6.   

    请教6楼的,修改属性ime-mode: disabled后,就不能输入小数点了,请教要怎么输入小数啊?
      

  7.   

    谢谢4楼的,但运行时提示错误,Operator '<' cannot be applied to operands of type 'bool' and 'int',该怎么改啊,谢谢帮忙啦 for(int i=0;i<c.Length;i++)
            {
                if(47<c[i]<58||c[i]==46)
                else
                {
                    MessageBox.Show("有非数字");
                    return null;
                }
            }
      

  8.   

    private void txt_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar != (char)Keys.Back && !char.IsDigit(e.KeyChar) || Regex.IsMatch(e.KeyChar.ToString(), "[.]"))
                {
                    e.Handled = true;
                }
                //byte[] array = System.Text.Encoding.Default.GetBytes(e.KeyChar.ToString());
                //if (!char.IsDigit(e.KeyChar) || array.LongLength == 2) e.Handled = true;
                //if (e.KeyChar == '\b' || e.KeyChar == '.') e.Handled = false;         }
    这个可以,输入时候限制的
      

  9.   

    文本框添加一个KeyPress事件
    把下面的代码拷进去
    if (e.KeyChar != (char)Keys.Back && !char.IsDigit(e.KeyChar) || Regex.IsMatch(e.KeyChar.ToString(), "[.]"))
                {
                    e.Handled = true;
                }