Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Assessment5A
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.             //On initialization of the form, the following code will execute, and create three columns
  19.             //The below line of code adds the headers to each column respectively
  20.             lstChart.Items.Add("F" + "\t" + "C" + "\t" + "K");
  21.             double y;
  22.             double z;
  23.             //Loops code to fill in all three columns with the correct temperature calculations, rounding to one decimal place when outputted
  24.             for (double x = 32.0; x <= 212.0; x++)
  25.             {
  26.              y = (x - 32) * 5 / 9;
  27.              z = (y + 273);
  28.             lstChart.Items.Add((x.ToString()) + "\t" + (y.ToString("n1")) + "\t" + (z.ToString("n1")));
  29.         }
  30.             }
  31.         private void btnExit_Click(object sender, EventArgs e)
  32.             //Closes the application when the Exit button is clicked by the user
  33.         {
  34.             this.Close();
  35.         }
  36.  
  37.         private void btnCalc_Click(object sender, EventArgs e)
  38.         {
  39.             //When a value is entered, the program will calculate the Celsius and Kelvin conversions below, and display them
  40.             double userIn;
  41.             double celCon;
  42.             double kelCon;
  43.             if (double.TryParse(txtUser.Text, out userIn))
  44.             {
  45.                 celCon = (userIn - 32) * 5 / 9;
  46.                 kelCon = celCon + 273;
  47.                 lblOutCel.Text = (celCon.ToString("n1"));
  48.                 lblOutKel.Text = (kelCon.ToString("n1"));
  49.             }
  50.             else
  51.             {
  52.                 //If the user does not enter a valid number(double) value, as the code cannot parse it, will display below message and prompt a
  53.                 //different input; Data Validation
  54.                 MessageBox.Show("Please enter a valid number!");
  55.                 txtUser.Text = "";
  56.                 txtUser.Focus();
  57.             }
  58.         }
  59.  
  60.         private void btnReset_Click(object sender, EventArgs e)
  61.         {
  62.             //Resets all fields to clear
  63.             txtUser.Text = "";
  64.             txtUser.Focus();
  65.             lblOutKel.Text = "";
  66.             lblOutCel.Text = "";
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement