Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- int focus = 0;
- TextBox txt = new TextBox();
- List<Res> unitListForTemp = new List<Res>();
- List<TextBox> textBoxListForTemp = new List<TextBox>();
- Res unit = new Res();
- private void validateTextDouble(object sender)
- {
- Exception X = new Exception();
- TextBox T = (TextBox)sender;
- try
- {
- double x = double.Parse(T.Text.Replace(',', '.'), CultureInfo.InvariantCulture);
- if (x < 0 || T.Text.Contains(','))
- throw X;
- }
- catch (Exception)
- {
- try
- {
- int CursorIndex = T.SelectionStart - 1;
- T.Text = T.Text.Remove(CursorIndex, 1);
- T.SelectionStart = CursorIndex;
- T.SelectionStart = 0;
- }
- catch (Exception) { }
- }
- }
- public List<TextBox> AllTextBoxes(DependencyObject parent)
- {
- var list = new List<TextBox>();
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
- {
- var child = VisualTreeHelper.GetChild(parent, i);
- if (child is TextBox)
- list.Add(child as TextBox);
- list.AddRange(AllTextBoxes(child));
- }
- return list;
- }
- public static List<Res> UnitConversion(Res from, double value, List<Res> unitList)
- {
- var list = unitList;
- double v = 0.0;
- foreach (var unit in list)
- {
- v = (value - from.Shift) / from.Scale; // ТУТ ОШИБКА
- unit.Value = v * unit.Scale + unit.Shift;
- }
- return list;
- }
- public void Calculate(Res unit, List<Res> unitList, string text, List<TextBox> textBoxList, TextBox txt)
- {
- double value = 0.0;
- if (txt.Text != string.Empty)
- {
- validateTextDouble(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;
- }
- }
- }
- private void txtBox_GotFocus(object sender, RoutedEventArgs e)
- {
- switch (((TextBox)sender).Name)
- {
- case "txtCelcius":
- focus = 1;
- break;
- case "txtFahrenheit":
- focus = 2;
- break;
- case "txtKelvin":
- focus = 3;
- break;
- }
- }
- 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;
- }
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- unitListForTemp = TemperatureUnit.list;
- textBoxListForTemp = AllTextBoxes(grdTemp);
- }
- }
- public class Res
- {
- public string TextBoxName { get; set; }
- public string Name { get; set; }
- public double Scale { get; set; }
- public double Shift { get; set; }
- public string Type { get; set; }
- public double Value { get; set; }
- }
- public class TemperatureUnit
- {
- public static List<Res> list = new List<Res>()
- {
- new Res() { Name="Celcius",TextBoxName="txtCelcius",Scale = 1.0,Shift=0.0,Type="Temperature",Value=0 },
- new Res() { Name="Fahrenheit",TextBoxName="txtFahrenheit",Scale = 1.8,Shift=32,Type="Temperature",Value=0 },
- new Res() { Name="Kelvin",TextBoxName="txtKelvin",Scale = 1.0,Shift=273.15,Type="Temperature",Value=0 }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement