excel vba 将一个文本框TEXTBOX1限定只能输入数字,如果输入其他汉字或者字母提示,输入错误,请输入数字
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsNumeric(TextBox1) Then
MsgBox "不是数字!"
Cancel = True
End If
End Sub
Private Sub CommandButton1_Click()
Dim i As Double
i = Format(Replace(TextBox1.Value, "。", "."), "0.00")
Sheet2.Range("A1").Value = i
End Sub
此代码不管textbox1输入的是句号还是点,都将变成点。四舍五入保留两位小数。
扩展资料:
AcceptsReturn 指示在多行TextBox组件中按ENTER键时,是在组件中创建一行新文本还是激活窗体的默认按钮。
AcceptsTab 该值指示在多行文本框组件中按TAB键时,是否在组件中键入一个TAB字符,而不是按选项卡的顺序将焦点移动到下一个组件。
AllowDrop 获取或设置一个值,该值指示组件是否可以接受用户拖放到它上面的数据。
Anchor 获取或设置组件的哪些边缘锚定到其容器边缘。
BackColor 获取或设置组件的背景色。
BackgroundImage 获取或设置在组件中显示的背景图像。
参考资料来源:百度百科-textbox
如何设置textbox只能输入数字
方法一:
privatevoidtBox_KeyPress(objectnder,KeyPressEventArg)
{
if(e.KeyChar==0x20)e.KeyChar=(char)0;//禁止空格键
if((e.KeyChar==0x2D)&&(((TextBox)nder).Text.Length==0))return;//处理负数
if(e.KeyChar>0x20)
{
try
{
double.Par(((TextBox)nder).Text+e.KeyChar.ToString());
}
catch
{
e.KeyChar=(char)0;//处理非法字符
}
}
}
方法二:
privatevoidTextBox_KeyPress(objectnder,KeyPressEventArg)
{
if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))
{
e.Handled=true;
}
}
或者
privatevoidTextBox_KeyPress(objectnder,KeyPressEventArg)
{
if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))
{
e.Handled=true;
}
}
方法三:
privatevoidtextBox1_KeyPress(objectnder,System.Windows.Forms.KeyPressEventArg)
{
if(e.KeyChar!='\b')//这是允许输入退格键
{
if((e.KeyChar<'0')||(e.KeyChar>'9'))//这是允许输入0-9数字
{
e.Handled=true;
}
}
}
方法四:
privatevoidtextBox1_Validating(objectnder,CancelEventArg)
{
conststringpattern=@"^\d+\.?\d+{1}quot;;
stringcontent=((TextBox)nder).Text;
if(!(Regex.IsMatch(content,pattern)))
{
errorProvider1.SetError((Control)nder,"只能输入数字!");
e.Cancel=true;
}
el
errorProvider1.SetError((Control)nder,null);
}
方法五:
privatevoidtextBox1_KeyPress(objectnder,System.Windows.Forms.KeyPressEventArg)
{
if(e.KeyChar=='.'&&this.textBox1.Text.IndexOf(".")!=-1)
{
e.Handled=true;
}
if(!((e.KeyChar>=48&&e.KeyChar<=57)||e.KeyChar=='.'||e.KeyChar==8))
{
e.Handled=true;
}
}
方法六:
privatevoidtbx_LsRegCapital_KeyPress(objectnder,KeyPressEventArg)
{
if(!Char.IsNumber(e.KeyChar)&&!Char.IsPunctuation(e.KeyChar)&&!Char.IsControl(e.KeyChar))
{
e.Handled=true;//消除不合适字符
}
elif(Char.IsPunctuation(e.KeyChar))
{
if(e.KeyChar!='.'||this.textBox1.Text.Length==0)//小数点
{
e.Handled=true;
}
if(textBox1.Text.LastIndexOf('.')!=-1)
{
e.Handled=true;
}
}
}
方法七:
利用ASCII码处理办法、
{
if((e.KeyChar<=48||e.KeyChar>=57)&&(e.KeyChar!=8)&&(e.KeyChar!=46))
e.Handled=true;
================48代表0,57代表9,8代表空格,46代表小数点
}
c# 如何实现textbox中只能输入数字
怎么使textbox文本框只能输入数字
如何控制textbox内只能输入数字
C# 文本框只能输入数字
代码如下:
调用TextBox的KeyPress事件
private void txtUrId_KeyPress(object nder, KeyPressEventArgs e)
{
//如果输入的不是数字键,也不是回车键、Backspace键,则取消该输入
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar!=(char)13 && e.KeyChar!=(char)8)
{
e.Handled = true;
}
}
扩展资料:注意事项
C#文本框输入限制
//只能输入数字和小数点和退格键
private void txt_KeyPress(object nder, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
{
e.Handled = true;
}
}
//只能输入数字和退格键
private void txt_KeyPress(object nder, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
{
e.Handled = true;
}
}
//限制输入只能为数字
private void txt_KeyPress(object nder, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (Char)8)
{
e.Handled = true;
}
}
//限制输入不能为中文和全角
private void txt_KeyPress(object nder, KeyPressEventArgs e)
{
int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fa5)转换成int(chfrom~chend)
int chend = Convert.ToInt32("9fa5", 16);
if (e.KeyChar >= (Char)chfrom && e.KeyChar <= (Char)chend)
{
e.Handled = true;
}
if (e.KeyChar >= (Char)65281 & (int)e.KeyChar <= (Char)65374)
{
e.Handled = true;
}
}
//限制输入只能输入数字和字母,退格键
private void txt_KeyPress(object nder, KeyPressEventArgs e)
{
if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')
|| (e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
{
e.Handled = fal;
}
el
{
e.Handled = true;
}
}
本文发布于:2023-02-28 18:49:00,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/zhishi/a/167758523945052.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:textbox只能输入数字.doc
本文 PDF 下载地址:textbox只能输入数字.pdf
留言与评论(共有 0 条评论) |