On slider1_ValueChanged I want to do something and then set a new value to slider but without triggering slider1_ValueChanged again otherwise I may have infinite loop.
I thought that setting a proper开发者_开发技巧ty wouldn't trigger the event but I just tested with Visual Studio Slider it does trigger its event. Using a flag is really ugly and complex I'd like to avoid maybe by using an other event ?
This is a common problem in Windows forms. You need to unhook the event handlers, make your changes, then hook up the event handlers again.
You can make this easier if you move the event handler hooking/unhooking code into their own functions, so that you can do the hookup from the constructor (or Loaded event) and the ValueChanged event.
I'm not sure about this one but in general when you set a property in code it does not trigger the event.
But if it does, the general technique is to guard with an exclusion field:
private bool _settingValue = false;
void valueChanged(object sender, ...)
{
if (! settingValue)
{
_settingValue = true;
try
{ ...
}
finally { settingvalue = false; }
}
}
精彩评论