c#文本框限制輸入內容


 
 
  //限制輸入不能為中文和全角
        private void zhbh_KeyPress( object sender, KeyPressEventArgs e)
        {
            int chfrom = Convert .ToInt32( "4e00" , 16);    //范圍(0x4e00~0x9fa5)轉換成int(chfrom~chend)
            int chend = Convert .ToInt32( "9fa5" , 16);
            if (e.KeyChar >= ( Char )chfrom && e.KeyChar <= ( Char )chend)
            {
                e.Handled = true ;
            }
            if (e.KeyChar >= ( Char )65281 & ( int )e.KeyChar <= ( Char )65374)
            {
                e.Handled = true ;
            }
        }
 
//*******以下方法需在文本框的KeyPress方法下調用
        //限制輸入只能為數字
        private void wz_qsh_KeyPress( object sender, KeyPressEventArgs e)
        {
            if (!( Char .IsNumber(e.KeyChar)) && e.KeyChar != ( Char )8)
            {
                e.Handled = true ;
            }
        }
  /**
         * 限制只能輸入數字和小數點
         * */
        public void validationOnlyFloat( KeyPressEventArgs e, TextBox t)
        {
            if ((( int )e.KeyChar < 48 || ( int )e.KeyChar > 57) && ( int )e.KeyChar != 8 && ( int )e.KeyChar != 46)
                e.Handled = true ;
            //小數點的處理。
            if (( int )e.KeyChar == 46)                           //小數點
            {
                if (t.Text.Length <= 0)
                    e.Handled = true ;   //小數點不能在第一位
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false , b2 = false ;
                    b1 = float .TryParse(t.Text, out oldf);
                    b2 = float .TryParse(t.Text + e.KeyChar.ToString(), out f);
                    if (b2 == false )
                    {
                        if (b1 == true )
                            e.Handled = true ;
                        else
                            e.Handled = false ;
                    }
                }
            }
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM