标签:
TextBox只允许输入数字,最大长度为10
//TextBox.ShortcutsEnabled为false 禁止右键和Ctrl+v private void txtNumber_KeyPress(object sender, KeyPressEventArgs e) { //只允许输入数字,粘贴数字 if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == (char)8)) { e.Handled = true; } } //允许Ctrl+v粘贴数字 private void txtNumber_KeyUp(object sender, KeyEventArgs e) { if (e.KeyData == (Keys.Control | Keys.V)) { if (Clipboard.ContainsText()) { try { Convert.ToInt64(Clipboard.GetText()); //检查是否数字 ((TextBox)sender).SelectedText = Clipboard.GetText().Trim(); //Ctrl+V 粘贴 if (((TextBox)sender).TextLength > 10) { ((TextBox)sender).Text = ((TextBox)sender).Text.Remove(10); //TextBox最大长度为10 移除多余的 } } catch (Exception) { e.Handled = true; //throw; } } } }
c# TextBox只允许输入数字,禁用右键粘贴,允许Ctrl+v粘贴数字
标签:
原文地址:http://www.cnblogs.com/han1982/p/4770270.html