winforms - c# lock a windows forms control -
i'm programming winforms app, , have encountered problem:
i have, example, numeric updown control, , when pressing up/down button, don't want change, want access new value, without changing number on control itself.
i need able unlock under condition, that:
private void numericupdown1_valuechanged(object sender, eventargs e) { if (!canchange) { int newvalue = get_expected_new_value(); dosomestuff(newvalue); //some_code_to_cancel_the_value_change; } else { //allow change dosomeotherstuff(); } }
how can thins thing?
you can use tag
property of numericupdown1
store last value. although it's not particulary elegant solution.
credit to: c# numericupdown.onvaluechanged, how changed?
in case can this:
private void numericupdown1_valuechanged(object sender, eventargs e) { var o = (numericupdown) sender; int thisvalue = (int) o.value; int lastvalue = (o.tag == null) ? 0 : (int)o.tag; o.tag = thisvalue; if (checkbox1.checked) //some custom logic { //remove event handler it's not fired when change value in code. o.valuechanged -= numericupdown1_valuechanged; o.value = lastvalue; o.tag = lastvalue; //now add o.valuechanged += numericupdown1_valuechanged; } //otherwise allow normal }
basicaly store last known value in tag
property. check condition , set value last value.
Comments
Post a Comment