OnClick |
The following code snippets can be invoked when a control is clicked on.
Add the SourceTag of a control as a trend in a TrendGraph control on click. The following snippet also shows how static methods on the ControlBase class can be used to get a property from any kind of control, assuming the control has the property specified.
public void AddTrendOnClick(object sender, EventArgs eventArgs) { // check if control clicked on has a source tag string sourceTag; // NOTE: use static method on control base that can get property // of any control, assuming the property exists on a control ControlBase.TryGetProperty(sender, "SourceTag", out sourceTag); if (!string.IsNullOrEmpty(sourceTag)) { // trend graph we will add trend to string trendGraphName = "TrendGraph1"; ControlTrendGraph trendGraph = Screen.ScreenControls[trendGraphName] as ControlTrendGraph; // check if trend graph already has trend with source tag if (IsTrendAdded(trendGraph, sourceTag)) { Log.Info("Trend has already been added."); return; } // add trend ControlTrend trend = ControlFactory.CreateControl(ControlKind.Trend) as ControlTrend; trend.SourceTag = sourceTag; trendGraph.Children.Add(trend); } else { Log.Info("Invalid SourceTag."); } } private bool IsTrendAdded(ControlTrendGraph trendGraph, string sourceTag) { foreach (ControlBase child in trendGraph.Children) { // NOTE: use static method on control base that can get property // of any control, assuming the property exists on a control string compareSourceTag; ControlBase.TryGetProperty(child, "SourceTag", out compareSourceTag); if (sourceTag == compareSourceTag) return true; } return false; }
Updating the property of a control OnClick. The following snippet shows how to use the OnClick event of a Button control to set the text property of a Label control
public void Button1_OnClick(object sender, EventArgs eventArgs) { // get label we will set new text on string labelName = "Label1"; ControlLabel label = Screen.ScreenControls[labelName] as ControlLabel; // determine and set new text value if (label.Text == "Label") { label.Text = "1"; } else if (int.TryParse(label.Text ?? "Nan", out int number)) { label.Text = (number + 1).ToString(); } else { label.Text = "Error"; } }