Overview
The CodeBehind tab serves as a platform for defining functions linked to a display, written in either standard VB.Net or C# for WPF Clients and JavaScript for Web Clients. CodeBehind functions operate when opening, closing, or while the display is open, based on your code configuration.
CodeBehind enables you to script custom functionalities and responses to user inputs or system changes.
On this page:
Understanding CodeBehind
You can switch between VB.Net and C#, and the system automatically converts existing code to the chosen language. For your own assembly references, use Scripts → References.
The ability to switch between VB.Net and C# is a powerful feature that allows developers to leverage the strengths of both languages. Remember to thoroughly test your code after switching languages, as subtle differences between the two can lead to unexpected behaviors.
Pre-defined Methods and Custom .NET Variables
Pre-defined Methods
The CodeBehind of each display includes a set of pre-defined methods, designed to streamline common display events:
- DisplayOpening(): Triggered when a display is in the process of opening.
- DisplayIsOpen(): Regularly called throughout the duration the display remains open.
- DisplayClosing(): Activated when a display is closing.
- DialogOnOK(): Invoked when the OK button on a dialog display is pressed. Returning 1 permits the dialog to close, while returning 0 prevents its closure.
Custom .NET Variables and Methods
In addition to pre-defined methods, CodeBehind allows for the incorporation of custom .NET variables and methods, for example as UI handles for Mouse Actions.
Custom Properties Extension
Alongside these methods, displays also support the use of Custom Properties Extensions. These properties serve as local variables exclusive to a particular display and can be accessed both within the display itself and in CodeBehind. Custom Properties can also be linked to tags to increase their functionality. One significant advantage of employing custom properties is the potential reduction of communication points while preserving project functionality.
Several runtime methods are available for managing Custom Properties, including methods for retrieving and setting property values, obtaining all properties as a string, and eliminating all custom properties. Note, however, that the logic programmed with Custom Properties is strictly valid for the specific display they're attached to, indicating that data cannot be preserved from one page for use on another.
For information about CustomProperties and their associated methods, see Custom Properties.
Code Execution Performance
When the CodeBehind runs on the client side, the display locks while the code is running. For lengthy procedures, opt for Scripts (tasks and classes) to prevent displays from locking for too long.
Code execution efficiency is crucial in HMI SCADA systems. Long running code can block the user interface, leading to poor user experience. Always strive to optimize your CodeBehind routines and consider offloading heavy processing tasks to the server or background processes.
Managing Multiple Popups
FactoryStudio provides diverse methods to manage and interact with displays and popups, including Display.Open
, Display.NewPopup
, Client.OpenDisplay
, and Client.NewPopup
, each with its own unique syntax and behavior. These methods provide flexibility in how new displays or popups are opened and handled in your project.
One key consideration is the difference between @Display
and @Client
methods. For instance, the software automatically updates the method in @Display
if the display name changes, but not in @Client
methods, which require manual updates.
Moreover, while utilizing labels as parameters for symbols can streamline your project setup, be aware of potential limitations when combining NewPopup input labels and Symbols. A common workaround is to use individual Popups for each symbol.
Online Configuration is another powerful tool for real-time project updates directly from the Engineering Environment.
For an in-depth understanding and examples of managing multiple popups in FactoryStudio, please visit Multiple Popups.
Knowledge of the available methods and their distinct behaviors can greatly enhance the flexibility and interactivity of your project.
Working with the Display Namespace
The Display Namespace contains information on how to manage displays and popups in FactoryStudio. It lists the displays and layouts with their properties and open and close methods, as well as contains the properties of the client environment on each connected device.
There are several ways to open a new display or popup, such as Display.DisplayName.Open
, Display.DisplayName.NewPopup
, Client.OpenDisplay
, and Client.NewPopup
. Each method has specific characteristics and behaviors that should be considered when using them.
In addition, the namespace includes a series of useful methods like Close()
, GetCustomPropertiesAsString()
, SetCustomProperties()
, and SetCustomPropertyValue()
, which allow you to manage and customize display properties.
For more information, see Objects and Namespaces
Example: Mouse Wheel Event in CurrentDisplay
To capture the mouse wheel event of a display and configure the Display.ZoomLevel of a page from it, use the following code in the CodeBehind to zoom in and out using the mouse wheel.
public void DisplayIsOpen() { this.CurrentDisplay.GetThis().PreviewMouseWheel += this.PreviewMouseWheelChanged; } private void PreviewMouseWheelChanged(object sender, MouseWheelEventArgs e) { e.Handled = true; @Info.Trace("PreviewMouseWheelChanged || Delta: " + e.Delta); // If the mouse wheel delta is positive if (e.Delta > 0) { @Display.MainPage.ZoomLevel += 0.1; } // If the mouse wheel delta is negative? if (e.Delta < 0) { @Display.MainPage.ZoomLevel -= 0.1; } }
References and Language Code Selection
To reference your own assemblies, use Scripts→ References. Language selection allows you to choose between VB.Net and C#, with the system automatically converting the existing code to the chosen language.
When referencing assemblies, ensure they are compatible with your selected language and that they are correctly versioned. When changing languages, always review the converted code for potential translation issues as some constructs in VB.Net might not have direct equivalents in C#, and vice versa.
In this section: