The question is not very specific, but it has been clarified in the comments.
Your real need would be to convert units by changing the value of the combobox.
Based on the past code, I made the following code, which includes only 3 units, but has support to add as many as needed.
Instead of defining the units as a string, we can define them as an enumerable:
public enum PressUnit
{
[Description("Atm")]
Atm = 1,
[Description("Psi")]
Psi = 2,
[Description("Kpa")]
Kpa = 3
}
Class itself, which performs the conversion calculation and has a dictionary with values indexed by the unit atm
:
public class Press
{
public Press()
{
Valor = 1;
Unidade = PressUnit.Atm;
}
public PressUnit Unidade { get; private set; }
public decimal Valor { get; private set; }
public void SetValor(decimal _valor, PressUnit _unidade)
{
this.Unidade = _unidade;
this.Valor = _valor;
}
public void SetValor(decimal _valor)
{
this.Valor = _valor;
}
public bool ConvertTo(PressUnit _unidade)
{
decimal c = 0;
if (TabelaAtm.TryGetValue(this.Unidade, out c))
{
decimal atm = this.Valor / c;
decimal d = 0;
if (TabelaAtm.TryGetValue(_unidade, out d))
{
this.Unidade = _unidade;
this.Valor = atm * d;
return true;
}
else
return false;
}
else return false;
}
public static Dictionary<PressUnit, decimal> TabelaAtm = new Dictionary<PressUnit, decimal>()
{
{PressUnit.Atm,1},
{PressUnit.Psi,(decimal)14.6959},
{PressUnit.Kpa,(decimal)101.325}
};
}
To use the Enum Description attribute more easily, I use this extension method:
static class Extension
{
public static string GetEnumDescription<TEnum>(this TEnum item)
{
DescriptionAttribute x = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false)
.Cast<DescriptionAttribute>().FirstOrDefault();
return x == null ? String.Empty : x.Description;
}
}
Utilizing:
Form1.Cs
public partial class Form1 : Form
{
private Press objPress;
public Form1()
{
InitializeComponent();
objPress = new Press();
//comboBox1.DataSource = Enum.GetValues(typeof(PressUnit));
GetComboUnidades();
}
private void GetComboUnidades()
{
comboBox1.DisplayMember = "Description";
comboBox1.ValueMember = "Value";
comboBox1.DataSource = Enum.GetValues(typeof(PressUnit))
.Cast<Enum>()
.Select(value => new
{
Description = value.GetEnumDescription(),
value
})
.OrderBy(item => item.value)
.ToList();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "0";
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedValue != null)
{
objPress.ConvertTo((PressUnit)comboBox1.SelectedValue);
textBox1.Text = objPress.Valor.ToString("N3");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(textBox1.Text) && comboBox1.SelectedValue != null)
{
objPress.SetValor(Convert.ToDecimal(textBox1.Text), (PressUnit)comboBox1.SelectedValue);
}
}
}
Upshot:
I hope it helps.
you have two events on the combobox:
SelectedIndexChanged
orSelectedValueChanged
Just handle these events. If you want more details, enter the code as you fill your combobox– Rovann Linhalis
But before that I’m c a problem, on how to implement the class that makes the conversions...
– Renan Narciso
8796,3 H2O(g) > X oz(g)
for example ?– Rovann Linhalis
Yes... Example: I want to put a value there in textField and then fill the value, I want to see how it will look if it is converted to another unit... Right by clicking on the combobox and selected another unit, I want you to bring the converted value, in which the unit was selected in the combo.
– Renan Narciso
Type, ...
– Renan Narciso
So, I think the question has to be different, and show the code that you have ready, which units would be and to advance the side of the people here, which is the calculation for conversion. Also inform if these units are fixed in the system, or can be registered
– Rovann Linhalis
Here what I have... https://ghostbin.com/paste/tn36t And these units are fixed in the system, there is no place where they are registered.
– Renan Narciso
Some progress ?
– Rovann Linhalis
Yes, I managed to. Thank you!
– Renan Narciso