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; }