Solved

# How do I pull the input from both comboboxes?

Posted on 2014-08-02
120 Views
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)
{
}

foreach (string str in measurements)
{
}

}

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
Question by:Johnney Jones

LVL 23

Expert Comment

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

LVL 29

Expert Comment

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"));

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

foreach (string str in measurements)
{
}

}

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

}
}
}
``````
0

LVL 29

Expert Comment

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

LVL 29

Accepted Solution

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"));

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

foreach (string str in measurements)
{

}

}

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)
{
}
if (str != selectedFrom)
{
}
}
cboFrom.SelectedItem = selectedFrom;
cboTo.SelectedItem = selectedTo;
}
}
}
``````
0

## Featured Post

### Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…