How to handle different combobox data at runtime

Asked

Viewed 323 times

1

I have a textfield and a combobox next to it...

Follow the image

inserir a descrição da imagem aqui

I want to make sure, given the value in the field textfield, I want you to click on other item of the combobox, the value is converted in the textfield for the corresponding item (the one chosen in the combo).

Someone can give me a north?

  • you have two events on the combobox: SelectedIndexChanged or SelectedValueChanged Just handle these events. If you want more details, enter the code as you fill your combobox

  • But before that I’m c a problem, on how to implement the class that makes the conversions...

  • 8796,3 H2O(g) > X oz(g) for example ?

  • 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.

  • Type, ...

  • 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

  • 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.

  • 1

    Some progress ?

  • 1

    Yes, I managed to. Thank you!

Show 4 more comments

1 answer

2


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.

  1. 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
    }
    
  2. 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}
        };
    }
    
  3. 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:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I hope it helps.

  • I’m not home right now to see this, tomorrow at work I’ll see what you’ve given me, but right away, this is exactly what I wanted. Anyway, thank you very much for your attention. Araços!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.