Pavlex4

Untitled

Oct 10th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. if (comboBox1.Text == "4")
  4. {
  5. if (textBox1.Text == "0")
  6. {
  7. MessageBox.Show("Otpornost mora biti veca od nule!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error);
  8. return;
  9. }
  10. if(comboBox2.Text=="")
  11. {
  12. MessageBox.Show("Izaberi vrednost tolerancije!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error);
  13. return;
  14. }
  15. var colours = new string[] { "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Grey","White", "Silver", "Gold" };
  16. var factorDictionary = new Dictionary<char, double>() { { 'p', 0.000000000001 }, { 'n', 0.000000001 }, { 'u', 0.000001 }, { 'm', 0.001 }, { 'R', 1 }, { 'K', 1000 }, { 'M', 1000000 }, { 'G', 1000000000 } };
  17. string res = string.Empty;
  18.  
  19. while (string.IsNullOrEmpty(res))
  20. {
  21. res = textBox1.Text;
  22. }
  23.  
  24. var lastChar = res.Last();
  25. var isUnitCorrect = factorDictionary.ContainsKey(lastChar);
  26. var value = res.Substring(0, res.Length - 1);
  27. var isValueCorrect = !value.Any(x => !char.IsDigit(x));
  28.  
  29. if (isUnitCorrect && isValueCorrect)
  30. {
  31. double mul = factorDictionary[lastChar];
  32. double val = double.Parse(value) * mul;
  33. int third = 0;
  34.  
  35. if (val < 0.1)
  36. {
  37. MessageBox.Show("Otpornost ne moze da bude manja od 0.1 oma!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error);
  38. return;
  39. }
  40. if (val < 1)
  41. {
  42. val *= 100;
  43. third = 10;
  44. }
  45. else if (val < 10)
  46. {
  47. val *= 10;
  48. third = 11;
  49. }
  50.  
  51. res = val.ToString();
  52.  
  53. if (res.Count() > 24)
  54. {
  55. MessageBox.Show("Pogresna vrednost!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error);
  56. return;
  57. }
  58.  
  59. else
  60. {
  61. label15.BackColor = Color.FromName(colours[res[0] - '0']);
  62. label16.BackColor = Color.FromName(colours[res[1] - '0']);
  63. label17.BackColor = Color.FromName(colours[third != 0 ? third : res.Count() - 2]);
  64. label6.Text = textBox1.Text;
  65. textBox2.Text = colours[res[0] - '0'];
  66. textBox3.Text = colours[res[1] - '0'];
  67. textBox4.Text = colours[third != 0 ? third : res.Count() - 2];
  68. }
  69. if (comboBox1.Text == "4")
  70. {
  71. if (comboBox2.Text == "E24 (5%)")
  72. {
  73. label18.BackColor = Color.Gold;
  74. textBox5.Text = "Gold";
  75. }
  76. if (comboBox2.Text == "E12 (10%)")
  77. {
  78. label18.BackColor = Color.Silver;
  79. textBox5.Text = "Silver";
  80. }
  81. }
  82. }
  83. else
  84. {
  85. MessageBox.Show("Invalid value!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error);
  86. return;
  87. }
  88. }
Add Comment
Please, Sign In to add comment