Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Easy Heading Macro
headingIndent40
selectorh2,h3
navigationExpandOptiondisable-expand-collapse
This page has information about the CodeBehind tab.

Overview

The CodeBehind tab

is used to define a set of functions connected

serves as a platform for defining functions linked to a display

. You can write code

, 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:

Table of Contents
maxLevel3
stylenone


Understanding CodeBehind

You can switch between VB.Net and C#, and or CSharp. You can also switch between both. If you change your language selection, the system automatically converts existing code to the selected chosen language. If you need references to For your own assembliesassembly references, use Run → Build Scripts → References.

CodeBehind functions can be executed when opening or closing, or while the display is open, depending on how you configure the code. You can use CodeBehind to define the mouse and input command handling methods to be executed on a specific display.

For Dialog displays, use the built-in DialogOnOK method, which is called when the built-in OK button on the dialog is pressed. If "True" is returned on that method, the dialog is closed; if "False" is returned, the dialog remains open. This method is commonly used to ensure data validation on the dialog and prompts the user to correct incorrect entries before closing the dialog.

The Code Behind of the Displays has the following pre-defined methods:

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
  • DisplayOpening()Executed when a display is opening.
  • DisplayIsOpen()Called in regular intervals while the display is open.
  • DisplayClosing()Executed : Activated when a display is closing.
  • DialogOnOK()Called when the OK button : Invoked when the OK button on a dialog display is pressed. Returning 1 allows permits the dialog to close. Returning , while returning 0 prevents the dialog from closing.its closure.

Custom .NET Variables and Methods

In addition to pre-defined methods, CodeBehind allows for the incorporation of custom You can add your own  .NET variables and methods to this page. 

Info

Since the client displays are designed to run on distributed environments as well as web environments, we recommend to avoid using functions that do not allow partial trust execution or that refer to physical file paths.

, 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

InfoOnce

the CodeBehind runs on the client side, the display

will be locked when

locks while the code is running

. To avoid locked displays for too long, choose Scripts (tasks and classes) if the code has too many procedures.

(This can be circumvented by using Async functions). 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.

Code Block
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:

In this section...

Page Tree
root@parent
spacesV10