List Box showing Value and Display members?
-
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()) {
-
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()) {
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.
-
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()) {
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?
-
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?
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();
-
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();
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.
-
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.
-
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()) {
Great idea..Thanks for sharing your knowledge