OnValueChange |
The following code snippets can be invoked when a value changes.
The OnBeforeValueChange event is triggered before the value of a control has been updated. You can use this to change the value that will be applied to a control.
public void OnBeginValueChange(object sender, TagSubscriptionArgs subscription, TVQ tvq) { //// control that value is being changed on // ControlBase control = sender as ControlBase; // NOTE: Event is triggered BEFORE control property has been changed. // Any modifications to the `tvq` argument will be applied to the control. double number; if (double.TryParse(tvq?.Value?.ToString() ?? "NaN", out number)) { switch (number) { case 0: tvq.Value = "Off"; return; case 1: tvq.Value = "On"; return; default: tvq.Value = "Undefined"; return; } } tvq.Value = "Error"; }
The OnEndValueChange event is triggered after the value of a control has been updated. In most cases you will want to use the OnBeforeValueChange event.
public void OnEndValueChange(object sender, TagSubscriptionArgs subscription, TVQ tvq) { // control that value is being changed on ControlBase control = sender as ControlBase; // NOTE: Event is triggered AFTER control property has been changed. // Any modifications to the `tvq` argument will NOT be applied to the control. double number; if (double.TryParse(tvq?.Value?.ToString() ?? "NaN", out number)) { switch (number) { case 0: tvq.Value = "Off"; break; case 1: tvq.Value = "On"; break; default: tvq.Value = "Undefined"; break; } } else { tvq.Value = "Error"; } // BAD CODE: since value of control has already been changed // we need to set the value explicitly which could cause an // infinite loop since the OnEndValueChange event gets fired // when the value is changed. Fortunately, Axiom prevents this // condition from getting stuck in an infinite loop, but it is // NOT RECOMMENDED to change the value of the control that // the event was triggered on in the OnEndValueChange event. control.Value = tvq; // GOOD CODE: if we want to change the value of a different // control based on the value of this control, we can do it // by getting a reference to that control and changing its // value. The following code assumes there is a control in // the current screen named "Label1". string labelName = "Label1"; ControlLabel label = Screen.ScreenControls[labelName]; label.Text = (tvq.Value as string) ?? ""; }