Ejemplo n.º 1
0
        private void txtBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            switch (focus)
            {
                case 1:
                    txt = txtCelcius;
                    unit = TemperatureUnit.list.Where(x => x.Name == "Celcius").FirstOrDefault();
                    Calculate(unit, unitListForTemp, txt.Text, textBoxListForTemp, txt);
                    break;
                case 2:
                    txt = txtFahrenheit;
                    unit = TemperatureUnit.list.Where(x => x.Name == "Fahrenheit").FirstOrDefault();
                    Calculate(unit, unitListForTemp, txt.Text, textBoxListForTemp, txt);
                    break;
                case 3:
                    txt = txtKelvin;
                    unit = TemperatureUnit.list.Where(x => x.Name == "Kelvin").FirstOrDefault();
                    Calculate(unit, unitListForTemp, txt.Text, textBoxListForTemp, txt);
                    break;
                case 4:
                    txt = txtKilometer;
                    unit = LengthUnit.list.Where(x => x.Name == "Kilometer").FirstOrDefault();
                    Calculate(unit, unitListForLength, txt.Text, textBoxListForLength, txt);
                    break;
                case 5:
                    txt = txtMeter;
                    unit = LengthUnit.list.Where(x => x.Name == "Meter").FirstOrDefault();
                    Calculate(unit, unitListForLength, txt.Text, textBoxListForLength, txt);
                    break;
                case 6:
                    txt = txtCentimeter;
                    unit = LengthUnit.list.Where(x => x.Name == "Centimeter").FirstOrDefault();
                    Calculate(unit, unitListForLength, txt.Text, textBoxListForLength, txt);
                    break;
                case 7:
                    txt = txtMillimeter;
                    unit = LengthUnit.list.Where(x => x.Name == "Millimeter").FirstOrDefault();
                    Calculate(unit, unitListForLength, txt.Text, textBoxListForLength, txt);
                    break;
                case 8:
                    txt = txtLeague;
                    unit = LengthUnit.list.Where(x => x.Name == "League").FirstOrDefault();
                    Calculate(unit, unitListForLength, txt.Text, textBoxListForLength, txt);
                    break;
                case 9:
                    txt = txtMile;
                    unit = LengthUnit.list.Where(x => x.Name == "Mile").FirstOrDefault();
                    Calculate(unit, unitListForLength, txt.Text, textBoxListForLength, txt);
                    break;
                case 10:
                    txt = txtYard;
                    unit = LengthUnit.list.Where(x => x.Name == "Yard").FirstOrDefault();
                    Calculate(unit, unitListForLength, txt.Text, textBoxListForLength, txt);
                    break;
                case 11:
                    txt = txtInch;
                    unit = LengthUnit.list.Where(x => x.Name == "Inch").FirstOrDefault();
                    Calculate(unit, unitListForLength, txt.Text, textBoxListForLength, txt);
                    break;

            }
        }
Ejemplo n.º 2
0
        public static List<Unit> UnitConversion(Unit from, double value, List<Unit> unitList)
        {
            double v = 0.0;
            
            foreach (var unit in unitList)
            {
                v = (value - from.Shift) / from.Scale;
                unit.Value = v * unit.Scale + unit.Shift;

            }
            return unitList;
        }
Ejemplo n.º 3
0
        public void Calculate(Unit unit, List<Unit> unitList, string text, List<TextBox> textBoxList, TextBox txt)
        {
            double value = 0.0;


            if (txt.Text != string.Empty)
            {
                validateTextDouble(txt);
                validateClipboard(txt);
                if (double.TryParse(text, out value))
                {
                    value = Convert.ToDouble(text);
                    TextBox textbox = new TextBox();
                    var list = UnitConversion(unit, value, unitList);
                    var itemToRemove = list.Where(x => x.Name == unit.Name).FirstOrDefault();
                    list.Remove(itemToRemove);
                    foreach (var item in list)
                    {
                        textbox = textBoxList.Where(x => x.Name == item.TextBoxName).FirstOrDefault();
                        textbox.Text = item.Value.ToString();
                    }
                    list.Add(itemToRemove);
                }
            }
            else if (txt.Text == string.Empty)
            {
                textBoxList.Remove(txt);
                foreach (var textBox in textBoxList)
                {
                    textBox.Text = string.Empty;
                }
            }
        }