Changing control value without triggering an event.

W

Wavemaker

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.
 
N

Nick Harris

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
 

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