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();
        }


    }
}
Johnney JonesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jens FiedererTest Developer/ValidatorCommented:
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_jimbelSenior DeveloperCommented:
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_jimbelSenior DeveloperCommented:
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_jimbelSenior DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.