比较输入值的ASCII以检查它是字符还是数字 [英] Compare ASCII of Input values to check whether it is Character or Numeric

查看:71
本文介绍了比较输入值的ASCII以检查它是字符还是数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查用户是否在 DataGridViewTextBoxCell 中输入了字符或数字.如何根据DataGridView的KeyPress事件中输入的键的ASCII值执行此比较.
.
有任何建议吗?

I need to check whether user entered a character or numeric digit in DataGridViewTextBoxCell. How to perform this comparison on the basis of ASCII values of entered key in KeyPress Event of DataGridView.
Any Suggestion ?

推荐答案

^ ].


我主要使用此代码段来验证文本框用户输入.但是我认为您也可以在DataGridViewCell上使用它.试试看.

而且您不必在C#中使用ASCII代码.您可以将 e.KeyChar 与一个直接表示ASCII值的整数进行比较.

I mostly used this code snippet to validate textbox user input. But I think you can use it on a DataGridViewCell as well. Give it a try.

And you don''t have to use ASCII codes in C#. You can compare e.KeyChar to an integer which represents the ASCII value directly.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //97 - 122 = Ascii codes for simple letters
            //65 - 90  = Ascii codes for capital letters
            //48 - 57  = Ascii codes for numbers

            if (e.KeyChar != 8)
            {
                if (e.KeyChar < 48 | e.KeyChar > 57)
                {
                    e.Handled = true;
                }
            }
        }


使用 [^ ]方法检查其内容.
Use one of the System.Char[^] methods to check what it is.


这篇关于比较输入值的ASCII以检查它是字符还是数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆