WPF Key is digit or number

前端 未结 7 685
感情败类
感情败类 2021-01-17 09:36

I have previewKeyDown method in my window, and I\'d like to know that pressed key is only A-Z letter or 1-0 number (without anyF1..12,

7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-17 10:15

    try this, it works.

        private void txbNumber_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key >= Key.D0 && e.Key <= Key.D9) ; // it`s number
            else if (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) ; // it`s number
            else if (e.Key == Key.Escape || e.Key == Key.Tab || e.Key == Key.CapsLock || e.Key == Key.LeftShift || e.Key == Key.LeftCtrl ||
                e.Key == Key.LWin || e.Key == Key.LeftAlt || e.Key == Key.RightAlt || e.Key == Key.RightCtrl || e.Key == Key.RightShift ||
                e.Key == Key.Left || e.Key == Key.Up || e.Key == Key.Down || e.Key == Key.Right || e.Key == Key.Return || e.Key == Key.Delete ||
                e.Key == Key.System) ; // it`s a system key (add other key here if you want to allow)
            else
                e.Handled = true; // the key will sappressed
        }
    

提交回复
热议问题