By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,847 Members | 2,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,847 IT Pros & Developers. It's quick & easy.

Changing control value without triggering an event.

P: n/a
I was wondering if there is a way to change the value of a control
without triggering an event. The ComboBox control, for example, will
trigger the SelectedIndexChanged event when its SelectedIndex
property is changed. I would like to change the SelectedIndex
property without triggering an event.

The reason I ask is that in the application I'm writing, I have many
controls that manipulate the program data. The data is encapsulated
into a model class to keep it separate from the gui portion of my
app. When a control's value changes and its corresponding event is
triggered, the model is modified.

Control ---> Model

So far so good. But sometimes the model can change without the
control, in which case the control will need to be updated to
accurately represent the model. Sticking with the ComboBox example,
this would mean changing its SelectedIndex property, but doing so
causes the SelectedIndexChanged event to trigger thus modifying the
model needlessly. So I wind up with a circular situation.

Control <--- Model
Control ---> Model

Any thoughts of a way around this? A better design, maybe?

The easy answer is for the delegate representing the method to
handle the control's event to be removed before the control is
changed and then added again afterwards. Or maybe a boolean variable
indicating to the event handler if it should modify the model. At
any rate, I'd appreciate any thoughts. Thanks.
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
hmmm....

Honestly, I'd probably go with the boolean value, or some other method to
check when to truly modify your data. You either subscribe to event, and
you have it, or you don't. Can't straddle the fence with this one. If you
Never want the event use -= to unsubscribe. If you want it sometimes,
subscribe to it and activate it's code only based on your business'
conditions.

Nick Harris, MCSD
http://www.VizSoft.net

"Wavemaker" <ja**********@BiteMeHotmail.com> wrote in message
news:wJ_Kb.769908$Tr4.2209716@attbi_s03...
I was wondering if there is a way to change the value of a control
without triggering an event. The ComboBox control, for example, will
trigger the SelectedIndexChanged event when its SelectedIndex
property is changed. I would like to change the SelectedIndex
property without triggering an event.

The reason I ask is that in the application I'm writing, I have many
controls that manipulate the program data. The data is encapsulated
into a model class to keep it separate from the gui portion of my
app. When a control's value changes and its corresponding event is
triggered, the model is modified.

Control ---> Model

So far so good. But sometimes the model can change without the
control, in which case the control will need to be updated to
accurately represent the model. Sticking with the ComboBox example,
this would mean changing its SelectedIndex property, but doing so
causes the SelectedIndexChanged event to trigger thus modifying the
model needlessly. So I wind up with a circular situation.

Control <--- Model
Control ---> Model

Any thoughts of a way around this? A better design, maybe?

The easy answer is for the delegate representing the method to
handle the control's event to be removed before the control is
changed and then added again afterwards. Or maybe a boolean variable
indicating to the event handler if it should modify the model. At
any rate, I'd appreciate any thoughts. Thanks.

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.