WPF Key is digit or number

前端 未结 7 682
感情败类
感情败类 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:33

    bit of a cludge but it works :)

     private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            Regex R = new Regex("^([A-Z]|[0-9]){1}$");
    
            var strKey = new KeyConverter().ConvertToString(e.Key);
            if(strKey.Length > 1 )
            {
                strKey = strKey.Replace("NumPad", "").Replace("D", "");
            }
            if (strKey.Length == 1)
            {
                if (!R.IsMatch(strKey))
                {
                    e.Handled = true;
                }
            }
            else
            {
                e.Handled = true;
            }
        }
    
    0 讨论(0)
提交回复
热议问题