ComboBox.SelectedIndexChanged by user or code?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is more of a grumble than a question really.

I have a config file that reads various settings into my app on start-up.
One of these settings sets the selected item in a combo box, which in turn
fires the combo's SelectedIndexChanged event, which in turn causes all hell
to break loose.

I realise I can add RemoveHandler/AddHandler code before/after the line that
reads this setting from the config file but, as the app grows, this sort of
redundant code replication is going to start to annoy me.

Wouldn't it be far better if .NET could make a distinction between the
selected index being changed in code and the selected index being changed by
the user? Couldn't we have something like a SelectedIndexUserChanged event as
well? This was available in VB6, after all (ListIndex and Click).
 
Mark said:
This is more of a grumble than a question really.

I have a config file that reads various settings into my app on start-up.
One of these settings sets the selected item in a combo box, which in turn
fires the combo's SelectedIndexChanged event, which in turn causes all hell
to break loose.

I realise I can add RemoveHandler/AddHandler code before/after the line that
reads this setting from the config file but, as the app grows, this sort of
redundant code replication is going to start to annoy me.

Wouldn't it be far better if .NET could make a distinction between the
selected index being changed in code and the selected index being changed by
the user? Couldn't we have something like a SelectedIndexUserChanged event as
well? This was available in VB6, after all (ListIndex and Click).

I solved this problem in each class with a single boolean flag,
something like

bool _updating;

It's usually set to false, unless I'm updating controls
programmatically:

this._updating = true;
try
{
this.comboBox1.SelectedIndex = ... ;
this.textBox1.Text = ... ;
etc.
}
finally { this._updating = false; }

Then, in the event handlers:

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if (!this._updating)
{
... react to event ...
}
}
 
Back
Top