Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#使用try parse强制数字上下只接受正整数_C#_Integer_Numeric_Updown - Fatal编程技术网

c#使用try parse强制数字上下只接受正整数

c#使用try parse强制数字上下只接受正整数,c#,integer,numeric,updown,C#,Integer,Numeric,Updown,我只做了几天,我很困惑 其他一切都正常,该框仅显示整数,但仍使用十进制值进行计算。您应该能够执行int myInt=Convert.ToInt32(numBox.Value) Value是一个十进制数,但该代码将使其转换为整数 只要知道,如果你得到一个小数值,它会向上或向下取整 编辑: 如果你只想要正值,Aghilas Yakoub的解决方案可能会更好。我的解决方案将十进制转换为整数,但它仍然允许负数。实际上,您应该做的是将numBox.Minimum设置为0,使其不能低于0 编辑2: 如果要在

我只做了几天,我很困惑


其他一切都正常,该框仅显示整数,但仍使用十进制值进行计算。

您应该能够执行
int myInt=Convert.ToInt32(numBox.Value)

Value是一个十进制数,但该代码将使其转换为整数

只要知道,如果你得到一个小数值,它会向上或向下取整

编辑: 如果你只想要正值,Aghilas Yakoub的解决方案可能会更好。我的解决方案将十进制转换为整数,但它仍然允许负数。实际上,您应该做的是将
numBox.Minimum
设置为0,使其不能低于0

编辑2: 如果要在值为负值时发出警告,请尝试以下操作:

int myInt = Convert.ToInt32(numBox.Value);
if (myInt < 0)
{
    MessageBox.Show("Warning, your number must be 0 or greater");
}
int myInt=Convert.ToInt32(numBox.Value);
if(myInt<0)
{
MessageBox.Show(“警告,您的号码必须为0或更大”);
}
如果该值不是整数(具有十进制值),是否要发出警告?

您可以使用

UInt32 result = 0;

UInt32.TryParse("...",
                new CultureInfo(""),
                out result);

if(result == 0)
{
  Console.WriteLine("No parsing");
}
else
{
 Console.WriteLine("result={0}", result);
}

更优雅的解决方案是:

如果您可以访问DevExpress控件,则应使用控件。 要将其范围从0限制到999(仅限整数),请设置其:

  • Properties.Mask.EditMask
    to“##0;”
  • Properties.MaxValue
    至999
那么下面这一行应该毫无例外地正常工作:

int myInt = Convert.ToInt32(numBox.Value);

我应该说得更具体些,我不需要舍入它,我需要一个messagebox.show(“”)警告无效条目。谢谢你!是的,这正是我真正需要的抱歉,我真的不知道,但是既然CultureInfo说它没有被使用,我该如何定义它呢。有没有一种方法可以将其转换为警告消息messagebox.show(“”),而不仅仅是取整?