ComboBox SelectedValue property always returns null

6

6tc1

Hi all, I would like to create a ComboBox, then use the
SelectedIndexChanged event handler to activate and get the selected
value (not index).

Anyway, I have the following to link the control with the event
handler:
comboBox.SelectedIndexChanged += new
System.EventHandler(listControlValueChanged);

Here is how I add elements:
ArrayList tokenList = parseRangeString(rangeNodeStr);
for (int i = 0; i < tokenList.Count;i++)
{
comboBox.Items.Add(tokenList);
}

I also tried this method of adding the elements:
ArrayList comboBoxCollection = new ArrayList();
for (int i = 0; i < tokenList.Count;i++)
{
ComboBoxElement comboBoxElement = new ComboBoxElement
((String)tokenList,(String)tokenList);
comboBoxCollection.Add(comboBoxElement);
}
comboBox.DataSource = comboBoxCollection;
comboBox.ValueMember = "mValueMemeber";
comboBox.DisplayMember = "mDisplayMember";

But in either of the above cases, my event handler:
private void listControlValueChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
Console.Out.WriteLine("change: "+comboBox.Name+ " index: "+
comboBox.SelectedIndex+ " value: "+ comboBox.SelectedValue);
}

always shows null for the SelectedValue, but SelectedIndex works fine.

Any ideas?

Thanks,
Novice
 
6

6tc1

Well, I couldn't figure out why ComboBox would suck this badly - so I
just hacked together a solution of my own. Probably the right way to
have fixed it would be to have extended ComboBox and overriden the Add
method - so that the add method also throws this data into a data
structure that I can access at run-time.

So stupid.

Tim
 
6

6tc1

Okay, so here is how you get the SelectedValue:
comboBox.Items[comboBox.SelectedIndex]

Live and learn,
Novice
 
C

Claes Bergefall

I also tried this method of adding the elements:
ArrayList comboBoxCollection = new ArrayList();
for (int i = 0; i < tokenList.Count;i++)
{
ComboBoxElement comboBoxElement = new ComboBoxElement
((String)tokenList,(String)tokenList);
comboBoxCollection.Add(comboBoxElement);
}
comboBox.DataSource = comboBoxCollection;
comboBox.ValueMember = "mValueMemeber";
comboBox.DisplayMember = "mDisplayMember";


This would work provided ComboBoxElement has *properties*
named mValueMember and mDisplayMember. Does it?
(I'm guessing no. Sounds like public variables to me).

The ValueMember and DisplayMember strings must
contain the name of a property that an individual item
in your collection support (in your case an item of type
ComboBoxElement). So either implement mValueMember
and mDisplayMember as properties or change the values
to something that is a property.

I noticed you've already found a solution to your original
problem. Just wanted to make a few notes if you need to
get this working in the future.

/claes
 

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