Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 KB | None | 0 0
  1. public partial class MainWindow : Window
  2. {
  3. protected bool PriceFocused = true;
  4. //default values for price, provision, result, price w/o provision and tax
  5. double price = 0;
  6. double provision = 0;
  7. double result = 0;
  8. double valueWoProvision = 0;
  9. double tax = 0.23;
  10. public MainWindow()
  11. {
  12. InitializeComponent();
  13. taxValue();
  14. }
  15. //sets focus to Price textbox and enables it
  16. private void btnPrice_Click(object sender, RoutedEventArgs e)
  17. {
  18. if (PriceFocused == false)
  19. {
  20. PriceFocused = true;
  21. priceEnter.BorderBrush = Brushes.Red;
  22. priceEnter.BorderThickness = new Thickness(3);
  23. provisionEnter.BorderBrush = Brushes.Black;
  24. }
  25. }
  26. //show hint on Status Bar about Price button
  27. private void MouseEnterPriceArea(object sender, MouseEventArgs e)
  28. {
  29. statBarText.Text = "Set price.";
  30. }
  31. //sets focus to Provision textbox and enables it
  32. private void btnProvision_Click(object sender, RoutedEventArgs e)
  33. {
  34. if (PriceFocused == true)
  35. {
  36. PriceFocused = false;
  37. provisionEnter.BorderBrush = Brushes.Red;
  38. provisionEnter.BorderThickness = new Thickness(3);
  39. priceEnter.BorderBrush = Brushes.Black;
  40. }
  41. }
  42. //show hint on Status Bar about Provision button
  43. private void MouseEnterProvisionArea(object sender, MouseEventArgs e)
  44. {
  45. statBarText.Text = "Set provision. Provision has percentage value.";
  46. }
  47.  
  48. //click event for buttons
  49. private void One_Click(object sender, RoutedEventArgs e)
  50. {
  51. Button btn = sender as Button;
  52. var num = btn.Content.ToString();
  53. if (PriceFocused == true && priceEnter.Text.Length <=10)
  54. {
  55. priceEnter.Text += num;
  56. }
  57. if (PriceFocused == false && provisionEnter.Text.Length <=6)
  58. {
  59. provisionEnter.Text += num;
  60. }
  61. if (textBoxTax.IsFocused)
  62. {
  63. textBoxTax.Text += num;
  64. }
  65. }
  66. //if price textbox is changed, price is saved and calculation is done
  67. private void priceEnter_TextChangedTextboxPrice(object sender, TextChangedEventArgs e)
  68. {
  69. //if there's no price, there are no results!
  70. if (priceEnter.Text == string.Empty)
  71. {
  72. textBoxResults.Text = "PROVISION: " + "\nB: | N: " + "\nFOR CLIENT: ";
  73. }
  74. if (priceEnter.Text != string.Empty)
  75. {
  76. var priceStr = sender as TextBox;
  77. price = Convert.ToDouble(priceStr.Text);
  78. if (price != 0 && provision != 0 && tax != 0)
  79. {
  80. result = price * provision;
  81. valueWoProvision = price - result;
  82. textBoxResults.Text = "PROVISION: " + "\n" + "B: " + decimal.Round(Convert.ToDecimal(result), 2, MidpointRounding.AwayFromZero).ToString()
  83. + " | N: " + decimal.Round(Convert.ToDecimal(result - (result * tax)), 2, MidpointRounding.AwayFromZero).ToString() + "\nFOR CLIENT: " + "\n"
  84. + decimal.Round(Convert.ToDecimal(valueWoProvision), 2, MidpointRounding.AwayFromZero).ToString();
  85. }
  86. }
  87. }
  88. //if provision textbox is changed, provision is saved and calculation is done
  89. private void provisionEnter_TextChangedTextboxProvision(object sender, TextChangedEventArgs e)
  90. {
  91. //if there's no provision, there are no results!
  92. if (provisionEnter.Text == string.Empty)
  93. {
  94. textBoxResults.Text = "PROVISION: " + "\nB: | N: " + "\nFOR CLIENT: ";
  95. }
  96. if (provisionEnter.Text != string.Empty)
  97. {
  98. var provisionStr = sender as TextBox;
  99. provision = Convert.ToDouble(provisionStr.Text) / 100;
  100. if (price != 0 && provision != 0 && tax != 0)
  101. {
  102. result = price * provision;
  103. valueWoProvision = price - result;
  104. //if result is lower than 0, remove rast item
  105. while (valueWoProvision < 0)
  106. {
  107. MessageBox.Show("Result lower than 0!");
  108. provisionEnter.Text = provisionEnter.Text.Remove(provisionEnter.Text.Length - 1);
  109. }
  110. textBoxResults.Text = "PROVISION: " + "\n" + "B: " + decimal.Round(Convert.ToDecimal(result), 2, MidpointRounding.AwayFromZero).ToString()
  111. + " | N: " + decimal.Round(Convert.ToDecimal(result - (result * tax)), 2, MidpointRounding.AwayFromZero).ToString()
  112. + "\nFOR CLIENT: " + "\n" + decimal.Round(Convert.ToDecimal(valueWoProvision), 2, MidpointRounding.AwayFromZero).ToString();
  113. }
  114. }
  115. }
  116. //show actual tax value
  117. private void taxValue()
  118. {
  119. textBoxTax.Text = (tax * 100).ToString();
  120. }
  121. //calculate with updated tax
  122. private void taxChanged(object sender, TextChangedEventArgs e)
  123. {
  124. if (textBoxTax.Text == string.Empty)
  125. {
  126. textBoxResults.Text = "PROVISION: " + "\nB: | N: " + "\nFOR CLIENT: ";
  127. }
  128. if (textBoxTax.Text != string.Empty)
  129. {
  130. var taxStr = sender as TextBox;
  131. tax = Convert.ToDouble(taxStr.Text) / 100;
  132. if (price != 0 && provision != 0 && tax != 0)
  133. {
  134. result = price * provision;
  135. valueWoProvision = price - result;
  136. textBoxResults.Text = "PROVISION: " + "\n" + "B: " + decimal.Round(Convert.ToDecimal(result), 2, MidpointRounding.AwayFromZero).ToString()
  137. + " | N: " + decimal.Round(Convert.ToDecimal(result - (result * tax)), 2, MidpointRounding.AwayFromZero).ToString() + "\nFOR CLIENT: " + "\n"
  138. + decimal.Round(Convert.ToDecimal(valueWoProvision), 2, MidpointRounding.AwayFromZero).ToString();
  139. }
  140. }
  141. }
  142. //remove last number button
  143. private void remove_Click(object sender, RoutedEventArgs e)
  144. {
  145. if (PriceFocused == true && priceEnter.Text.Length > 0)
  146. {
  147. priceEnter.Text = priceEnter.Text.Remove(priceEnter.Text.Length - 1);
  148. }
  149. if (PriceFocused == true && priceEnter.Text.Length < 1)
  150. {
  151. priceEnter.Text = string.Empty;
  152. }
  153. if (PriceFocused == false && provisionEnter.Text.Length > 0)
  154. {
  155. provisionEnter.Text = provisionEnter.Text.Remove(provisionEnter.Text.Length - 1);
  156. }
  157. if (PriceFocused == false && provisionEnter.Text.Length < 1)
  158. {
  159. provisionEnter.Text = string.Empty;
  160. }
  161. }
  162. //clear all button
  163. private void clear_Click(object sender, RoutedEventArgs e)
  164. {
  165. priceEnter.Text = string.Empty;
  166. provisionEnter.Text = string.Empty;
  167. }
  168. //Hint about Change Tax button on Status Bar
  169. protected void MouseEnterTaxArea(object sender, MouseEventArgs e)
  170. {
  171. statBarText.Text = "Edit Tax value.";
  172. }
  173. //Default Status Bar Message
  174. protected void MouseLeaveArea(object sender, MouseEventArgs e)
  175. {
  176. statBarText.Text = "Ready";
  177. }
  178. //Hint about CE button on Status Bar
  179. private void MouseEnterClearArea(object sender, MouseEventArgs e)
  180. {
  181. statBarText.Text = "Clear all";
  182. }
  183. //Hint about Bsp button on Status Bar
  184. private void MouseEnterBackspaceArea(object sender, MouseEventArgs e)
  185. {
  186. statBarText.Text = "Delete last item.";
  187. }
  188. //Hint about Help menu on Status Bar
  189. private void MouseEnterHelpArea(object sender, MouseEventArgs e)
  190. {
  191. statBarText.Text = "Open Help.";
  192. }
  193. //Help click mode
  194. private void help_Click(object sender, RoutedEventArgs e)
  195. {
  196. MessageBox.Show("1. Click price or provision to check where you want to enter data.\n2. Use buttons or keyboard to enter your values. If you mistake, use Bsp button or backspace to clear last number or CE to clear everything.\n3. Calculation is real time so there's no need to press any button for calculation after you enter your numbers.\n4. You can change tax in tax window.\n5. B = provision value with Tax, N = provision value minus Tax.", "Help");
  197. }
  198. //only numbers allowed!
  199. private void PricePrevievInput(object sender, TextCompositionEventArgs e)
  200. {
  201. e.Handled = !IsTextAllowed(e.Text);
  202. }
  203. private void ProvisionPrevievInput(object sender, TextCompositionEventArgs e)
  204. {
  205. e.Handled = !IsTextAllowedWithDot(e.Text);
  206. }
  207. private static bool IsTextAllowed(string text)
  208. {
  209. Regex regex = new Regex("[^0-9]");
  210. return !regex.IsMatch(text);
  211. }
  212. private static bool IsTextAllowedWithDot(string text)
  213. {
  214. Regex regex = new Regex("[^0-9,]");
  215. return !regex.IsMatch(text);
  216. }
  217.  
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement