Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C# Temp. Converter program. Create a Celsius and Fahrenheit Temperature Converter application. The application must have 2 Labels (each one of them assigned only to

C# Temp. Converter program.

Create a Celsius and Fahrenheit Temperature Converter application. The application must have 2 Labels (each one of them assigned only to Celsius and Fahrenheit), 2 TextBoxes (each one of them assigned only to Celsius and Fahrenheit), only 1 Convert Button, 1 Clear Button, and 1 Exit Button. Also, 1 Error message label which will display the error messages.

The application should only allow the user to enter the desired temperature just in one of the TextBoxes and display the result on the other TextBox. For instance, when the user enters a temperature in Celsius TextBox, and after clicking the Convert button, the converted temperature should display in a Fahrenheit TextBox or vice versa.

I have most of the application complete I need only help with error messages.

My instructor wants me to show four error messages inside an error message label (not a message box) and all of the error messages should appear inside error message label. The four error messages should appear when:

1: User enters data inside both Celsius and fahrenheit text boxes: You should enter data in only one textbox. Press Clear to start over.

2: User leaves text boxes empty and tries to convert: You must enter the temperature that you want to convert. Press Clear to start over.

3: User enters any character that is not a number: "Characters are invalid. Press clear to start over."

4: User enters data below absolute zero (If the Celsius temperature is less than -273.15, Fahrenheit less than -459.67): "You entered an invalid data. Press clear to start over."

So far, I managed to get only 1st error message work and not being able to figure out the rest. Please help me.

public partial class Form1 : Form

{

double celsius;

double fahrenheit;

int flag;

public Form1()

{

InitializeComponent();

}

public void ToCelsius(double fah)

{

celsius = (fah - 32) * 5 / 9;

celsiusTextBox.Text = celsius.ToString("N2");

}

public void ToFahrenheit(double cel)

{

fahrenheit = (cel * 9 / 5) + 32;

fahrenheitTextBox.Text = fahrenheit.ToString("N2");

}

private void convertButton_Click(object sender, EventArgs e)

{

if (celsiusTextBox.Text != "")

{

if (fahrenheitTextBox.Text != "")

{

messageLabel.Text = "You should enter data in only one textbox. Press Clear to start over.";

}

}

if (flag == 1)

{

celsius = Convert.ToDouble(celsiusTextBox.Text);

ToFahrenheit(celsius);

}

else if (flag == 0)

{

fahrenheit = Convert.ToDouble(fahrenheitTextBox.Text);

ToCelsius(fahrenheit);

}

}

private void clearButton_Click(object sender, EventArgs e)

{

celsiusTextBox.Text = "";

fahrenheitTextBox.Text = "";

}

private void exitButton_Click(object sender, EventArgs e)

{

this.Close();

}

private void celsiusTextBox_TextChanged(object sender, EventArgs e)

{

flag = 1;

}

private void fahrenheitTextBox_TextChanged(object sender, EventArgs e)

{

flag = 0;

}

private void messageLabel_Click(object sender, EventArgs e)

{

}

}

}

image text in transcribed

Temperature Converter 1 23 Temperature Converter Temperature Converter Enter the value of the temperature that you want to convert next to its type name and press the Calculate button. Temperature Converter Enter the value of the temperature that you want to convent next to its type name and press the Calculate button. Celsius Fahrenheit: Celsius: 13.33 Fahrenheit 56 Convert Clear Exit Convert Clear Exit You should enter data in only one textbox. Press Clear to start over

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions