HOW TO CATCH EXCEPTION AND RESET WINDOWS FORM C#

Brad Pitt

Total Post:61

Points:429
Posted by  Brad Pitt
C#
 1112  View(s)
Ratings:
Rate this:

I'm trying to test if a user input is numeric, and if the input is not numeric, a little message box pops up and alerts the user. (I've done this part). The issue I'm having is the and error continues to pop in visual studio

Is this a formatting issue? Should I reformat my code? As you can probably see I am new the C# (and programming).

My code:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace wtgCalculator {

public partial class Form1 : Form {

   const double maleratio = 0.536;

   const double femaleratio = 0.492;

    public Form1() {

        InitializeComponent();

    }

    private void textBox1_TextChanged(object sender, EventArgs e) {

    }

    private void Form1_Load(object sender, EventArgs e) {

    }

    private void label1_Click(object sender, EventArgs e) {

    }

    private void textBox1_TextChanged_1(object sender, EventArgs e) {

    }

    private void WaistNumericCheck() {

        double waist;

        string inpwaist = heighttextbox.Text;

        if (double.TryParse(inpwaist, out waist)) {

            MessageBox.Show("Value must be numeric");      //this is where the problem is

        }                                   

    }

 

    private void HeightNumericCheck() {         

        //todo

    }

    private void button1_Click(object sender, EventArgs e) {

        //record inputs to variables

        WaistNumericCheck();

        HeightNumericCheck();

        double height = double.Parse(heighttextbox.Text);

        double waist = double.Parse(waisttextbox.Text);            

        //check is inputs are within boundry         

        CheckDimensions(height, waist);

        //test

       // ShowResult(height, waist);              

    }

    private void CheckLimits(double height, double waist) {

        double result = CalculateRatio(height, waist);

        if (Female.Checked) {

            if (result < femaleratio) {

                MessageBox.Show("Your risk of obesity related cardiovasular is low");

            }

            if (result > femaleratio) {

                MessageBox.Show("Your risk of obesity related to cardiovascular is high");

            }

        }

        if (Male.Checked) {

            if (result < maleratio) {

                MessageBox.Show("Your risk of obesity related cardiovasular is low");

            }

            if (result > maleratio) {

                MessageBox.Show("Your risk of obesity related cardiovasular is High");

            }

        }

        //testing

        MessageBox.Show(result.ToString());

    }

    private void ShowResult(double height, double waist) {

        //double result = CalculateRatio(height, waist);

        //if (Female.Checked) {

        //    if (result < femaleratio) {

        //        MessageBox.Show("Your risk of obesity related cardiovasular is low");

        //    }

        //    if (result > femaleratio) {

        //        MessageBox.Show("Your risk of obesity related to cardiovascular is high");

        //    }

        //}

        //if (Male.Checked) {

        //}

    }

    private static void CheckDimensions(double height, double waist) {

        if (height <= 120) {

            MessageBox.Show("Height must be greater than 120cm");

        }

        if (waist <= 60) {

            MessageBox.Show("Waist must be greater than 60cm");

        }   

    }

    private void Gender_CheckedChanged(object sender, EventArgs e) {

        button1.Enabled = true;                       

    }

    private static double CalculateRatio(double height, double waist) {

       double finalratio = waist / height;

       return finalratio;

    }

}

}

  1. Pawan Shukla

    Post:29

    Points:203
    Re: How to Catch exception and reset windows form C#



    You're not really handling this correctly. .Parse() is supposed to throw an exception if the value cannot be converted. The whole point of .TryParse() is to avoid that. So, your methods need to be cleaned up and edited to actually accomplish something:

    private bool WaistNumericCheck(out double waist)

    {

        bool canParse = double.TryParse(heighttextbox.Text, out waist);

     

        if ( !canParse ) {

            MessageBox.Show("Value must be numeric");

        } 

        return canParse;                     

    }

    private bool HeightNumericCheck(out double height)

    {         

        // todo -> same pattern as above

    }

    private void button1_Click(object sender, EventArgs e)

    {

        double height = 0, waist = 0;

        if( WaistNumericCheck(waist) && HeightNumericCheck(height) )

        {                   

            CheckDimensions(height, waist);

            /* ... the rest of your code */

        }             

    }

    This modifies your test methods to return a true/false based on whether the parse was successful, and outs your parsed variables so that you don't have to parse them again.

      Modified On Mar-17-2016 04:26:02 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!