天天看點

文本框隻允許輸入字母及數字,限制輸入漢字

 1、限制輸入漢字

       private void txtSum_Leave(object sender, EventArgs e)    //如果在textchance事件中,會循環彈出提示對話框,最好在此事件中判斷

        {

            string s="";

            foreach (char c in txtSum.Text)

            {

                int i = (int)c;

                if (i > 0x4E00 && i < 0x9fa5)

                {

                    s = "不允許輸入漢字";  

                }         

            }

            if (s!="")

            {

                txtSum.Text = "";

               MessageBox.Show("商品數量不允許輸入漢字", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

2、隻允許輸入字母及數字

 private void txtSum_KeyPress(object sender, KeyPressEventArgs e)

        {

            if ((e.KeyChar != 8 && !char.IsLetterOrDigit(e.KeyChar))&&e.KeyChar!=13)    //應該允許backspace(8)和回車(13)

            {

                MessageBox.Show("商品數量隻能輸入數字","操作提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

                e.Handled = true;

            }

        }

3、