[英]Converting TextBox text to integer gives 0
i have a text box and its value is natural positive number because am generating it from another text boxes after doing some calculation and my text box hold the result which is definitely number, when i try to convert the value to integer it always gives 0 while the text box hold a value greater than 0 and not null at all. 我有一个文本框,其值是自然正数,因为在进行一些计算后从另一个文本框生成了它,而我的文本框却保留了绝对是数字的结果,当我尝试将值转换为整数时,它总是给出0,而文本框的值大于0且不为null。
i tried both converting methods in here msdn 我想这两个转换方法在这里MSDN
also i tried the TryParse
method founded here Convert textbox text to integer 我也尝试了这里建立的TryParse
方法将文本框文本转换为整数
i create 2 break points to see the result of both text box text and integer value after converting the text 我创建了两个断点,以在转换文本后查看文本框文本和整数值的结果
here is my code: 这是我的代码:
MessageBox.Show(updateProductTQ.Text, "Quantity");
p.ProductQuantity = int.Parse(updateProductTQ.Text);
MessageBox.Show(p.ProductQuantity.ToString(), "Quantity");
updateProductTQ
is my text box name, p.ProductQuantity
is integer updateProductTQ
是我的文本框名称, p.ProductQuantity
是整数
the first MessageBox
output is the number shown in the text box 200
now 第一个MessageBox
输出是现在文本框200
显示的数字
the second MessageBox
output is big 0 第二个MessageBox
输出为大0
can someone tell me why i get 0 ??? 有人可以告诉我为什么我得到0 ???
i try the conversion for another text box holding number and it works fine that is really weird thing 我尝试将另一个文本框保存为数字进行转换,但效果很好,这确实很奇怪
here is my ProductQuantity
definition: 这是我的ProductQuantity
定义:
public int ProductQuantity
{
get
{
if (ProductCartoons >= 0)
{
if (ProductBigBox > 0 && ProductSmallBox > 0 && ProductMedBox > 0 && ProductItems >= 0)
{
_ProdQuan = ProductCartoons * ProductBigBox * ProductMedBox * ProductSmallBox * ProductItems;
}
else
if (ProductBigBox > 0 && ProductMedBox > 0 && ProductItems >= 0)
{
_ProdQuan = ProductCartoons * ProductBigBox * ProductMedBox * ProductItems;
}
else
if (ProductBigBox>0 && ProductSmallBox > 0 && ProductItems >= 0)
{
_ProdQuan = ProductCartoons * ProductBigBox * ProductSmallBox * ProductItems;
}
else
if (ProductMedBox > 0 && ProductSmallBox > 0 && ProductItems >= 0)
{
_ProdQuan = ProductCartoons * ProductMedBox * ProductSmallBox * ProductItems;
}
else
if (ProductBigBox>0 && ProductItems >= 0)
{
_ProdQuan = ProductCartoons * ProductBigBox * ProductItems;
}
else
if (ProductMedBox > 0 && ProductItems >= 0)
{
_ProdQuan = ProductCartoons * ProductMedBox * ProductItems;
}
else
if (ProductSmallBox > 0 && ProductItems >= 0)
{
_ProdQuan = ProductCartoons * ProductSmallBox * ProductItems;
}
else
if (ProductItems >= 0)
{
_ProdQuan = ProductCartoons * ProductItems;
}
}
return _ProdQuan;
}
set { _ProdQuan = value; }
}
and it Raised by these text boxes all done by binding and it is working well: 并且通过这些文本框引发的所有操作都是通过绑定完成的,并且运行良好:
public int ProductCartoons
{
get { return _Cartoons; }
set
{
_Cartoons = value;
RaisePropertyChangedEvent("ProductQuantity");
}
}
public int ProductBigBox
{
get { return _BigBox; }
set
{
_BigBox = value;
RaisePropertyChangedEvent("ProductQuantity");
}
}
public int ProductMedBox
{
get { return _MedBox; }
set
{
_MedBox = value;
RaisePropertyChangedEvent("ProductQuantity");
}
}
public int ProductSmallBox
{
get { return _SmallBox; }
set
{
_SmallBox = value;
RaisePropertyChangedEvent("ProductQuantity");
}
}
public int ProductItems
{
get { return _Items; }
set
{
_Items = value;
RaisePropertyChangedEvent("ProductQuantity");
}
}
There you see the reason, if you notice the ProductQuantity
definition it's value(in the getter) depends on ProductCartoons
, if this value is 0
you always get 0
for ProductQuantity
. 在那里,您会看到原因,如果您注意到ProductQuantity
定义,则其值(在getter中)取决于ProductCartoons
,如果该值为0
,则您始终获得0
的ProductQuantity
。
I would suggest use debugger and go through step by and see what is causing 0
in this case. 我建议使用调试器,并逐步了解导致这种情况的0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.