944,191 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1516
  • C# RSS
Nov 7th, 2009
0

Problem with ComboBox display

Expand Post »
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:
c# Syntax (Toggle Plain Text)
  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:
c# Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Nov 7th, 2009
0
Re: Problem with ComboBox display
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:
C# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 7th, 2009
0
Re: Problem with ComboBox display
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; Nov 7th, 2009 at 3:20 pm.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: lines in a text file
Next Thread in C# Forum Timeline: Exception:remote connection to database is not allowed !!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC