SelectedItem not firing whatever I do

  • Thread starter Thread starter wobbles
  • Start date Start date
W

wobbles

Hi Everyone,

I've tried many solutions I've found using Google and also this group
but nothing seems to work for me.

I have 2 dropdown lists and 1 listbox.
Selection of the 1st combo determines what's in the second. Selection
of the second determines what gets selected in the listbox.

When I set the "SelectedItem" property of the second dropdown the
event doesn't get fired at all.
I've tried various workarounds:
- Setting SelectedIndex = 0, then -1 (or -1, -1),
- Setting SelectedItem,
- Setting Combo.SelectedIndex = Combo.Items.IndexOf(item)

Is there anything I can do? What's the definitive solution?
I'm now very frustrated.

Thanks!

(I'm using VS.net 2003.)
 
According qith your request you want that when select an item in the first combo to populate the second and then the third...well why you don't try with the SelectionChangeCommitted event of the ComboBo

Regard

Johny Segura
 
According qith your request you want that when select an item in the first combo to populate the second and then the third...well why you don't try with the SelectionChangeCommitted event of the ComboBox

Regards

Johny Segura

Hi Johny,

I've just tried that but it doesn't work either :-(

I'm really a bit lost and can't see what I'm doing wrong.


I have a SetInitialValues() method after the InitializeComponent()
method.
Inside SetInitialValues() I populate my first combo, then set the
initial value of that combo based on a registry value.
This triggers the "Combo1_SelectedItemChanged" handler, which
populates Combo2.

So far, so good.

Combo2 is then populated properly, but when I try to set the initial
value for Combo2 (again from SetInitialValues()), Combo2's handler
doesn't pick up the ItemChanged event.

Result: When the form finishes loading, Combo2 has nothing selected.
This results in my listbox having nothing selected too.
 
* wobbles said:
I've tried many solutions I've found using Google and also this group
but nothing seems to work for me.

I have 2 dropdown lists and 1 listbox.
Selection of the 1st combo determines what's in the second. Selection
of the second determines what gets selected in the listbox.

When I set the "SelectedItem" property of the second dropdown the
event doesn't get fired at all.
I've tried various workarounds:
- Setting SelectedIndex = 0, then -1 (or -1, -1),
- Setting SelectedItem,
- Setting Combo.SelectedIndex = Combo.Items.IndexOf(item)

Are you sure the event handler is registered properly? Are you using a
databound control?
 
Are you sure the event handler is registered properly? Are you using a
databound control?


Hello Herfried,

Yes, the event handler is registered properly.
It works once the form is loaded, but not when it's still loading!

My combos aren't databound, but this is what I use to add items:

private void PopulateCombo2() {
cbo2.Items.Clear();
cbo2.Items.Add("All Types");
cbo2.Items.AddRange(GetLogEntryTypes().ToArray());
}

WHERE "GetLogEntryTypes()" is a function that returns an ArrayList.

It appears to be populating correctly.
Even though it's not databound, perhaps the same problem is occurring?
I've read about combo problems where they're databound...

P.S. "cbo2.Items.Add("All Types");"
because my combo is filled from a system enum which doesn't define
"All Types".
 
Hi Everyone,

I haven't found the answer but I've got a (ugly) workaround.

Instead of adding to the combobox programatically, if I hard-code the
information into the combo, the event is fired and the handler picks
it up.
I also found that if I did "SelectedIndex = ..." it worked, but this
was no use to me because that index might change depending on what's
in the EventLog...


Please could someone verify that I'm seeing something strange?
Am I doing something wrong???

Thanks!
Wobbles


I've run a simple test in a test form and I can replicate it.
The problem (for some reason) only seems to affect the second combo in
my app, but in the example below it fails for both controls.

Ingredients:
- Simple windows forms application
- 2 comboBoxes (anywhere on form)
- 2 buttons (anywhere on form)
- reference to System.Collections
- reference to System.Diagnostics

============CODE===================

//method to help populate combo
private ArrayList GetEnumList() {
ArrayList alSeverity = new ArrayList();
foreach (EventLogEntryType sv in
Enum.GetValues(typeof(EventLogEntryType))) {
alSeverity.Add(sv);
}
return alSeverity;
}


//this code goes straight AFTER InitializeComponent()
comboBox1.Items.AddRange(this.GetEnumList().ToArray());
comboBox2.Items.AddRange(this.GetEnumList().ToArray());


//Simple "click" handlers generated by VS.Net
private void button1_Click(object sender, System.EventArgs e) {
comboBox1.SelectedItem = "Information";
comboBox2.SelectedItem = "Error";
}

private void button2_Click(object sender, System.EventArgs e) {
comboBox1.SelectedIndex = 1;
comboBox2.SelectedIndex = 3;
}
 
Back
Top