新文章

2010年8月14日 星期六

[C#]讓TextBox避免輸入英文(一)

通常設計一個Win form 裡面的控制項中,如TextBox,我們要限制使用者輸入數字以外的字母,可以使用key_press事件處理。



點擊TextBox控制項,右手邊事件欄裡找到KeyPress,點擊兩下,輸入如下程式碼即可。

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
        {
            e.Handled = true;
            MessageBox.Show("Error!Please input the right number!");
        }
    }

沒有留言:

張貼留言