Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Globalization;
- namespace measurements
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- //
- // THIS IS WHERE MY CODE BEGINS
- //
- // indicate whether we are currently converting anything
- bool converting = false;
- // This helper method gets a float from a text box
- double get(TextBox textbox)
- {
- try
- {
- // InvariantCulture always wants . for decimal point
- string value = textbox.Text.Replace(",", ".");
- return double.Parse(
- value,
- NumberStyles.Float,
- CultureInfo.InvariantCulture);
- }
- catch
- {
- return 0;
- }
- }
- // This helper method displays a float in a text box
- void show(TextBox textbox, double value)
- {
- textbox.Text = Math.Round(value, 2).ToString();
- }
- //
- // The remaining two methods are triggered when a text box is modified
- //
- // Triggered whenever an imperial text box is edited
- void toMetric(object sender, EventArgs e)
- {
- // do not continue if we're already doing a conversion
- if (converting) return;
- converting = true;
- // get the feet and inches from the gui
- double inches = 12 * get(_feet) + get(_inch);
- // convert to centimeters and meters
- double centis = inches * 2.54;
- double meters = centis / 100;
- centis %= 100;
- // show conversion results in the gui
- show(_meter, (int)meters);
- show(_centi, centis);
- // conversion has finished, safe to re-enable signals
- converting = false;
- }
- // Triggered whenever a metric text box is edited
- private void toImperial(object sender, EventArgs e)
- {
- // do not continue if we're already doing a conversion
- if (converting) return;
- converting = true;
- // Get values from imerpail text boxes
- double centis = 100 * get(_meter) + get(_centi);
- // Conversions
- double inches = centis / 2.54;
- double feet = inches / 12;
- inches %= 12;
- // show conversion results in the gui
- show(_feet, (int)feet);
- show(_inch, inches);
- // can now do additional conversions
- converting = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment