Pavlex4

Untitled

Oct 9th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 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. MessageBox.Show("Otpornost mora biti veca od nule", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error);
  7.  
  8. // var black = Color.Black;
  9.  
  10.  
  11.  
  12. var colours = new string[] { "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Grey", "Silver", "Gold" };
  13. 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 } };
  14. string res = string.Empty;
  15.  
  16. while (string.IsNullOrEmpty(res))
  17. {
  18. // MessageBox.Show("Enter resistor value (use R, K, M ,G notation for .): ");
  19. res = textBox1.Text;
  20. }
  21.  
  22. var lastChar = res.Last();
  23. var isUnitCorrect = factorDictionary.ContainsKey(lastChar);
  24. var value = res.Substring(0, res.Length - 1);
  25. var isValueCorrect = !value.Any(x => !char.IsDigit(x));
  26.  
  27. if (isUnitCorrect && isValueCorrect)
  28. {
  29. double mul = factorDictionary[lastChar];
  30. double val = double.Parse(value) * mul;
  31. int third = 0;
  32.  
  33. if (val < 1)
  34. {
  35. val *= 100;
  36. third = 9;
  37. }
  38. else if (val < 10)
  39. {
  40. val *= 10;
  41. third = 10;
  42. }
  43.  
  44. res = val.ToString();
  45.  
  46. if (res.Count() > 24)
  47. MessageBox.Show("Invalid value");
  48.  
  49. else
  50. {
  51. label15.BackColor = Color.FromName(colours[res[0] - '0']);
  52. label16.BackColor = Color.FromName(colours[res[1] - '0']);
  53. label17.BackColor = Color.FromName(colours[third != 0 ? third : res.Count() - 2]);
  54. }
  55. if (comboBox1.Text == "4")
  56. {
  57. if (comboBox2.Text == "E24 (5%)")
  58. label18.BackColor = Color.Gold;
  59. if (comboBox2.Text == "E12 (10%)")
  60. label18.BackColor = Color.Silver;
  61. }
  62. }
  63. else
  64. MessageBox.Show("Invalid value!");
  65. }
Add Comment
Please, Sign In to add comment