Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. comboBox.SelectedItem/Index

comboBox.SelectedItem/Index

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasehelpquestion
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    T0D0
    wrote on last edited by
    #1

    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

    N 1 Reply Last reply
    0
    • T T0D0

      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

      N Offline
      N Offline
      NutSoft
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • N NutSoft

        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

        T Offline
        T Offline
        T0D0
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • T T0D0

          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

          N Offline
          N Offline
          NutSoft
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups