单击后c#空文本框

时间:2015-02-19 09:23:53

标签: c#

我有这个问题,我运行我的代码,它给我空文本框,我不知道为什么。我调试了它,发现number = double.Parse(txtTableAvgTemp.Text);poolCost = double.Parse(txtTableDollars.Text);都返回NULL。该代码用于计算相关池的加热成本。

const double poolLengthMin = 5; // min pool length
     const double poolLengthMax =50; // max pool length
     const double poolWidthMin = 2;  // min pool width
     const double poolWidthMax = 20; // max pool width
     const double poolDepthMin = 2;  // min pool depth
     const double poolDepthMax = 4;  // max pool depth

     // variable used in btnCalculate_Click
    float poolLength;
    float poolWidth;
    float poolDepth;
    float SurfaceArea = 0;
    float Volume = 0;
    const int poolSize = 0;
    const int smallPool = 500000 ;
    const int mediumPool = 1500000;
    const double poolTemp = 1.5;
    double poolDegreesMin = 5;
    double poolDegreesMax = 25;
    double number;
    double costToHeatPool;
    double heatingVolume;
    double poolDegrees = 5;


    /* validation statements for pool
     * length, width and depth
     */
    bool ValidPoolLength(double poolLength)
    {
        if (poolLength >= poolLengthMin && poolLength <= poolLengthMax)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool ValidPoolWidth(double poolWidth)
    {
        if (poolWidth >= poolWidthMin && poolWidth <= poolWidthMax)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool ValidPoolDepth(double poolDepth)
    {
       if(poolDepth >= poolDepthMin && poolDepth <= poolDepthMax)
       {
            return true;
       }
       else 
       {
            return false;
       }
    }
    // end of validation statements

    private void lblCategory_Click(object sender, EventArgs e)
    {

    }

    private void btnCalculate_Click(object sender, EventArgs e)
    {   // convert variable to float from double from string
        poolLength = float.Parse(txtLength.Text);
        poolWidth = float.Parse(txtWidth.Text);
        poolDepth = float.Parse(txtAvgDepth.Text);


        //clear string 
        txtVolume.Clear();
        txtSurfaceArea.Clear();
        txtTableDollars.Clear();


        // error massages for pool length
        //pool width and pool depth

    if (!(ValidPoolLength(poolLength)))
    {
        MessageBox.Show("Length measurement is invalid \r\n Please enter a value between : " + poolLengthMin + " and " + poolLengthMax, "Pool Lenght Invalid" , MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
    }
    if (!(ValidPoolWidth(poolWidth)))
    {
        MessageBox.Show("Width measurment is invalid \r\n Please enter a value between : " + poolWidthMin + " and " + poolWidthMax, "Pool Width Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return;
    }
    if (!(ValidPoolDepth(poolDepth)))
    {
        MessageBox.Show("Pool Depth is invalid \r\n Please enter a value between : " + poolDepthMin + " and " + poolDepthMax, "Pool Depth Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return;
    }
        // caclulate surface area and show in txtSurfaceArea

        SurfaceArea = poolLength * poolWidth;
        txtSurfaceArea.Text += SurfaceArea;

        //calculate pool volume and show in txtVolume
        Volume = poolLength * poolWidth * poolDepth * 1000;
        txtVolume.Text += Volume;


        //calculate size of pool Small, Medium or large
        //and show in lblcategory

     Volume = float.Parse(txtVolume.Text);
     if (poolSize <= smallPool && smallPool >= Volume)
     {
        lblCategory.Text = "Pool Category: Small";
     }
     else if (poolSize <= mediumPool && mediumPool >= Volume)
     {

        lblCategory.Text = "Pool Category: Medium";
     }
     else
     {
       lblCategory.Text = "Pool Category: Large";
     }


        //cost to heat the pool to 25 degrees


     while (poolTemp >= poolDegrees && poolTemp < poolDegrees)
    {
        number = poolDegrees + poolTemp;
        txtTableAvgTemp.Text +=  number.ToString() + "\r\n";
        poolDegrees += poolTemp;

        //variable for costing out heating of pool

        double poolCost = costToHeatPool;
        heatingVolume = float.Parse(txtVolume.Text);
        costToHeatPool = float.Parse(txtVolume.Text);
        poolCost = double.Parse(txtTableDollars.Text);//empty ?
        number = double.Parse(txtTableAvgTemp.Text);//empty ?
        poolCost = (Math.Truncate(poolCost));


            //formula for costing of heating to pool
            costToHeatPool = (25 - number) * heatingVolume / 32500;
            txtTableDollars.Text += poolCost + "\r\n";
    }
}

我已经把你的全部代码都给了你,因为我不确定我哪里出错了。我确实尝试过poolDegreesMinpoolDegreesMax

2 个答案:

答案 0 :(得分:2)

我只看到解析itselve的一些问题。您解析txtTableAvgTemp.Text的文本。 使用新的温度线为每个while循环扩展此文本:

txtTableAvgTemp.Text += number.ToString() + "\r\n";

这永远不会解析。

此外,在第一个周期,文本为空("",而不是null)。这也会抛出FormatException。

首先使用0初始化文本框。如果您想稍后从中解析,请不要在文本框中添加新行。您应该使用临时双字段来存储最后的值。然后就不需要解析整个时间了。

// init with 0
txtTableDollars.Text = 0;
txtTableAvgTemp.Text = 0;
double currentCost = 0;

// do them outside, no need to do it on every loop
float heatingVolume = float.Parse(txtVolume.Text);

// working while
while (poolDegrees >= 3.5 && poolDegrees < 23)
{
    poolDegrees += poolTemp;

    double costToHeatPool = (25 - poolDegrees) * heatingVolume / 32500;

    currentCost += costToHeatPool;

    txtTableAvgTemp.Text += poolDegrees + System.Environment.NewLine;
    txtTableDollars.Text += currentCost + System.Environment.NewLine;
}

补充一点:我不确切地知道,但这里的循环看起来真的很可疑

while (poolTemp >= poolDegrees && poolTemp < poolDegrees) {/*...*/}

对我而言,这看起来像while(false)。这是有意的吗?永远不应该执行这个循环......

答案 1 :(得分:1)

嗯,在代码的开头有代码行:

txtTableDollars.Clear();

会清除文本框,所以稍后当您使用该文本时,显然它是空的:

poolCost = double.Parse(txtTableDollars.Text);

稍后在同一个循环中 - 您将其分配为

txtTableDollars.Text += poolCost + "\r\n";

所以如果你的循环不止一次执行 - 再次解析时会得到一些奇怪的结果(或异常)。

看起来您应该将计算与可视化分开。解析您的输入一次并使用数字进行计算,不要再次重新分析文本值。