c#中如何使 textbox的输入问题

来源:百度知道 编辑:UC知道 时间:2024/07/03 04:51:08
c#中如何使 textbox只能输入decimal型数字而输入其他字符时,弹出对话框提示输入有误。

本人菜鸟,请详细说明编程过程

重写OnKeyPress,就行了,你可以看看下面这段程序
protected override void OnKeyPress(KeyPressEventArgs e)
{
//非符号,非退格键,非小数点号,非数字退出
if(!char.IsNumber(e.KeyChar) && e.KeyChar!='.' && e.KeyChar!=8 && e.KeyChar!='-')
{
e.Handled =true;
}
else if(e.KeyChar =='0')
{
if(this.SelectionStart ==1 && this.Text[0]=='0')
{
e.Handled =true;
}
}
else if(e.KeyChar =='-')
{
if(this.Text.Length -this.SelectedText.Length ==0)
{
}
else
{
for(int i=0;i<this.Text.Length;i++)
{
if(this.Text[i]=='-')
{
e.Handled =true;
}
}
}
}
else if(e.KeyChar ==8)
{

}
else if(e.KeyChar =='.')
{
if(this.MyMode==DataMode.DataFloat)
{
if(this