Share via


Try/catch statement if user enters a letter instead of a number?

Question

Sunday, November 3, 2013 8:17 AM

I have successfully made my first Windows Form Application in Visual Studio 2010 C#. It is just for practice.

Anyway, the program pops up a nice-looking form. It asks the user for a temperature in Fahrenheit, then they click Convert and it gives them the temperature in Celsius. Also, it has a space to ask for Celsius and then convert it to Fahrenheit. The form has a CLEAR and an EXIT button.

What I am having trouble with is adding a try/catch statement that displays a message to the user if they enter anything except a number in the text boxes. I keep getting tons of errors, so I removed the try/catch statement completely.

Please help!

Thank you.

Mitch Hean

p.s. Here is 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.Windows.Forms;

namespace TemperatureConversionMichelleHean
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // Close the form.
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            // Clear the text boxes.
            txtInputFahrenheit.Text = "";
            txtInputCelsius.Text = "";
            txtCelsius.Text = "";
            txtFahrenheit.Text = "";

            //Reset the focus to txtInputFahrenheit text box.
            txtInputFahrenheit.Focus();
        }

      
       
private void btnConvertToCelsius_Click(object sender, EventArgs e)
        {
        double Y; //declares Y.
            double X; //declares X.

            X = double.Parse(txtInputFahrenheit.Text); //Assigns value typed into text box as X.
            Y = 5.0 / 9.0 * (X - 32); //Converts Fahrenheit to Celsius.
            txtCelsius.Text = Y.ToString(); //Converts to string and displays in label.

        }

        private void btnConvertToFahrenheit_Click(object sender, EventArgs e)
        {
            double A; //declares A.
            double B; //declares B.

            A = double.Parse(txtInputCelsius.Text); //Assigns value typed into text box as A.
            B = A * 9 / 5 + 32; //Converts Celsius to Fahrenheit.
            txtFahrenheit.Text = B.ToString(); //Converts to string and displays in label.
        }
}

        
    }

All replies (4)

Sunday, November 3, 2013 9:00 AM âś…Answered | 1 vote

Try this:

try
{
    X = double.Parse( txtInputFahrenheit.Text );
}
catch( FormatException )
{
    MessageBox.Show( "The value is incorrect.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );

    txtInputFahrenheit.Focus();
    txtInputFahrenheit.SelectAll();
}

However you can use an if and double.TryParse instead. Then you do not need try-catch.


Sunday, November 3, 2013 12:49 PM

Where do I put this code? I tried it after namspace: TemperatureConversionMichelleHean, but it gave me tons of errors, and also the word "void", wherever it appeared, then had a red wavy line underneath it.

Thank you.

Mitch


Sunday, November 3, 2013 1:21 PM

I put the code directly after namespace, and here are the errors:

Error 1 { expected I:\Software Fundamentals\ASSIGNMENTS\Assignment 3\TemperatureConversionMichelleHean\TemperatureConversionMichelleHean\Form1.cs 10 44 TemperatureConversionMichelleHean

Error 2 A namespace cannot directly contain members such as fields or methods I:\Software Fundamentals\ASSIGNMENTS\Assignment 3\TemperatureConversionMichelleHean\TemperatureConversionMichelleHean\Form1.cs 12 9 TemperatureConversionMichelleHean

Thank you.

Mitch


Sunday, November 3, 2013 1:42 PM

It worked! I put it before the code that it applied to.

Thank you very much.

Mitch