textbox只能输入数字

更新时间:2023-02-28 19:53:59 阅读: 评论:0

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中只能输入数字

这篇文章主要介绍了C# TextBox控件实现只能输入数字的方法,本文使用TextBox的keypress事件实现这个需求,需要的朋友可以参考下

只需要在控件TextBox的keypress事件中写入如下代码即可满足要求:

代码如下:

if (e.KeyChar == '.' && this.txbEnd.Text.IndexOf(".") != -1)

{

e.Handled = true;

}

if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))

{

e.Handled = true;

}

其中.txbEnd为当前你窗体中textbox控件的name

怎么使textbox文本框只能输入数字

工具/原料

Microsoft Visual Studio 2010
方法/步骤

打开Microsoft Visual Studio 2010, 新建名字为【文本框限制输入为数字示例】的程序。
在新程序界面窗口上放置合适的控件:包括,
显示过程日志和提示的textbox输出控件;
用于输入11位数字的输入框。

选择输入的文本框,将属性中的最大长度限制修改为11,保证只能输入11位数字。

选择文本框的事件窗口,找到按键输入的方法KeyPress,双击建立新的方法。

在按键方法中添加文本框只能输入数字的代码。

新增日志输出方法output,可以将过程日志进行输出。

7
生成exe文件进行测试。

如何控制textbox内只能输入数字

不用那么麻烦,2种办法:1. 用js正则表达式: 1)首先在你的html<head></head>之间添加如下js代码: <script type="text/javascript">
function isDigit(s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec(s))
{
alert('请输入数字');
return fal;
}
return true
}
</script> 2)然后在你的textbox 上添加如下修饰:onkeyup="return isDigit(this.value);" 例如: <asp:TextBox ID="TextBox1" runat="rver" onkeyup="return isDigit(this.value);"></asp:TextBox> 2. 另一种方法比较方便,使用.net自带的validation控件: 1)从工具箱中拖动一个RegularExpressionValidator控件 2)设置这个RegularExpressionValidator控件的ControToValidate属性 为你的textbox 3) 设置这个RegularExpressionValidator的ValidationExpression属性为^[0-9]{1,20}$,就ok了 两种方法你可以随便选择,都行。有什么问题再讨论吧。

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

标签:数字   textbox
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 实用文体写作网旗下知识大全大全栏目是一个全百科类宝库! 优秀范文|法律文书|专利查询|