Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. public partial class MainWindow : Window
  2. {
  3. public MainWindow()
  4. {
  5. InitializeComponent();
  6. }
  7. int focus = 0;
  8. TextBox txt = new TextBox();
  9. List<Res> unitListForTemp = new List<Res>();
  10. List<TextBox> textBoxListForTemp = new List<TextBox>();
  11. Res unit = new Res();
  12. private void validateTextDouble(object sender)
  13. {
  14. Exception X = new Exception();
  15. TextBox T = (TextBox)sender;
  16. try
  17. {
  18. double x = double.Parse(T.Text.Replace(',', '.'), CultureInfo.InvariantCulture);
  19.  
  20. if (x < 0 || T.Text.Contains(','))
  21. throw X;
  22. }
  23. catch (Exception)
  24. {
  25. try
  26. {
  27. int CursorIndex = T.SelectionStart - 1;
  28. T.Text = T.Text.Remove(CursorIndex, 1);
  29.  
  30. T.SelectionStart = CursorIndex;
  31. T.SelectionStart = 0;
  32. }
  33. catch (Exception) { }
  34. }
  35. }
  36. public List<TextBox> AllTextBoxes(DependencyObject parent)
  37. {
  38. var list = new List<TextBox>();
  39. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
  40. {
  41. var child = VisualTreeHelper.GetChild(parent, i);
  42. if (child is TextBox)
  43. list.Add(child as TextBox);
  44. list.AddRange(AllTextBoxes(child));
  45. }
  46. return list;
  47. }
  48. public static List<Res> UnitConversion(Res from, double value, List<Res> unitList)
  49. {
  50. var list = unitList;
  51. double v = 0.0;
  52.  
  53. foreach (var unit in list)
  54. {
  55. v = (value - from.Shift) / from.Scale; // ТУТ ОШИБКА
  56. unit.Value = v * unit.Scale + unit.Shift;
  57. }
  58. return list;
  59. }
  60. public void Calculate(Res unit, List<Res> unitList, string text, List<TextBox> textBoxList, TextBox txt)
  61. {
  62. double value = 0.0;
  63.  
  64. if (txt.Text != string.Empty)
  65. {
  66. validateTextDouble(txt);
  67.  
  68. if (double.TryParse(text, out value))
  69. {
  70. value = Convert.ToDouble(text);
  71. TextBox textbox = new TextBox();
  72. var list = UnitConversion(unit, value, unitList);
  73. var itemToRemove = list.Where(x => x.Name == unit.Name).FirstOrDefault();
  74. list.Remove(itemToRemove);
  75. foreach (var item in list)
  76. {
  77. textbox = textBoxList.Where(x => x.Name == item.TextBoxName).FirstOrDefault();
  78. textbox.Text = item.Value.ToString();
  79. }
  80. list.Add(itemToRemove);
  81. }
  82. }
  83. else if (txt.Text == string.Empty)
  84. {
  85. textBoxList.Remove(txt);
  86. foreach (var textBox in textBoxList)
  87. {
  88. textBox.Text = string.Empty;
  89. }
  90. }
  91. }
  92. private void txtBox_GotFocus(object sender, RoutedEventArgs e)
  93. {
  94. switch (((TextBox)sender).Name)
  95. {
  96. case "txtCelcius":
  97. focus = 1;
  98. break;
  99. case "txtFahrenheit":
  100. focus = 2;
  101. break;
  102. case "txtKelvin":
  103. focus = 3;
  104. break;
  105. }
  106. }
  107. private void txtBox_TextChanged(object sender, TextChangedEventArgs e)
  108. {
  109. switch (focus)
  110. {
  111. case 1:
  112. txt = txtCelcius;
  113. unit = TemperatureUnit.list.Where(x => x.Name == "Celcius").FirstOrDefault();
  114. Calculate(unit, unitListForTemp, txt.Text, textBoxListForTemp, txt);
  115. break;
  116. case 2:
  117. txt = txtFahrenheit;
  118. unit = TemperatureUnit.list.Where(x => x.Name == "Fahrenheit").FirstOrDefault();
  119. Calculate(unit, unitListForTemp, txt.Text, textBoxListForTemp, txt);
  120. break;
  121. case 3:
  122. txt = txtKelvin;
  123. unit = TemperatureUnit.list.Where(x => x.Name == "Kelvin").FirstOrDefault();
  124. Calculate(unit, unitListForTemp, txt.Text, textBoxListForTemp, txt);
  125. break;
  126.  
  127. }
  128. }
  129.  
  130. private void Window_Loaded(object sender, RoutedEventArgs e)
  131. {
  132. unitListForTemp = TemperatureUnit.list;
  133. textBoxListForTemp = AllTextBoxes(grdTemp);
  134. }
  135. }
  136. public class Res
  137. {
  138. public string TextBoxName { get; set; }
  139. public string Name { get; set; }
  140. public double Scale { get; set; }
  141. public double Shift { get; set; }
  142. public string Type { get; set; }
  143. public double Value { get; set; }
  144. }
  145. public class TemperatureUnit
  146. {
  147. public static List<Res> list = new List<Res>()
  148. {
  149. new Res() { Name="Celcius",TextBoxName="txtCelcius",Scale = 1.0,Shift=0.0,Type="Temperature",Value=0 },
  150. new Res() { Name="Fahrenheit",TextBoxName="txtFahrenheit",Scale = 1.8,Shift=32,Type="Temperature",Value=0 },
  151. new Res() { Name="Kelvin",TextBoxName="txtKelvin",Scale = 1.0,Shift=273.15,Type="Temperature",Value=0 }
  152. };
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement