简体   繁体   中英

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.
i tried both converting methods in here msdn
also i tried the TryParse method founded here Convert textbox text to integer
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
the first MessageBox output is the number shown in the text box 200 now
the second MessageBox output is big 0

can someone tell me why i get 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:

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 .

I would suggest use debugger and go through step by and see what is causing 0 in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM