Another ComboBox data source gotcha

W

web1110

Here's another one. The code is below.

If I display to the ListBox, I get the exception:

Could not bind to the new display member, Parameter name:
newDisplayMember

and all ListBox members are set to MtProg.Form1.USState

If I comment out the listBox1 lines, leaving the comboBox1 lines, I get the
same exception but the combo box remains unchanged.


public class USState
{
public string longName;
public string shortName;
public USState(string shortName, string longName)
{
this.shortName=shortName;
this.longName=longName;
}
}

ArrayList USStates = new ArrayList();

private void button1_Click(object sender, System.EventArgs e)
{
USStates.Add(new USState("Alabama", "AL"));
USStates.Add(new USState("Washington", "WA"));
USStates.Add(new USState("West Virginia", "WV"));
USStates.Add(new USState("Wisconsin", "WI"));
USStates.Add(new USState("Wyoming", "WY"));

listBox1.DataSource = USStates;
listBox1.DisplayMember = "longName";
listBox1.ValueMember = "shortName";

comboBox1.DataSource = USStates;
comboBox1.DisplayMember = "longName";
comboBox1.ValueMember = "shortName";
}
 
W

web1110

OK, I made some progreess. If I remove the ValueMember lines, the exception
goes away.

However, both controls contain the values:

"ComboBoxArrayBind.Form1+USState"

Can anyone see what I am missing?

Thanx,
Bill
 
W

web1110

It works for a one-D ArrayList



public ArrayList stData = new ArrayList();



private void Form1_Load(object sender, System.EventArgs e)

{

stData.Add("AAA");

stData.Add("BBB");

stData.Add("CCC");

stData.Add("DDD");

try

{

listBox1.DataSource = stData;

comboBox1.DataSource = stData;

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}
 
W

web1110

Got it. Had to access via properties.

public ArrayList USStates = new ArrayList();
public class USState
{
public string longName;
public string shortName;
public USState(string longName, string shortName)
{
this.shortName=shortName;
this.longName=longName;
}
public string LongName
{
get
{
return longName;
}
}
public string ShortName
{
get
{
return shortName;
}
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
USStates.Add(new USState("Alabama", "AL"));
USStates.Add(new USState("Washington", "WA"));
USStates.Add(new USState("West Virginia", "WV"));
USStates.Add(new USState("Wisconsin", "WI"));
USStates.Add(new USState("Wyoming", "WY"));
USStates.Add(new USState("New Jersey", "NJ"));
try
{
listBox1.DataSource = null;
listBox1.DataSource = USStates;
listBox1.DisplayMember = "LongName";
comboBox1.DataSource = null;
comboBox1.DataSource = USStates;
comboBox1.DisplayMember = "LongName";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top