[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 142
  • Last Modified:

How do I pull the input from both comboboxes?

I'm not sure where to go next for this project. The calculate button should use the conversion calculations, but I'm not quite sure how to go about it, and I can't find any relevant answers since its pulling the data from two combo boxes. Also, the comboboxes should change based on what the user picks. So, for example, if the user chooses "in" from the first combobox, they should not be able to choose "in" from the second combobox. If someone could point me in the right direction, I would be very appreciative.

public partial class Form1 : Form
    {
        const double mi_To_km = 1.6093;
        const double Km_To_mi = 0.6214;
        const double ft_To_m = 0.3048;
        const double m_To_ft = 3.2808;
        const double in_To_cm = 2.54;
        const double cm_To_in = 0.3937;
        const double ft_To_mi = 1 / mi_To_ft;
        const double mi_To_ft = 5280;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            cboFrom.Items.Clear(); //clear the combobox

            string[,] measurements =  
            {  {"in"}, {"ft"}, {"mi"}, {"cm"}, {"m"}, {"km"} };


            foreach (string str in measurements)
            {
                cboFrom.Items.Add(str);
            }
           
            foreach (string str in measurements)
            {
                cboTo.Items.Add(str);
            }

        }

        public bool IsDecimal(TextBox txtEntry, string entry)
        {
            try
            {
                Convert.ToDecimal(txtEntry.Text);
                return true;
            }

            catch (FormatException)
            {
                MessageBox.Show(entry + " must be a decimal value.", "Entry Error");
                txtEntry.Focus();
                return false;
            }
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
           
            int from = cboFrom.SelectedIndex;
            int to = cboTo.SelectedIndex;


        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            foreach (var c in this.Controls)
            {
                if (c is TextBox)
                {
                    ((TextBox)c).Text = String.Empty;
                }
            }
            cboFrom.SelectedIndex = 0;
            cboTo.SelectedIndex = 0;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
0
Johnney Jones
Asked:
Johnney Jones
  • 3
1 Solution
 
Jens FiedererCommented:
One way to do this is with a Dictionary of Dictionaries....

Dictionary <string, Dictionary<string, double>> lookup =  new Dictionary <string, Dictionary<string, double>>();


Dictionary<string, double> fromMiles = new Dictionary<string, double>();
fromMiles["mi_To_km"] = 1.6093;
fromMiles["mi_To_ft"] = 1.6093;

lookup["mi"] = fromMiles;

(note that this would need to be exhaustive, you'd have to calculate "mi_to_cm" and such too)

Then you can do lookup[fromComboBoxValue][toComboBoxValue] with your combo boxes....
0
 
anarki_jimbelCommented:
If it would be on me I'd add all coefficients to xml file, e.g. Or to a datatable.

Lets consider a simple example with a datatable in memory. I create and fill a table when loading a form.
A table has a stricture something like:

================================
from    |    to     |     cc (conversion coefficient)
================================
mi        |   km    |    1.6093
----------------------------------------------------


And so on. You just select 'from' and 'to' from the combo boxes and get a coefficient from the table. Code is simple. Of course, this is just example code (no conversions done :)):

namespace Converter
{
    public partial class Form1 : Form
    {
        const double mi_To_km = 1.6093;
        const double Km_To_mi = 0.6214;
        const double ft_To_m = 0.3048;
        const double m_To_ft = 3.2808;
        const double in_To_cm = 2.54;
        const double cm_To_in = 0.3937;
        const double ft_To_mi = 1 / mi_To_ft;
        const double mi_To_ft = 5280;
        private DataTable conversions = new DataTable();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[,] measurements = { { "in" }, { "ft" }, { "mi" }, { "cm" }, { "m" }, { "km" } };

            DataColumn dc1 = new DataColumn("from", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("to", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("cc", Type.GetType("System.Double"));
            conversions.Columns.Add(dc1);
            conversions.Columns.Add(dc2);
            conversions.Columns.Add(dc3);

            // for the example I add only one row
            DataRow dr = conversions.NewRow();
            dr["from"] = "mi";
            dr["to"] = "km";
            dr["cc"] = 1.6093;

            conversions.Rows.Add(dr);

            foreach (string str in measurements)
            {
                cboFrom.Items.Add(str);
                cboTo.Items.Add(str);
            }

        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            string from = cboFrom.SelectedItem.ToString();
            string to = cboTo.SelectedItem.ToString();

            DataRow[] drs = conversions.Select("from = 'mi'");
            if (drs.Length > 0)
            {
                double coefficient = (double)drs[0]["cc"];

                // read numbers and do calculations here
            }
            else
            {
                MessageBox.Show("No suitable coefficient");
            }

        }
    }
}

Open in new window

0
 
anarki_jimbelCommented:
How to populate and re-populate comboboxes is a bit different story. One solution is to allow "km" --> "km" conversions, just make a coefficient = "1".

If you nee a more sophisticated solution just let know...
0
 
anarki_jimbelCommented:
I've added simple code to my solution (combo click event handlers)  to populate combos as you want. Notice that initially I add empty string to  combos and no items are selected. You may play around this. Depends on what you want exactly.

namespace Converter
{
    public partial class Form1 : Form
    {
        const double mi_To_km = 1.6093;
        const double Km_To_mi = 0.6214;
        const double ft_To_m = 0.3048;
        const double m_To_ft = 3.2808;
        const double in_To_cm = 2.54;
        const double cm_To_in = 0.3937;
        const double ft_To_mi = 1 / mi_To_ft;
        const double mi_To_ft = 5280;
        private DataTable conversions = new DataTable();

        string[] measurements = { "in", "ft", "mi", "cm", "m", "km" };

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //string[,] measurements = { { "in" }, { "ft" }, { "mi" }, { "cm" }, { "m" }, { "km" } };

            DataColumn dc1 = new DataColumn("from", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("to", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("cc", Type.GetType("System.Double"));
            conversions.Columns.Add(dc1);
            conversions.Columns.Add(dc2);
            conversions.Columns.Add(dc3);

            // for the example I add only one row
            DataRow dr = conversions.NewRow();
            dr["from"] = "mi";
            dr["to"] = "km";
            dr["cc"] = 1.6093;

            conversions.Rows.Add(dr);

            cboFrom.Items.Add("");
            cboTo.Items.Add("");
            foreach (string str in measurements)
            {

                cboFrom.Items.Add(str);
                cboTo.Items.Add(str);
            }

        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            string from = cboFrom.SelectedItem.ToString();
            string to = cboTo.SelectedItem.ToString();

            DataRow[] drs = conversions.Select("from = 'mi'");
            if (drs.Length > 0)
            {
                double coefficient = (double)drs[0]["cc"];

                // read numbers and do calculations here
            }
            else
            {
                MessageBox.Show("No suitable coefficient");
            }

        }

        //    private void fillCombo(ComboBox cb, 
        //}


        private void cboFrom_Click(object sender, EventArgs e)
        {
            setCombos();
        }

        private void cboTo_Click(object sender, EventArgs e)
        {
            setCombos();
        }

        private void setCombos()
        {
                        string selectedFrom = (string)cboFrom.SelectedItem;
            string selectedTo = (string)cboTo.SelectedItem;
            cboFrom.Items.Clear();
            cboTo.Items.Clear();
            foreach (string str in measurements)
            {
                if (str != selectedTo)
                {
                    cboFrom.Items.Add(str);
                }
                if (str != selectedFrom)
                {
                    cboTo.Items.Add(str);
                }
            }
            cboFrom.SelectedItem = selectedFrom;
            cboTo.SelectedItem = selectedTo;
        }
    }
}

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now