Hi all,
For a combobox I was trying to imitate the behaviour of this code example:http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.valuemember.aspx
So I have this class:
class Planet
{
private string myName;
private double myGravitationalAcceleration;
public Planet(string strName, double Acceleration)
{
this.myName = strName;
this.myGravitationalAcceleration = Acceleration;
}
public string Name
{
get
{
return myName;
}
}
public double GravitationalAcceleration
{
get
{
return myGravitationalAcceleration;
}
}
}
And a form with a combobox on it and this code:
public partial class InputData : Form
{
public InputData()
{
InitializeComponent();
List<Planet> Planets = new List<Planet>();
Planets.Add(new Planet("Earth at equator 0°", 9.78));
Planets.Add(new Planet("Earth at equinox 23.5°", 9.788));
Planets.Add(new Planet("Earth at lattitude 50°", 9.81));
Planets.Add(new Planet("Earth at pole 90°", 9.83));
Planets.Add(new Planet("Moon", 1.63));
Planets.Add(new Planet("Mars", 3.69));
Planets.Add(new Planet("Venus", 8.87));
Planets.Add(new Planet("Titan", 1.352));
GraviAccelCombo.DataSource = Planets;
//add names of properties here, seems cool!
GraviAccelCombo.DisplayMember = "Name";
GraviAccelCombo.ValueMember = "GravitationalAcceleration";
}
//private void GraviAccelCombo_SelectedIndexChanged(object sender, EventArgs e)
//{
// GraviAccelCombo.Text = GraviAccelCombo.SelectedValue.ToString();
//}
private void GraviAccelCombo_SelectedValueChanged(object sender, EventArgs e)
{
if (GraviAccelCombo.SelectedIndex != -1)
{
GraviAccelCombo.Text = GraviAccelCombo.SelectedValue.ToString();
}
}
}
What I want is: the user selects a planet(yeah there are moons too) name and in the display area of the combo I like to display the gravitational acceleration value. So if I select "Venus" from the dropdownlist, GraviAccelCombo.Text is equal to 8.87, but the word "Venus" gets still displayed instead of "8.87". Have a feeling it must be something very obvious, but I have no clue what so ever.
Any help is again greatly appreciated.