Question

C# Temperature Converter Application:

I have most of the application complete I just 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. Attached is the code and screenshot of the error message I managed to figured out. I need help with error messages 2 to 4.

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)

        {

          

        }

    }

}

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

As you have used following code snippet for first error condition:

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

if (celsiusTextBox.Text != "")

   {

                if (fahrenheitTextBox.Text != "")

                {

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

                }

   }

Similarly you can use multiple conditions with AND OR Logic and implement it as following:

Error Condition 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.”

Code

if (string.IsNullOrEmpty(celsiusTextBox.Text) && string.IsNullOrEmpty(fahrenheitTextBox.Text))

   {

                    messageLabel.Text = "You must enter the temperature that you want to convert. Press Clear to start over.";

                }

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

Note : All the date from the fields are fetched in String type, we need to convert it into numeric before comparison.
Code

if (celsiusTextBox.Text != "")

   {

long number = 0; // Variable to store Celsius Value in Long Datatype
  bool canConvertNum = long.TryParse(celsiusTextBox.Text, out number);
if(canConvertNum == false){

messageLabel.Text = "Characters are invalid. Press clear to start over."

}   

   }

if (fahrenheitTextBox.Text != "")

   {

long number = 0; // Variable to store Fahrenheit Value
  bool canConvertNum = long.TryParse(fahrenheitTextBox.Text, out number);
if(canConvertNum == false){

messageLabel.Text = "Characters are invalid. Press clear to start over."

}   

   }

Error Condition 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."

Code

long valueCelsius = 0; // Variable to store Celsius Value in Long Datatype
long valueFahrenheit = 0; // Variable to store Fahrenheit Value in Long Datatype

// Converting String Data to Long Data as String can not be compared to numeric value

bool canConvertNumCel = long.TryParse(celsiusTextBox.Text, out valueCelsius );

bool canConvertNumFah = long.TryParse(fahrenheitTextBox.Text, out valueFahrenheit );

if(canConvertNumCel == true && valueCelsius < -273.15){

messageLabel.Text = "You entered an invalid data. Press clear to start over."

}

if(canConvertNumFah == true && valueFahrenheit < -459.67){

messageLabel.Text = "You entered an invalid data. Press clear to start over."

}

Add a comment
Know the answer?
Add Answer to:
C# Temperature Converter Application: I have most of the application complete I just need only help...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C# Temp. Converter program. Create a Celsius and Fahrenheit Temperature Converter application. The application must have...

    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...

  • C# I am having trouble this program. So far my temperature converter program is converting temps...

    C# I am having trouble this program. So far my temperature converter program is converting temps from C to F. However, whenever I try to convert F to C, it's keep crashing and displaying an error message, which I can not figure it out and do not know how to fix it. Attached are two screenshots are of message. Do you know what is causing the issue? I would much appreciate your assistance. namespace Temp Conv { public partial class...

  • C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to...

    C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to create a Windows Form Application that will mimics a calculator. This is the screenshot how it shouldl look like. Calculator It should have a textbox and the following buttons: + Addition - Subtraction * Multiplication / Division = Equals (Will perform the final calculation) C Clear (Will clear the text box) There will be no maximize or minimize buttons. The rules are these: 2...

  • Need help with develop Visual C# Exercise 4-3 form to display the Fahrenheit temperature and corresponding...

    Need help with develop Visual C# Exercise 4-3 form to display the Fahrenheit temperature and corresponding Celsius temperature for Fahrenheit temperatures ranging from -40 to +40 degrees, at increments of 10. Use a for loop. The conversion formula is Celsius (Fahrenheit - 32) * 5/9. I ran the following but nothing is displaying in my listbox. I am not familiar with Form1_load, see below. I am not sure how to create in my form. Help please private void Form1_Load_Click(object sender,...

  • Hi everyone, For my Assignment I have to use C# in order to create a validation...

    Hi everyone, For my Assignment I have to use C# in order to create a validation form and include the following things to register and validate: Email Address, Username, Password, Retype Password, and a Submit Button. I have figured out some of the code for the Email Address validation: 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 Validation4 {     public partial class Form1 : Form     {        ...

  • What am I doing wrong on this? Chapter 17, Problem 3E // Michael Cole // 6/28/2017...

    What am I doing wrong on this? Chapter 17, Problem 3E // Michael Cole // 6/28/2017 // Name spaces for application using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace Week8 {     public partial class Form1: Form     {         // Get the location where the data file would be created         string fileLoc = Application.StartupPath +             "\\Datafile.txt";         /* Constructor of the class to create the GUI of the form */         public Form1()        ...

  • Use Kilometer Converter application code to write Temperature Converter application to convert degrees Fahrenheit into degrees...

    Use Kilometer Converter application code to write Temperature Converter application to convert degrees Fahrenheit into degrees Celsius ((F - 32)*5/9). It needs to be JavaFX 1 import javafx.application. Application; 2 import javafx.stage. Stage; 3 import javafx.scene. Scene; 4 import javafx.scene.layout.HBox; 5 import javafx.scene.layout. VBox; 6 import javafx.geometry.Pos; 7 import javafx.geometry.Insets; 8 import javafx.scene.control.Label; 9 import javafx.scene.control. TextField; 10 import javafx.scene.control.Button; 11 import javafx.event. EventHandler; 12 import javafx.event. ActionEvent; 13 14 ** 15 * Kilometer Converter application 16 17 18 public...

  • Program using visual basic.net You will create a very simple two numbers calculator with save options;...

    Program using visual basic.net You will create a very simple two numbers calculator with save options; here is the specifications for the application. Create a form divided vertically in two halves with right and left panels 1- In the left panel you will create the following control, the label or the value of the control will be in "" and the type of the control will in [] a- "First Number" [textbox] b- "Second number" [texbox] c- "Result" [textbox] -...

  • C#: I am working on this application and keep running into this error. Please help me fix it. Err...

    C#: I am working on this application and keep running into this error. Please help me fix it. Error: Design: 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 SinghAjayProgram08 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double totalInserted = 0; double totalWon = 0; private void spinButton_Click(object sender, EventArgs e) { Random rand = new Random(); int index = rand.Next(fruitsImageList.Images.Count); firstPictureBox.Image =...

  • This is just part of a C# windows form Application. When the user enters their information, checks the check box to save info and hits the payment button the information is saved to a .txt file. When...

    This is just part of a C# windows form Application. When the user enters their information, checks the check box to save info and hits the payment button the information is saved to a .txt file. When user enters last name and clicks autofill if name is found in file it will auto fill the boxes but i'm having issues with this part. For some reason it skips the firstname box and enters the last name in the first name...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT