comboBox.SelectedItem/Index
-
Hi, I've got a combobox to which I add objects. I would like to have one specifiec object selected. Some of the objects contain the same DisplayMember but differ in other aspects = index. When the objects that do not have dublicates are selected there is no problem in selecting correct item as selected, but when the object has DisplayMember dublicates the first displayed items is always selected no matter what SelectedIndex or SelectedItem is assigned. Any tip?
ToDo
-
Hi, I've got a combobox to which I add objects. I would like to have one specifiec object selected. Some of the objects contain the same DisplayMember but differ in other aspects = index. When the objects that do not have dublicates are selected there is no problem in selecting correct item as selected, but when the object has DisplayMember dublicates the first displayed items is always selected no matter what SelectedIndex or SelectedItem is assigned. Any tip?
ToDo
I looked at DisplayMember under the ComboBox entry in MSDN for VS2005 and there was an example program (for ListBoxes) which I subtley changed to be a ComboBox. I also modified the sample data to contain duplicate DisplayMember values. It seems to work perfectly as far as I can tell, but maybe I am missing your point? Just copy and paste all this code into a WindowsApplication. HTH - Des using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; namespace WindowsApplication3 { public partial class Form1 : Form { //private ListBox ListBox1 = new ListBox(); // Just for our test private ComboBox ListBox1 = new ComboBox(); private TextBox textBox1 = new TextBox(); public Form1() { this.ClientSize = new Size(292, 181); this.Text = "ListBox Sample3"; ListBox1.Location = new Point(24, 16); ListBox1.Name = "ListBox1"; ListBox1.Size = new Size(232, 130); textBox1.Location = new Point(24, 160); textBox1.Name = "textBox1"; textBox1.Size = new Size(240, 24); this.Controls.AddRange(new Control[] { ListBox1, textBox1 }); // Populates the list box using DataSource. // DisplayMember is used to display just the long name of each state. ArrayList USStates = new ArrayList(); USStates.Add(new USState("Alabama", "AL")); USStates.Add(new USState("Washington", "WA")); USStates.Add(new USState("Washington", "W1")); // Duplicate DisplayMember USStates.Add(new USState("West Virginia", "WV")); USStates.Add(new USState("Wisconsin", "WI")); // Duplicate DisplayMember USStates.Add(new USState("Wisconsin", "W1")); USStates.Add(new USState("Wyoming", "WY")); ListBox1.SelectedValueChanged += new EventHandler(ListBox1_SelectedValueChanged); ListBox1.DataSource = USStates; ListBox1.DisplayMember = "LongName"; ListBox1.ValueMember = "ShortName"; } private void ListBox1_SelectedValueChanged(object sender, EventArgs e) { if (ListBox1.SelectedIndex != -1) textBox1.Text = ListBox1.SelectedValue.ToString(); } } public class USState { private string myShor
-
I looked at DisplayMember under the ComboBox entry in MSDN for VS2005 and there was an example program (for ListBoxes) which I subtley changed to be a ComboBox. I also modified the sample data to contain duplicate DisplayMember values. It seems to work perfectly as far as I can tell, but maybe I am missing your point? Just copy and paste all this code into a WindowsApplication. HTH - Des using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; namespace WindowsApplication3 { public partial class Form1 : Form { //private ListBox ListBox1 = new ListBox(); // Just for our test private ComboBox ListBox1 = new ComboBox(); private TextBox textBox1 = new TextBox(); public Form1() { this.ClientSize = new Size(292, 181); this.Text = "ListBox Sample3"; ListBox1.Location = new Point(24, 16); ListBox1.Name = "ListBox1"; ListBox1.Size = new Size(232, 130); textBox1.Location = new Point(24, 160); textBox1.Name = "textBox1"; textBox1.Size = new Size(240, 24); this.Controls.AddRange(new Control[] { ListBox1, textBox1 }); // Populates the list box using DataSource. // DisplayMember is used to display just the long name of each state. ArrayList USStates = new ArrayList(); USStates.Add(new USState("Alabama", "AL")); USStates.Add(new USState("Washington", "WA")); USStates.Add(new USState("Washington", "W1")); // Duplicate DisplayMember USStates.Add(new USState("West Virginia", "WV")); USStates.Add(new USState("Wisconsin", "WI")); // Duplicate DisplayMember USStates.Add(new USState("Wisconsin", "W1")); USStates.Add(new USState("Wyoming", "WY")); ListBox1.SelectedValueChanged += new EventHandler(ListBox1_SelectedValueChanged); ListBox1.DataSource = USStates; ListBox1.DisplayMember = "LongName"; ListBox1.ValueMember = "ShortName"; } private void ListBox1_SelectedValueChanged(object sender, EventArgs e) { if (ListBox1.SelectedIndex != -1) textBox1.Text = ListBox1.SelectedValue.ToString(); } } public class USState { private string myShor
Hi, thanks for the reply. Try chaning this code part, I get the the first Washington selected. // Populates the list box using DataSource. // DisplayMember is used to display just the long name of each state. ArrayList USStates = new ArrayList(); USState defaultSelection = new USState("Washington", "W1"); //New USStates.Add(new USState("Alabama", "AL")); USStates.Add(new USState("Washington", "WA")); USStates.Add(defaultSelection); // Duplicate DisplayMember //Changed USStates.Add(new USState("West Virginia", "WV")); USStates.Add(new USState("Wisconsin", "WI")); // Duplicate DisplayMember USStates.Add(new USState("Wisconsin", "W1")); USStates.Add(new USState("Wyoming", "WY")); ListBox1.SelectedValueChanged += new EventHandler(ListBox1_SelectedValueChanged); ListBox1.DataSource = USStates; ListBox1.DisplayMember = "LongName"; ListBox1.ValueMember = "ShortName"; ListBox1.SelectedItem = defaultSelection; //New Regards,
ToDo
-
Hi, thanks for the reply. Try chaning this code part, I get the the first Washington selected. // Populates the list box using DataSource. // DisplayMember is used to display just the long name of each state. ArrayList USStates = new ArrayList(); USState defaultSelection = new USState("Washington", "W1"); //New USStates.Add(new USState("Alabama", "AL")); USStates.Add(new USState("Washington", "WA")); USStates.Add(defaultSelection); // Duplicate DisplayMember //Changed USStates.Add(new USState("West Virginia", "WV")); USStates.Add(new USState("Wisconsin", "WI")); // Duplicate DisplayMember USStates.Add(new USState("Wisconsin", "W1")); USStates.Add(new USState("Wyoming", "WY")); ListBox1.SelectedValueChanged += new EventHandler(ListBox1_SelectedValueChanged); ListBox1.DataSource = USStates; ListBox1.DisplayMember = "LongName"; ListBox1.ValueMember = "ShortName"; ListBox1.SelectedItem = defaultSelection; //New Regards,
ToDo
Now I see what you mean, I originally thought that no matter which of the duplicates you selected, you were only getting the object mapped to the first duplicate returned, but I think what you are saying is that when you go to the ComboBox, the selected object is the first of the duplicates in the list. I haven't a way around this yet, but at least I now understand the problem.