C# WINFORM中限制文本框輸入


 

限制只能輸入數字:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

{

            if ((e.KeyChar >= '0' && e.KeyChar <= '9')  || (byte)(e.KeyChar) == 8)//8就是回格,backspace(刪除).
            {
            }
            else
            {
                e.Handled = true;
            }

}

限制只能輸入數字和一個小數點:

要限制輸入的textbox的Keypress事件:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || (byte)(e.KeyChar) == 8)//8就是回格,backspace(刪除).
            {
                if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') >= 0)
                {
                    e.Handled = true;
                }
            }
            else
            {
                e.Handled = true;
            }
        }


免責聲明!

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



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