如何使用C#验证texbox [英] how to validate the texbox using c#
本文介绍了如何使用C#验证texbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发Windows应用程序,在这里我需要验证文本框,该文本框只能接受数字值,单点(.),退格键和Delete键.为此,我正在编写此代码
I am developing a windows application where i need to validate the textbox which can accept only numeric value, single dot(.), and backspace and delete key. for this i am writing this code
if ((e.KeyChar=='.' && textBox1.Text.IndexOf('.')>-1) || char.IsLetter(e.KeyChar) || char.IsPunctuation(e.KeyChar) || char.IsSymbol(e.KeyChar))
{
e.Handled = true;
}
但它不接受点号(.),因为(.)属于标点符号.请帮助我
为此,它接受(.)除外的其他标点符号.
but it is not accepting the dot(.) because (.) comes under punctuation. please help me
to do this which accepts the (.) except others punctuation.
推荐答案
为什么使用文本框?
改用NumericUpDown-它看起来像一个文本框(带有向上和向下箭头),它像文本框一样工作,但是已经内置了验证功能.
Why use a textbox?
Use a NumericUpDown instead - it looks like a textbox (with an up and a down arrow), it works like a text box, but it has the validation already built in.
简单一点:
It would be a little simpler:
if ((e.KeyChar==''.'' && textBox1.Text.IndexOf(''.'')>-1) || ! e.KeyChar==Keys.Delete && ! e.KeyChar==Keys.Back || ! char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
hiii,
试试这个
hiii,
try this
if ( char.IsLetter(e.KeyChar) || char.IsPunctuation(e.KeyChar) || char.IsSymbol(e.KeyChar))
{
if ((e.KeyChar == '.' && textBox1.Text.IndexOf('.')>-1 ))
{
e.Handled = true;
}
}
这篇关于如何使用C#验证texbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文