控制TextBox只能输入货币格式

public void money(KeyPressEventArgs e,TextBox TextBox1)   

 //对文本框的输入进行限制,只能输入数字或第一个小数点       

{           

if (char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back)           

 {                

//输入的是数字或小数点或是backspace键     

 //如果输入的是数字或小数点          

e.Handled = false;       //让操作生效            

int j = 0;               

for (int i = 0; i < TextBox1.Text.Length; i++)               

{                   

if (TextBox1.Text[i] == '.')                   

{                       

j++;                   

}                   

if (j >= 1)                   

{                       

if (e.KeyChar == '.')                       

{                           

e.Handled = true;             //让操作失效          

}                       

break;                   

}               

}           

}           

else           

{               

e.Handled = true;           

}       

}

//此方法用于文本框的KeyPress事件

你可能感兴趣的:(C#)