' Visual Basic
Private Sub TextBox1_Validating(ByVal Sender As Object, ByVal e As CancelEventArgs) Handles TextBox1.Validating
If Not IsNumeric(TextBox1.Text) Then
ErrorProvider1.SetError (TextBox1, "Not a numeric value.")
Else
' Clear the error.
ErrorProvider1.SetError (TextBox1, "")
End If
End Sub// C#
protected void textBox1_Validating (object sender, CancelEventArgs e)
{
try
{
int x = Int32.Parse(textBox1.Text);
errorProvider1.SetError(textBox1, "");
}
catch
{
errorProvider1.SetError(textBox1, "Not an integer value.");
}
}连microsoft都这么写,你还能想出什么办法吗?
Private Sub TextBox1_Validating(ByVal Sender As Object, ByVal e As CancelEventArgs) Handles TextBox1.Validating
If Not IsNumeric(TextBox1.Text) Then
ErrorProvider1.SetError (TextBox1, "Not a numeric value.")
Else
' Clear the error.
ErrorProvider1.SetError (TextBox1, "")
End If
End Sub// C#
protected void textBox1_Validating (object sender, CancelEventArgs e)
{
try
{
int x = Int32.Parse(textBox1.Text);
errorProvider1.SetError(textBox1, "");
}
catch
{
errorProvider1.SetError(textBox1, "Not an integer value.");
}
}连microsoft都这么写,你还能想出什么办法吗?
解决方案 »
- 求完整的论坛源码和博客源码,c#的,谢谢!!
- 用BitConverter.ToString方法得到的字串怎么转?
- VS2005中 如何通过Key来对dictionary 排序
- 如何屏蔽IE的后退功能-求具体代码
- lanMsg的程序设计原理,不明白呀
- 请问:TcpListener类的AcceptTcpClient()方法,与AcceptSocket()方法,有什么不同使用地方呢?
- 我在FORM1中如何给FORM2的控件赋值?
- 在其他Form里面如何调用控制MainForm里面的控件?
- 求助:C#.net如何制作安装程序,安装程序能够自动创建oracle表空间、用户,并创建数据表?
- 用C#编写windows应用程序能不能带命令行参数?
- XML注释问题.
- 在线求救,WINFORM中如何限制TEXTBOX的输入符合一定格式?
或者使用vb.net(vb里面也有)的IsNumeric,如果在c#里面使用,需要添加对Microsoft.VisualBasic的引用。使用的方法:Microsoft.VisualBasic.Information.IsNumeric(this.txtID.Text))
as 运算符类似于类型转换,所不同的是,当转换失败时,as 运算符将产生空,而不是引发异常。在形式上,这种形式的表达式:
expression as type
等效于:
expression is type ? (type)expression : (type)null
只是 expression 只被计算一次。
请注意,as 运算符只执行引用转换和装箱转换。as 运算符无法执行其他转换,如用户定义的转换,这类转换应使用 cast 表达式来代替其执行。