Quantcast
Channel: Project Customization and Programming forum
Viewing all articles
Browse latest Browse all 5347

C# Error handling and how to ignore a blank text box

$
0
0

I have coded a conversion programme that takes from 2 inputs and gives it as 2 outputs however I am having trouble with when one of the inputs is empty, and the programme just comes up with an error. I have tried certain ways but the code does not work when i make changes.

- Using Visual studio 2013

- C#

//Converting Button
        private void btnConvert_Click(object sender, EventArgs e)
        {

            double inputValue1 = Convert.ToDouble(txtInput1.Text);
            double inputValue2 = Convert.ToDouble(txtInput2.Text);

            double outputValue1 = 0;
            double outputValue2 = 0;

            if (rbPtoG.Checked == true)
            {

                //Using 2 inputs -> converting all to pounds
                double r = (inputValue2 / 16);
                double s = (inputValue1 + r);
                double p = (s / 2.2046);
                double q = ((s / 2.2046) % 1) * 1000;

                //Pounds to Kilograms
                outputValue1 = Math.Floor(p);
                outputValue2 = Math.Round(q, 5);

                lbOutput1.Text = outputValue1.ToString() + "  Kg";
                lbOutput2.Text = outputValue2.ToString() + "  g";
            }
            else
            {

                double w = (inputValue2 / 1000);
                double x = (inputValue1 + w);
                double z = (x * 2.2046);
                double y = ((x * 2.2046) % 1) * 16;
                //Kilograms to Pounds
                outputValue1 = Math.Floor(z);
                outputValue2 = Math.Round(y, 5);


                lbOutput1.Text = outputValue1.ToString() + "  lb";
                lbOutput2.Text = outputValue2.ToString() + "  Oz";
            }

            try
            {

            }

            catch (Exception ex)
                {
                throw ex;
                }
        }
    }
}



Viewing all articles
Browse latest Browse all 5347

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>