Problem with ComboBox display

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Problem with ComboBox display

 
0
  #1
16 Days Ago
Hi all,
For a combobox I was trying to imitate the behaviour of this code example:http://msdn.microsoft.com/en-us/libr...luemember.aspx
So I have this class:
  1. class Planet
  2. {
  3. private string myName;
  4. private double myGravitationalAcceleration;
  5.  
  6. public Planet(string strName, double Acceleration)
  7. {
  8. this.myName = strName;
  9. this.myGravitationalAcceleration = Acceleration;
  10. }
  11.  
  12. public string Name
  13. {
  14. get
  15. {
  16. return myName;
  17. }
  18. }
  19.  
  20. public double GravitationalAcceleration
  21. {
  22. get
  23. {
  24. return myGravitationalAcceleration;
  25. }
  26. }
  27. }
And a form with a combobox on it and this code:
  1. public partial class InputData : Form
  2. {
  3. public InputData()
  4. {
  5. InitializeComponent();
  6. List<Planet> Planets = new List<Planet>();
  7. Planets.Add(new Planet("Earth at equator 0°", 9.78));
  8. Planets.Add(new Planet("Earth at equinox 23.5°", 9.788));
  9. Planets.Add(new Planet("Earth at lattitude 50°", 9.81));
  10. Planets.Add(new Planet("Earth at pole 90°", 9.83));
  11. Planets.Add(new Planet("Moon", 1.63));
  12. Planets.Add(new Planet("Mars", 3.69));
  13. Planets.Add(new Planet("Venus", 8.87));
  14. Planets.Add(new Planet("Titan", 1.352));
  15. GraviAccelCombo.DataSource = Planets;
  16. //add names of properties here, seems cool!
  17. GraviAccelCombo.DisplayMember = "Name";
  18. GraviAccelCombo.ValueMember = "GravitationalAcceleration";
  19.  
  20. }
  21.  
  22. //private void GraviAccelCombo_SelectedIndexChanged(object sender, EventArgs e)
  23. //{
  24. // GraviAccelCombo.Text = GraviAccelCombo.SelectedValue.ToString();
  25. //}
  26.  
  27. private void GraviAccelCombo_SelectedValueChanged(object sender, EventArgs e)
  28. {
  29. if (GraviAccelCombo.SelectedIndex != -1)
  30. {
  31. GraviAccelCombo.Text = GraviAccelCombo.SelectedValue.ToString();
  32. }
  33. }
  34. }

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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
16 Days Ago
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:
  1. private void comboBox1_DropDown(object sender, EventArgs e)
  2. {
  3. comboBox1.DisplayMember = "Name";
  4. }
  5.  
  6. private void comboBox1_DropDownClosed(object sender, EventArgs e)
  7. {
  8. comboBox1.DisplayMember = "GravitationalAcceleration";
  9. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
0
  #3
16 Days Ago
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.
Last edited by ddanbe; 16 Days Ago at 3:20 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

Tags
c#, combobox

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC