Jay Lopez

Jay Lopez

  • NA
  • 4
  • 5.5k

How to handle a FormatException if the user inputs letters?

Sep 25 2016 10:54 PM
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 Project2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void convertTempButton_Click(object sender, EventArgs e)
{
int Fahrenheit;
int Celsius;
try
{
Fahrenheit = int.Parse(temperatureTextBox.Text);
}
catch (Exception ex)
{
MessageBox.Show("Please enter a Numeric Value");
}
Fahrenheit = int.Parse(temperatureTextBox.Text);
Celsius = (Fahrenheit - 32) * 5 / 9;
convertTempLabel.Text = Convert.ToString(Celsius) + " Celsius";
}
}
}

Answers (1)