mkv

FIXED

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