Guest User

Untitled

a guest
Feb 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. public enum PressUnit
  2. {
  3. [Description("Atm")]
  4. Atm = 1,
  5. [Description("Psi")]
  6. Psi = 2,
  7. [Description("Kpa")]
  8. Kpa = 3
  9. }
  10.  
  11. public class Press
  12. {
  13. public Press()
  14. {
  15. Valor = 1;
  16. Unidade = PressUnit.Atm;
  17. }
  18.  
  19. public PressUnit Unidade { get; private set; }
  20. public decimal Valor { get; private set; }
  21.  
  22. public void SetValor(decimal _valor, PressUnit _unidade)
  23. {
  24. this.Unidade = _unidade;
  25. this.Valor = _valor;
  26. }
  27.  
  28. public void SetValor(decimal _valor)
  29. {
  30. this.Valor = _valor;
  31. }
  32.  
  33. public bool ConvertTo(PressUnit _unidade)
  34. {
  35. decimal c = 0;
  36. if (TabelaAtm.TryGetValue(this.Unidade, out c))
  37. {
  38. decimal atm = this.Valor / c;
  39.  
  40. decimal d = 0;
  41. if (TabelaAtm.TryGetValue(_unidade, out d))
  42. {
  43. this.Unidade = _unidade;
  44. this.Valor = atm * d;
  45. return true;
  46. }
  47. else
  48. return false;
  49. }
  50. else return false;
  51. }
  52.  
  53. public static Dictionary<PressUnit, decimal> TabelaAtm = new Dictionary<PressUnit, decimal>()
  54. {
  55. {PressUnit.Atm,1},
  56. {PressUnit.Psi,(decimal)14.6959},
  57. {PressUnit.Kpa,(decimal)101.325}
  58. };
  59. }
  60.  
  61. static class Extension
  62. {
  63. public static string GetEnumDescription<TEnum>(this TEnum item)
  64. {
  65. DescriptionAttribute x = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false)
  66. .Cast<DescriptionAttribute>().FirstOrDefault();
  67. return x == null ? String.Empty : x.Description;
  68. }
  69. }
  70.  
  71. public partial class Form1 : Form
  72. {
  73. private Press objPress;
  74. public Form1()
  75. {
  76. InitializeComponent();
  77. objPress = new Press();
  78. //comboBox1.DataSource = Enum.GetValues(typeof(PressUnit));
  79. GetComboUnidades();
  80. }
  81. private void GetComboUnidades()
  82. {
  83. comboBox1.DisplayMember = "Description";
  84. comboBox1.ValueMember = "Value";
  85. comboBox1.DataSource = Enum.GetValues(typeof(PressUnit))
  86. .Cast<Enum>()
  87. .Select(value => new
  88. {
  89. Description = value.GetEnumDescription(),
  90. value
  91. })
  92. .OrderBy(item => item.value)
  93. .ToList();
  94. }
  95. private void Form1_Load(object sender, EventArgs e)
  96. {
  97. textBox1.Text = "0";
  98. }
  99.  
  100. private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
  101. {
  102. if (comboBox1.SelectedValue != null)
  103. {
  104. objPress.ConvertTo((PressUnit)comboBox1.SelectedValue);
  105. textBox1.Text = objPress.Valor.ToString("N3");
  106. }
  107. }
  108.  
  109. private void textBox1_TextChanged(object sender, EventArgs e)
  110. {
  111. if (!String.IsNullOrEmpty(textBox1.Text) && comboBox1.SelectedValue != null)
  112. {
  113. objPress.SetValor(Convert.ToDecimal(textBox1.Text), (PressUnit)comboBox1.SelectedValue);
  114. }
  115. }
  116. }
Add Comment
Please, Sign In to add comment