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. C#
  4. List Box showing Value and Display members?

List Box showing Value and Display members?

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 4 Posters 0 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.
  • U Offline
    U Offline
    uniflare
    wrote on last edited by
    #1

    Ok this is a CONCEPT problem (the code is not professional, just read it for the concept, though it does compile and run). Right, I have asked many people on IRC and no one is giving me a straight answer lol so maybe some professionals can help me. I am trying to use a Datasource property for my combobox. This is so I can just pass an entire KeyValue pair with indexes and values to be shown in the listbox. Creating the keyvaluepair as a WHOLE and passing that to datasource property works perfectly. Creating a keyvaluepair PROCEDURALLY (in a loop or w/e) ends in the datasource property ToString()ing it, list box items show BOTH value AND display members in square brackets as an entire option. NOTE: Getting the lsitbox.value results in ONLY the valuemember being returned even though the listbox shows both the valuemembers AND the displaymember? it knows they are different? Now, I dont want any alternatives or comments about the quality of the code, all I want to know is WHY is it doing this, it seems compeltely illogical that the same TYPE and Values and can different Effects within the datasource property? Its like it knows it was built procedurally? Anyway here is some code for reference:

    // Windows Form

    namespace skbtInstaller
    {
    public partial class frmMainWindow : Form
    {
    public frmMainWindow()
    {
    InitializeComponent();

        // coxArmaPath = Combo Box (simple drop down)
            this.cBoxArmaPath.ValueMember = "Key";
            this.cBoxArmaPath.DisplayMember = "Value";
        // ....
        }
    
        public void addServerPathItem(String Key, String Value)
    {
            this.cBoxArmaPath.Items.Add(new KeyValuePair(Key,Value));
        // This works fine, only the "DisplayMember" is displayed and the "ValueMember" is hidden.
        }
    
    // This acts differently with the same types???
        public void setServerPathDatasource(List\> source)
        {
            this.cBoxArmaPath.DataSource = new BindingSource(source, null);
        }
    }
    
    public class skbtServerControl
    {
        private frmMainWindow frmMainWindowHandle;
        public void refreshformWindow()
        {
            // Clear Drop Box (empties drop box)
            this.frmMainWindowHandle.clearPathBox();
    
        //skbtServerConfig is very simple property object
            foreach (KeyValuePair pair in CoreConfig.getServerConfigList())
            {
    
    Richard Andrew x64R P S 3 Replies Last reply
    0
    • U uniflare

      Ok this is a CONCEPT problem (the code is not professional, just read it for the concept, though it does compile and run). Right, I have asked many people on IRC and no one is giving me a straight answer lol so maybe some professionals can help me. I am trying to use a Datasource property for my combobox. This is so I can just pass an entire KeyValue pair with indexes and values to be shown in the listbox. Creating the keyvaluepair as a WHOLE and passing that to datasource property works perfectly. Creating a keyvaluepair PROCEDURALLY (in a loop or w/e) ends in the datasource property ToString()ing it, list box items show BOTH value AND display members in square brackets as an entire option. NOTE: Getting the lsitbox.value results in ONLY the valuemember being returned even though the listbox shows both the valuemembers AND the displaymember? it knows they are different? Now, I dont want any alternatives or comments about the quality of the code, all I want to know is WHY is it doing this, it seems compeltely illogical that the same TYPE and Values and can different Effects within the datasource property? Its like it knows it was built procedurally? Anyway here is some code for reference:

      // Windows Form

      namespace skbtInstaller
      {
      public partial class frmMainWindow : Form
      {
      public frmMainWindow()
      {
      InitializeComponent();

          // coxArmaPath = Combo Box (simple drop down)
              this.cBoxArmaPath.ValueMember = "Key";
              this.cBoxArmaPath.DisplayMember = "Value";
          // ....
          }
      
          public void addServerPathItem(String Key, String Value)
      {
              this.cBoxArmaPath.Items.Add(new KeyValuePair(Key,Value));
          // This works fine, only the "DisplayMember" is displayed and the "ValueMember" is hidden.
          }
      
      // This acts differently with the same types???
          public void setServerPathDatasource(List\> source)
          {
              this.cBoxArmaPath.DataSource = new BindingSource(source, null);
          }
      }
      
      public class skbtServerControl
      {
          private frmMainWindow frmMainWindowHandle;
          public void refreshformWindow()
          {
              // Clear Drop Box (empties drop box)
              this.frmMainWindowHandle.clearPathBox();
      
          //skbtServerConfig is very simple property object
              foreach (KeyValuePair pair in CoreConfig.getServerConfigList())
              {
      
      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      uniflare wrote:

      comments about the quality of the code

      No worries, mate. We've seen worse!

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • U uniflare

        Ok this is a CONCEPT problem (the code is not professional, just read it for the concept, though it does compile and run). Right, I have asked many people on IRC and no one is giving me a straight answer lol so maybe some professionals can help me. I am trying to use a Datasource property for my combobox. This is so I can just pass an entire KeyValue pair with indexes and values to be shown in the listbox. Creating the keyvaluepair as a WHOLE and passing that to datasource property works perfectly. Creating a keyvaluepair PROCEDURALLY (in a loop or w/e) ends in the datasource property ToString()ing it, list box items show BOTH value AND display members in square brackets as an entire option. NOTE: Getting the lsitbox.value results in ONLY the valuemember being returned even though the listbox shows both the valuemembers AND the displaymember? it knows they are different? Now, I dont want any alternatives or comments about the quality of the code, all I want to know is WHY is it doing this, it seems compeltely illogical that the same TYPE and Values and can different Effects within the datasource property? Its like it knows it was built procedurally? Anyway here is some code for reference:

        // Windows Form

        namespace skbtInstaller
        {
        public partial class frmMainWindow : Form
        {
        public frmMainWindow()
        {
        InitializeComponent();

            // coxArmaPath = Combo Box (simple drop down)
                this.cBoxArmaPath.ValueMember = "Key";
                this.cBoxArmaPath.DisplayMember = "Value";
            // ....
            }
        
            public void addServerPathItem(String Key, String Value)
        {
                this.cBoxArmaPath.Items.Add(new KeyValuePair(Key,Value));
            // This works fine, only the "DisplayMember" is displayed and the "ValueMember" is hidden.
            }
        
        // This acts differently with the same types???
            public void setServerPathDatasource(List\> source)
            {
                this.cBoxArmaPath.DataSource = new BindingSource(source, null);
            }
        }
        
        public class skbtServerControl
        {
            private frmMainWindow frmMainWindowHandle;
            public void refreshformWindow()
            {
                // Clear Drop Box (empties drop box)
                this.frmMainWindowHandle.clearPathBox();
        
            //skbtServerConfig is very simple property object
                foreach (KeyValuePair pair in CoreConfig.getServerConfigList())
                {
        
        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        When you put a breakpoint on the setServerPathDataSource for the problem code, what do you see in pathDataSource? How does it compare to the one that works? Do those objects actually appear identical internally?

        U 1 Reply Last reply
        0
        • P Pete OHanlon

          When you put a breakpoint on the setServerPathDataSource for the problem code, what do you see in pathDataSource? How does it compare to the one that works? Do those objects actually appear identical internally?

          U Offline
          U Offline
          uniflare
          wrote on last edited by
          #4

          Ok did some debugging, it seems that when i set a new datasource, the combobox.DisplayMember defaults to "". Although the combobox.ValueMember stays as "Key". the fix Im using is to re-set DisplayMember to "Value" on each datasource update. Is this a bug? Is it supposed to reset these properties when the datasource is set? or is it my implementation that is buggy? All other factors are identical. The actual problem was only the very FIRST datasource set worked as intended, every subsequent datasource was displayed improperly with displaymembers and valuemember being shown in the listbox. Revised Code Example: (Working with fix) (Compiles and runs with the components on designer..)

          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Windows.Forms;

          namespace etstapp1
          {
          public partial class Form1 : Form
          {
          private skbtServerControl sc;
          public Form1()
          {
          InitializeComponent();

                  this.sc = new skbtServerControl(this);
          
                  // coxArmaPath = Combo Box (simple drop down)
                  this.cBoxArmaPath.ValueMember = "Key";
                  this.cBoxArmaPath.DisplayMember = "Value"; // This doesnt seem to stick after the first datasource set??
                  // ....
              }
          
              public void addServerPathItem(String Key, String Value)
              {
                  this.cBoxArmaPath.Items.Add(new KeyValuePair(Key, Value));
                  // This works fine, only the "DisplayMember" is displayed and the "ValueMember" is hidden.
              }
          
              // This acts differently with the same types???
              public void setServerPathDatasource(List\> source)
              {
                  this.cBoxArmaPath.DisplayMember = "Value"; // fix datasource problem
                  this.cBoxArmaPath.ValueMember = "Key"; // fix datasource problem
          
                  this.cBoxArmaPath.DataSource = new BindingSource(source, null);
              }
          
              public void clearPathBox()
              {
                  if(this.cBoxArmaPath.DataSource == null){
                      this.cBoxArmaPath.Items.Clear();
                  }
                  else
                  {
                      this.cBoxArmaPath.DataSource = null;
                  }
              }
          
              private void btnStatic\_Click(object sender, EventArgs e)
              {
                  this.sc.refreshformWindowWithSTATICDatasource();
          
          P 1 Reply Last reply
          0
          • U uniflare

            Ok did some debugging, it seems that when i set a new datasource, the combobox.DisplayMember defaults to "". Although the combobox.ValueMember stays as "Key". the fix Im using is to re-set DisplayMember to "Value" on each datasource update. Is this a bug? Is it supposed to reset these properties when the datasource is set? or is it my implementation that is buggy? All other factors are identical. The actual problem was only the very FIRST datasource set worked as intended, every subsequent datasource was displayed improperly with displaymembers and valuemember being shown in the listbox. Revised Code Example: (Working with fix) (Compiles and runs with the components on designer..)

            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows.Forms;

            namespace etstapp1
            {
            public partial class Form1 : Form
            {
            private skbtServerControl sc;
            public Form1()
            {
            InitializeComponent();

                    this.sc = new skbtServerControl(this);
            
                    // coxArmaPath = Combo Box (simple drop down)
                    this.cBoxArmaPath.ValueMember = "Key";
                    this.cBoxArmaPath.DisplayMember = "Value"; // This doesnt seem to stick after the first datasource set??
                    // ....
                }
            
                public void addServerPathItem(String Key, String Value)
                {
                    this.cBoxArmaPath.Items.Add(new KeyValuePair(Key, Value));
                    // This works fine, only the "DisplayMember" is displayed and the "ValueMember" is hidden.
                }
            
                // This acts differently with the same types???
                public void setServerPathDatasource(List\> source)
                {
                    this.cBoxArmaPath.DisplayMember = "Value"; // fix datasource problem
                    this.cBoxArmaPath.ValueMember = "Key"; // fix datasource problem
            
                    this.cBoxArmaPath.DataSource = new BindingSource(source, null);
                }
            
                public void clearPathBox()
                {
                    if(this.cBoxArmaPath.DataSource == null){
                        this.cBoxArmaPath.Items.Clear();
                    }
                    else
                    {
                        this.cBoxArmaPath.DataSource = null;
                    }
                }
            
                private void btnStatic\_Click(object sender, EventArgs e)
                {
                    this.sc.refreshformWindowWithSTATICDatasource();
            
            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            This would seem to indicate that the new BindingSource was resetting the values. While it may not be a bug, it's certainly not a very clever API design (by the designers of WinForms, not you I will hasten to add). Okay, I've been having more of a think about this and I suspect that the reason that you need to do this is because when you change your BindingSource, the ComboBox has no concept of what it's bound to. You could be binding to something that doesn't use Key and Value here, so it forces you to update these values here.

            U 1 Reply Last reply
            0
            • P Pete OHanlon

              This would seem to indicate that the new BindingSource was resetting the values. While it may not be a bug, it's certainly not a very clever API design (by the designers of WinForms, not you I will hasten to add). Okay, I've been having more of a think about this and I suspect that the reason that you need to do this is because when you change your BindingSource, the ComboBox has no concept of what it's bound to. You could be binding to something that doesn't use Key and Value here, so it forces you to update these values here.

              U Offline
              U Offline
              uniflare
              wrote on last edited by
              #6

              Thanks for the detailed response, your explanation does seem logical. Thanks again guys!

              1 Reply Last reply
              0
              • U uniflare

                Ok this is a CONCEPT problem (the code is not professional, just read it for the concept, though it does compile and run). Right, I have asked many people on IRC and no one is giving me a straight answer lol so maybe some professionals can help me. I am trying to use a Datasource property for my combobox. This is so I can just pass an entire KeyValue pair with indexes and values to be shown in the listbox. Creating the keyvaluepair as a WHOLE and passing that to datasource property works perfectly. Creating a keyvaluepair PROCEDURALLY (in a loop or w/e) ends in the datasource property ToString()ing it, list box items show BOTH value AND display members in square brackets as an entire option. NOTE: Getting the lsitbox.value results in ONLY the valuemember being returned even though the listbox shows both the valuemembers AND the displaymember? it knows they are different? Now, I dont want any alternatives or comments about the quality of the code, all I want to know is WHY is it doing this, it seems compeltely illogical that the same TYPE and Values and can different Effects within the datasource property? Its like it knows it was built procedurally? Anyway here is some code for reference:

                // Windows Form

                namespace skbtInstaller
                {
                public partial class frmMainWindow : Form
                {
                public frmMainWindow()
                {
                InitializeComponent();

                    // coxArmaPath = Combo Box (simple drop down)
                        this.cBoxArmaPath.ValueMember = "Key";
                        this.cBoxArmaPath.DisplayMember = "Value";
                    // ....
                    }
                
                    public void addServerPathItem(String Key, String Value)
                {
                        this.cBoxArmaPath.Items.Add(new KeyValuePair(Key,Value));
                    // This works fine, only the "DisplayMember" is displayed and the "ValueMember" is hidden.
                    }
                
                // This acts differently with the same types???
                    public void setServerPathDatasource(List\> source)
                    {
                        this.cBoxArmaPath.DataSource = new BindingSource(source, null);
                    }
                }
                
                public class skbtServerControl
                {
                    private frmMainWindow frmMainWindowHandle;
                    public void refreshformWindow()
                    {
                        // Clear Drop Box (empties drop box)
                        this.frmMainWindowHandle.clearPathBox();
                
                    //skbtServerConfig is very simple property object
                        foreach (KeyValuePair pair in CoreConfig.getServerConfigList())
                        {
                
                S Offline
                S Offline
                seemajoshii
                wrote on last edited by
                #7

                Great idea..Thanks for sharing your knowledge

                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