Question

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 Form1 : Form
{
double celsius;
double fahrenheit;

public Form1()
{
InitializeComponent();
}
public void ToCelsius(double fah)
{

celsius = (fah - 32) * (9 / 5);
celsiusTextBox.Text = celsius.ToString();
}

public void ToFahrenheit(double cel)
{

fahrenheit = (cel * (9 / 5)) + 32;
fahrenheitTextBox.Text = fahrenheit.ToString();
}

private void convertButton_Click(object sender, EventArgs e)
{
if (celsiusTextBox.Focus())
{
celsius = Convert.ToDouble(celsiusTextBox.Text);
ToFahrenheit(celsius);
}


if (fahrenheitTextBox.Focus())
{
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)
{
// Close the form.
this.Close();
}
}
}

37 38 39 40 41 42 fahrenheit-(cel * (9 / 5)) + 32; fahrenheitTextBox.Text - fahrenheit. ToString(); private void fahrenheitTextBox_TextChanged(object sender, EventArgs e) 45 46 47 48 49 50 51 52 53 54 private void convertButton_Click(object sender, EventArgs e) | if (celsiusTextBox. Focus( celsius Convert.ToDouble(celsiusTextBox.Text); toFahrenheit(celsius) 피x Exception Unhandled System.FormatException: Input string was not in a correct format. 56 57 58 59 60 61 62 63 64 65 View Details Copy Details > Exception Settings private void Form1_Load (object sender, EventArgs e)

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 Celsius Temperature Converter Fahrenheit Enter the value of the temperature that you want to convert next to its type name and press the Calculate button. Convert Clear Exit Celsius: ปั Fahrenhet: 80 Convert Clear Exit

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

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new windows forms application in C# is created using visual studio 2017 with name "TempConversion".This application contains a form with name "Form1.cs".Below are the files associated with this form.

1.Form1.cs[Design]

Temprature Converter Temprature Converter Enter the value of the temprature that you want to convert next to its type and press Celsius Fahrenheit Convert Clear Exit

2.Form1.Designer.cs

3.Form1.cs

//namespace
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;
//application namespace
namespace TempConversion
{
public partial class Form1 : Form
{
//declaring variables
double celsius;
double fahrenheit;
int flag;
public Form1()
{
InitializeComponent();
}
//method to convert from fahrenheit to celsius
public void ToCelsius(double fah)
{
//convert to celsius
celsius = (fah - 32) * 5 / 9;
//display celsius temp in the textbox
celsiusTextBox.Text = celsius.ToString("N2");
}
//method to convert from celsius to fahrenheit
public void ToFahrenheit(double cel)
{
//convert to fahrenheit
fahrenheit = (cel * 9 / 5) + 32;
//display fahrenheit temp in the textbox
fahrenheitTextBox.Text = fahrenheit.ToString("N2");
}
//convert button click
private void convertButton_Click(object sender, EventArgs e)
{
//checking value of flag
if (flag == 1)
{
//take user entered celsius temprature and parse it to double
celsius = Convert.ToDouble(celsiusTextBox.Text);
//call method and pass temp in celsius
ToFahrenheit(celsius);
}
//if flag is zero then
else if (flag == 0)
{
//take user entered fahrenheit temprature and parse it to double
fahrenheit = Convert.ToDouble(fahrenheitTextBox.Text);
//call method and pass temp in fahrenheit
ToCelsius(fahrenheit);
}
}
//Clear button click
private void clearButton_Click(object sender, EventArgs e)
{
//When this button clicked clear textboxes
celsiusTextBox.Text = "";
fahrenheitTextBox.Text = "";
}
//Exit button click
private void exitButton_Click(object sender, EventArgs e)
{
// Close the form.
this.Close();
}
//handling enter evnet for textbox celsiusTextBox
private void celsiusTextBox_Enter(object sender, EventArgs e)
{
flag = 1; //setting value of flag
}
//handling enter evnet for textbox fahrenheitTextBox
private void fahrenheitTextBox_Enter(object sender, EventArgs e)
{
//setting value of flag
flag = 0;
}
}
}

======================================================

Output : Run application using F5 and will get the screen as shown below

Screen 1 :

Temprature Converter Temprature Converter Enter the value of the temprature that you want to convert next to its type and press Celsius Fahrenheit Convert Clear Exit

Screen 2 :Screen when temp is entered in fahrenheit TextBox

Screen 3 :Screen showing temp in celsius

(골 Temprature Converter Temprature Converter Enter the value of the temprature that you want to convert next to its type and press Celsius 44.44 Fahrenheit 112 Convert Llear Exit

Screen 4 :Screen showing temp in fahrenheit

Temprature Converter Temprature Converter Enter the value of the temprature that you want to convert next to its type and press Celsius 80 176.00 Fahrenheit Convert Clear Exit

Note :In case of both textbox Focus() event returns true hence getting error.

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
C# I am having trouble this program. So far my temperature converter program is converting temps...
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# Temperature Converter Application: I have most of the application complete I just need only help...

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

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

  • so im trying to make a grade program and idk what im doing wrong actually if...

    so im trying to make a grade program and idk what im doing wrong actually if someone can help? (heres the lines of code i have so far if its wrong let me know how to fix them Option Strict On Option Explicit On Public Class Form1 Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click Dim lblscore1 As Double Dim lblscore2 As Double Dim lblscore3 As Double lblscore1 = CDbl(Txtscore1.Text) lblscore2 = CDbl(Txtscore2.Text) lblscore3 = CDbl(Txtscore3.Text) lblaverage =...

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

  • In java Se8 i am trying to write a program convert temperature between celsius, fahrenheit and...

    In java Se8 i am trying to write a program convert temperature between celsius, fahrenheit and kelvin, but i am stuck at how to return the proper result without chage the frame, and I cnould not figure out how to use those three settemp method. import java. uti1. Arrays public class Temperature different scale names/ public static Stringll scales -Celsius", "Fahrenheit", "Kelvin private double temperature private char scale; public Temperature (double temp temp 273. 15; this. scale-C if (temp <-273....

  • use this code of converting Km to miles , to create Temperature converter by using java...

    use this code of converting Km to miles , to create Temperature converter by using java FX import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.geometry.Pos; import javafx.geometry.Insets; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.control.Button; import javafx.event.EventHandler; import javafx.event.ActionEvent; /** * Kilometer Converter application */ public class KiloConverter extends Application { // Fields private TextField kiloTextField; private Label resultLabel; public static void main(String[] args) { // Launch the application. launch(args); } @Override public void start(Stage primaryStage) { //...

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

  • 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()        ...

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