mkv

metric to imperial

mkv
Jan 1st, 2015
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 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.Windows.Forms;
  9.  
  10. namespace measurements
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.  
  20.         //
  21.         // THIS IS WHERE MY CODE BEGINS
  22.         //
  23.        
  24.         // indicate whether we are currently converting anything
  25.         bool converting = false;
  26.        
  27.         // This helper method gets a float from a text box
  28.         double get(TextBox textbox)
  29.         {
  30.             try { return double.Parse(textbox.Text.Replace(",", ".")); } catch { }
  31.             try { return double.Parse(textbox.Text.Replace(".", ",")); } catch { }
  32.             return 0;
  33.         }
  34.  
  35.         // This helper method displays a float in a text box
  36.         void show(TextBox textbox, double value)
  37.         {
  38.             textbox.Text = Math.Round(value, 2).ToString();
  39.         }
  40.  
  41.  
  42.  
  43.         //
  44.         // The remaining two methods are triggered when a text box is modified
  45.         //
  46.  
  47.         // Triggered whenever an imperial text box is edited
  48.         void toMetric(object sender, EventArgs e)
  49.         {
  50.             // do not continue if we're already doing a conversion
  51.             if (converting) return;
  52.             converting = true;
  53.  
  54.             // get the feet and inches from the gui
  55.             double inches = 12 * get(_feet) + get(_inch);
  56.            
  57.             // convert to centimeters and meters
  58.             double centis = inches * 2.54;
  59.             double meters = centis / 100;
  60.             centis %= 100;
  61.  
  62.             // show conversion results in the gui
  63.             show(_meter, (int)meters);
  64.             show(_centi, centis);
  65.  
  66.             // conversion has finished, safe to re-enable signals
  67.             converting = false;
  68.         }
  69.  
  70.         // Triggered whenever a metric text box is edited
  71.         private void toImperial(object sender, EventArgs e)
  72.         {
  73.             // do not continue if we're already doing a conversion
  74.             if (converting) return;
  75.             converting = true;
  76.  
  77.             // Get values from imerpail text boxes
  78.             double centis = 100 * get(_meter) + get(_centi);
  79.  
  80.             // Conversions
  81.             double inches = centis / 2.54;
  82.             double feet = inches / 12;
  83.             inches %= 12;
  84.  
  85.             // show conversion results in the gui
  86.             show(_feet, (int)feet);
  87.             show(_inch, inches);
  88.  
  89.             // can now do additional conversions
  90.             converting = false;
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment