TextBox禁止复制粘贴和数字验证,小数验证,汉字验证

验证小数

#region 验证小数
/// <summary>
/// 验证小数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtRoomArea_KeyDown_ForFloat(object sender, KeyEventArgs e)
{
TextBox txt = sender as TextBox;
//屏蔽非法按键,只能输入小数
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Subtract || e.Key == Key.Decimal)
{
if (txt.Text.Contains(".") && e.Key == Key.Decimal)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod || e.Key == Key.Subtract) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
{
if (txt.Text.Contains(".") && e.Key == Key.OemPeriod)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else
{
e.Handled = true;
} }
#endregion

验证小数

TextBox内容改变的时候检测

  #region TextBox_TextChanged
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{ //屏蔽中文输入和粘贴输入
TextBox textBox = sender as TextBox;
TextChange[] change = new TextChange[e.Changes.Count];
e.Changes.CopyTo(change, ); int offset = change[].Offset;
if (change[].AddedLength > )
{
double num = ;
if (!Double.TryParse(textBox.Text, out num) && !textBox.Text.Equals("-"))
{
textBox.Text = textBox.Text.Remove(offset, change[].AddedLength);
textBox.Select(offset, );
}
}
// NoteFieldConfigModel noteField = NoteFieldConfigs.First(t => t.FfieldName == textBox.Name);
// string value = string.IsNullOrEmpty(textBox.Text) == true ? null : textBox.Text;
// if(string.IsNullOrEmpty(value))
// {
// return;
// }
//if (noteField.FfieldType == 3)
//{
// textBox.Text = string.Format("{0:N2}", Convert.ToDouble(value.TrimStart(',')));
//}
//if (noteField.FfieldType == 2 && (noteField.FimptClassID == null || noteField.FimptClassID == 0))
//{
// textBox.Text = string.Format("{0:N0}", Convert.ToDouble(value.TrimStart(',')));
//} }
#endregion

内容改变的时候的事件

上一篇:gcc 优化选项 -O1 -O2 -O3 -Os 优先级


下一篇:ASP.NET MVC & WebApi 中实现Cors来让Ajax可以跨域访问 (转载)