List Box showing Value and Display members?

  • Thread starter Thread starter Chemical Bliss
  • Start date Start date
C

Chemical Bliss

Guest
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<String, String>(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<KeyValuePair<String, String>> 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<String, skbtServerConfig> pair in CoreConfig.getServerConfigList())
{
// Populate Drop Box
this.frmMainWindowHandle.addServerPathItem(pair.Key, pair.Value.getTextualName()); // this works as intended
// cBox gets items like: Some Display Text, Some Display Text, Some Display Text (PERFECT!)
}
}

// This works absolutely fine. ValueMembers are hidden.
public void refreshformWindowWithSTATICDatasource()
{
// Clear Drop Box
this.frmMainWindowHandle.clearPathBox();

var pathDataSource = new List<KeyValuePair<String, String>>()
{
new KeyValuePair<String, String>("testKey1", "somevalue1"),
new KeyValuePair<String, String>("testKey2", "somevalue2"),
new KeyValuePair<String, String>("testKey3", "somevalue3")
};

// Populate Drop Box
this.frmMainWindowHandle.setServerPathDatasource(pathDataSource);
// cBox gets items like: Some Display Text, Some Display Text, Some Display Text (PERFECT!)
}

// ** HERE IS THE PROBLEM?? **
// This creates a type that seems different to the above function which works fine...
public void refreshformWindowWithDatasource()
{
// Clear Drop Box
this.frmMainWindowHandle.clearPathBox();

var pathDataSource = new List<KeyValuePair<String, String>>();

//skbtServerConfig is very simple property object
foreach (KeyValuePair<String, skbtServerConfig> pair in CoreConfig.getServerConfigList())
{
pathDataSource.Add(new KeyValuePair<String, String>(pair.Key, pair.Value.getTextualName()));
}

// Populate Drop Box
this.frmMainWindowHandle.setServerPathDatasource(pathDataSource);
// cBox gets items like [asjAETJQ5785d45,Some Display Text], [asawfgQ5785d45,Some Display Text], [asjAhrrQ5785d45,Some Display Text]
// ????? surely this function should act exactly like the function above??
}
}
}


Thanks!

(I have posted on codeproject too, will condense any replies).

Continue reading...
 
Back
Top