热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将TextBox文本转换为整数给出0-ConvertingTextBoxtexttointegergives0

ihaveatextboxanditsvalueisnaturalpositivenumberbecauseamgeneratingitfromanothertex

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:

我有一个文本框,它的值是自然正数,因为我做了一些计算后从另一个文本框生成它,我的文本框保存结果肯定是数字,当我尝试将值转换为整数时,它总是给0文本框保存大于0的值,而不是null。我尝试了两种转换方法在这里msdn也尝试了这里建立的TryParse方法将文本框文本转换为整数我创建2个断点,以查看转换文本后文本框文本和整数值的结果这里是我的代码:

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

updateProductTQ是我的文本框名,p.ProductQuantity是整数,第一个MessageBox输出是文本框200中显示的数字,现在第二个MessageBox输出大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

谁能告诉我为什么我得到0 ???我尝试转换另一个文本框保持号码,它工作正常,这是非常奇怪的事情

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");
        }
    }

1 个解决方案

#1


0  

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的原因。


推荐阅读
author-avatar
淡漠初夏0_176
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有