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.

I don't think it allows you to override the selection like that when it is databound. You could flip/flop the display member though:

private void comboBox1_DropDown(object sender, EventArgs e)
    {
      comboBox1.DisplayMember = "Name";
    }

    private void comboBox1_DropDownClosed(object sender, EventArgs e)
    {
      comboBox1.DisplayMember = "GravitationalAcceleration";
    }

Did not really solve my "problem" but you gave me hints to continue.
B.T.W. my design sucked:( By introducing an extra textbox (as in the MSDN example btw) I got the best of both worlds: a name and a value. This makes it clearer for the user anyway.
Thanks for the effort.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.