我有一個文本框,其值是自然正數,因爲我做了一些計算後,從另一個文本框中生成它,我的文本框保存的結果肯定是數字,當我嘗試將該值轉換爲它始終給出0的整數,而文本框的值保持大於0且根本不爲空。
我都嘗試轉換方法在這裏msdn
我也試過TryParse
方法在這裏成立Convert textbox text to integer
我創建2個破發點看到轉換後的文字都文本框中的文本和整數值的結果
這裏是我的代碼:將文本框文本轉換爲整數給出0
MessageBox.Show(updateProductTQ.Text, "Quantity");
p.ProductQuantity = int.Parse(updateProductTQ.Text);
MessageBox.Show(p.ProductQuantity.ToString(), "Quantity");
updateProductTQ
是我的文本框的名字,p.ProductQuantity
是整數
第一MessageBox
輸出是在文本框中顯示的數字10現在
第二個MessageBox
輸出很大0
有人能告訴我爲什麼我得到0嗎?
我嘗試另一個文本框持有數中的轉換和正常工作是很奇怪的事情
這裏是我的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; }
}
,並通過這些文本框提出的所有通過結合和完成它運作良好:
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");
}
}
你可以發佈'updateProductTQ.Text'的值嗎? –
你能告訴我們'ProductQuantity'屬性是如何定義的嗎? –
您測試過的產品是否爲ProductCarton> 0?然後它不會直接返回存儲的'_ProdQuan',而是重新計算它。 –