c锐利的isnumeric [英] isnumeric in c sharp

查看:89
本文介绍了c锐利的isnumeric的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中有一个掌上电脑项目。


从文本框我必须验证输入是否为数字。我在msdn中找到了

这样的样本:textbox1.numeric = true / false。但是

这在我的环境中不起作用。


如果我将文本框内容显式转换为数值,并且是

不是数字,程序遇到错误处理程序(试试catch)。

这不是我想要的!


例如:2个文本框, 1按钮

如果用户点击按钮,程序应该证明文本框的内容是否为数字,如果没有,则应使用标准值

然后再继续。


像''isnumeric'这样的好方法会很高兴。


谢谢大家帮助。

I have a pocket pc project in c#.

From a textbox i have to verify if the input is numeric. I have found
in the msdn a sample like this: textbox1.numeric = true / false. But
this do not work in my environment.

If I convert the textbox content explicit to a numeric value, and is
is not numeric, the program ran into the error handler (try catch).
This is not what I want!

For example: 2 textbox, 1 button
If the user click the button, the program should proof if the content
of the textboxes is numeric, if not, it should use a standard value
and then go on.

A nice method like ''isnumeric'' would be glad.

Thanks for all assistance.

推荐答案

您好,


您可以尝试提供文本框的Text属性值到Int32类型的Parse

方法,并捕获FormatException,OverflowException

和可选的ArgumentNullException(这个不太可能被提出)。

如果抛出了任何异常,则为文本框's文本不是有效的

整数。


这种方法对Double,Decimal或任何其他类型的工作方式相同

支持Parse方法。


-

Dmitriy Lapshin [C#/ .NET MVP]

X-Unity测试工作室
http://www.x-unity.net/teststudio .aspx

将单元测试的强大功能带到VS .NET IDE


" crowl" < CR *** @ gmx.de>在消息中写道

news:e1 ************************** @ posting.google.c om ...
Hi,

You can attempt to feed the textbox''s Text property value to the Parse
method of the Int32 type, and catch the FormatException, OverflowException
and optionally ArgumentNullException (this one is unlikely to be raised).
If any of the exceptions has been thrown, the textbox''s text is not a valid
integer.

This approach works the same way for Double, Decimal or any other type
supporting the Parse method.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"crowl" <cr***@gmx.de> wrote in message
news:e1**************************@posting.google.c om...
我在c#中有一个掌上电脑项目。

从文本框中我必须验证输入是否为数字。我在msdn中找到了这样一个样本:textbox1.numeric = true / false。但是
这在我的环境中不起作用。

如果我将文本框内容显式转换为数值,并且
不是数字,程序就会遇到错误处理程序(试试看)。
这不是我想要的!

例如:2个文本框,1个按钮
如果用户点击按钮,程序应该证明是否有内容
的文本框是数字的,如果没有,它应该使用标准值
然后继续。

像''isnumeric'这样的好方法会很高兴。<谢谢你的帮助。
I have a pocket pc project in c#.

From a textbox i have to verify if the input is numeric. I have found
in the msdn a sample like this: textbox1.numeric = true / false. But
this do not work in my environment.

If I convert the textbox content explicit to a numeric value, and is
is not numeric, the program ran into the error handler (try catch).
This is not what I want!

For example: 2 textbox, 1 button
If the user click the button, the program should proof if the content
of the textboxes is numeric, if not, it should use a standard value
and then go on.

A nice method like ''isnumeric'' would be glad.

Thanks for all assistance.






你有没有理由不能使用try / catch块与System.Convert





void Button_Click(对象发送者,eventargs e


//验证文本框是否包含一个整数

tr


int i = System.Convert.ToInt32(TextBox1.Text,10)

//如果你到达那里,那么i包含TextBox1.Te的整数值x


catch(System.FormatException e


//转换失败所以使用默认值

i = defaultValue


//继续处理

杰克逊戴维斯[msft

-

发布该帖子按现状没有保证,也没有权利

样品须遵守指定的条款
http://www.microsoft.com/info/cpyright.ht

Is there a reason you can''t use a try / catch block with System.Convert

such as

void Button_Click(object sender, eventargs e

// verify the textbox contains an intege
tr

int i = System.Convert.ToInt32(TextBox1.Text, 10)
// if you get here then the i contains the integer value of TextBox1.Tex

catch (System.FormatException e

// the conversion failed so use the default value
i = defaultValue

// Continue processing
Jackson Davis [msft
-
This posting is provided "AS IS" with no warranties, and confers no right
Samples are subject to the terms specified a
http://www.microsoft.com/info/cpyright.ht


Hello Jackson ,


我认为捕捉FormatException只是一个不可靠的解决方案。

想象一下,用户输入了类似9999999999999999999999999999的内容。

文本框。在这种情况下,我想在调用ToInt32方法时抛出一个OverflowException实例,而不是FormatException。


-

Dmitriy Lapshin [C#/ .NET MVP]

X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx

将单元测试的强大功能带到VS .NET IDE中/>

" Jackson Davis [MSFT]" <一个******* @ discussions.microsoft.com>写在

消息新闻:81 ********************************** @ microsof t.com ...
Hello Jackson,

I think catching FormatException only would be an unreliable solution.
Imagine the user has typed something like "9999999999999999999999999999" to
the textbox. In this case, I suppose an OverflowException instance will be
thrown upon calling the ToInt32 method, not a FormatException.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Jackson Davis [MSFT]" <an*******@discussions.microsoft.com> wrote in
message news:81**********************************@microsof t.com...
你有没有理由不能在System.Convert上使用try / catch块?

如:

void Button_Click(对象发送者,eventargs e)
//验证文本框是否包含整数
尝试
{i / int int = System.Convert。 ToInt32(TextBox1.Text,10);
//如果你到了这里,那么i包含
的整数值TextBox1.Text}
catch(System.FormatException e)
{
//转换失败,请使用默认值。
i = defaultValue;
}
//继续处理。
}
杰克逊戴维斯[msft]
-
此帖子是原样提供的。没有保证,也没有授予任何权利
样品须遵守
http://www.microsoft.com/info/cpyright.htm
Is there a reason you can''t use a try / catch block with System.Convert?

such as:

void Button_Click(object sender, eventargs e)
{
// verify the textbox contains an integer
try
{
int i = System.Convert.ToInt32(TextBox1.Text, 10);
// if you get here then the i contains the integer value of TextBox1.Text }
catch (System.FormatException e)
{
// the conversion failed so use the default value.
i = defaultValue;
}
// Continue processing.
}

Jackson Davis [msft]
--
This posting is provided "AS IS" with no warranties, and confers no rights
Samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm






这篇关于c锐利的isnumeric的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆