Add logic to displays with CodeBehind.
How-to Guides → Pillars → Operator → Displays → CodeBehind | How-To Guide | Reference
Displays CodeBehind (Reference) runs in each client and adds pre-defined event methods and custom functions to a display, in VB.NET, C#, or JavaScript (HTML5-only displays).
CodeBehind enables:
Access via Displays → Draw → CodeBehind tab.
Every display includes these event handlers:
| Method | Triggered When | Purpose |
|---|---|---|
| DisplayOpening() | Display starts opening | Initialize variables, setup |
| DisplayIsOpen() | While display remains open | Continuous updates, monitoring |
| DisplayClosing() | Display starts closing | Cleanup, save state |
| DialogOnOK() | OK button pressed (dialogs only) | Validate input, return 1 to close, 0 to prevent |
csharp
public void DisplayOpening()
{
// Initialize display
}
public void DisplayIsOpen()
{
// Called continuously while open
// Use sparingly for performance
}
public void DisplayClosing()
{
// Cleanup before close
}
public int DialogOnOK()
{
// Dialogs only - validate and return
if (ValidateInput())
return 1; // Allow close
else
return 0; // Prevent close
}Local variables exclusive to the display instance:
csharp
// Set property value
SetCustomPropertyValue("PropertyName", value);
// Get property value
var value = GetCustomPropertyValue("PropertyName");
// Get all as string
string allProps = GetCustomPropertiesAsString();
// Clear all properties
ClearCustomProperties();See → Custom Properties for detailed documentation.
csharp
public async void DisplayOpening()
{
await Task.Run(() => LongRunningOperation());
}CurrentDisplay is available in C# and VB.NET CodeBehind. It refers to the display instance running the code, so each popup instance works on itself.
Member | Purpose |
|---|---|
| The display element, for size and layout, for example |
| An element of the display, found by its Uid, not its Name. Cast the result to the control type, for example |
| Reads a parameter the display was opened with. See Popup Parameters below. |
| Writes a value to the tag a parameter names. |
| Moves the display. |
| Closes this display instance. |
// Size of this display double width = this.CurrentDisplay.GetThis().ActualWidth; double height = this.CurrentDisplay.GetThis().ActualHeight; // Close this display instance this.CurrentDisplay.Close(); |
A display opens with optional parameters: name=value pairs separated by semicolons. Its CodeBehind reads them with GetLabel and writes through them with SetLabel, so one display serves many tags.
// Opening the popup, in another display or in a button Action:
// @Display.MotorDetail.NewPopup("Motor=Tag.Pump1;Mode=2;Title='Pump 1'", 100, 100);
// In the CodeBehind of MotorDetail, called from a button:
public void ApplySettings()
{
// Value of the tag passed as Motor (here, Tag.Pump1)
object motorValue = this.CurrentDisplay.GetLabel("Motor");
// Number and text parameters
int mode = Convert.ToInt32(this.CurrentDisplay.GetLabel("Mode"));
string title = Convert.ToString(this.CurrentDisplay.GetLabel("Title"));
// Writes 0 to the tag passed as Motor (here, Tag.Pump1)
this.CurrentDisplay.SetLabel("Motor", 0);
} |
GetLabel returns the value of the parameter: the value of a tag, a number, or a text. Numbers come back as double. To read a member of a tag passed as a parameter, add it after a dot, for example GetLabel("Motor.Quality"). Convert the result to the type you need, as in the example.
SetLabel writes the value to the tag the parameter names. The parameter keeps pointing to the same tag. Its return value does not confirm the write: read the tag back when the result matters.
Parameter values: a value starting with a digit, + or - is a number. A value in single quotes is a text, and it cannot contain ; or =. Any other value is a tag or object name.
Names are not case-sensitive, and the # prefix is optional in the open call and in GetLabel. When a name appears twice in the open call, the first value wins.
A name the display was not opened with returns an empty text. A parameter whose value names a tag or object missing from the solution makes GetLabel and SetLabel throw an exception.
In HTML5 displays, GetLabel returns the value the browser holds at that moment and does not wait for the server. Call it from a button or a later event, not while the display opens.
GetLabel and SetLabel read the parameters of the display. Inside a symbol, use symbol parameters (see Symbols Control Reference).
The parameters come from @Display.<Name>.NewPopup(...), @Client.UI.NewPopup("Name", ...) and @Client.UI.OpenDisplay("Name", ...). When a display contains symbols, a parameter with the same name as a symbol label also fills that label; see Displays Multiple Popups Example.
Multiple methods available:
| Method | Auto-Updates on Rename | Usage |
|---|---|---|
@Display.PageName.Open() | Yes | Preferred for pages |
@Display.PageName.NewPopup() | Yes | Create popup instance |
@Client.OpenDisplay("PageName") | No | String-based navigation |
@Client.NewPopup("PageName") | No | String-based popup |
csharp
public void DisplayIsOpen()
{
this.CurrentDisplay.GetThis().PreviewMouseWheel += PreviewMouseWheelChanged;
}
private void PreviewMouseWheelChanged(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
if (e.Delta > 0)
@Display.MainPage.ZoomLevel += 0.1;
else if (e.Delta < 0)
@Display.MainPage.ZoomLevel -= 0.1;
}Each call to NewPopup opens a new, independent instance of the display. A display whose mode is Page does not open as a popup.
NewPopup returns an empty string. Pass the data each instance needs as parameters, and read them in the popup with GetLabel.
Each instance closes itself with this.CurrentDisplay.Close().
Keep the number of open instances in proportion to the memory of the client.
// In the calling display: one popup instance per item
public void OpenDetail(string tagName)
{
@Display.DetailPopup.NewPopup("Item=" + tagName);
}
// In the CodeBehind of DetailPopup, called from a button
public void Acknowledge()
{
this.CurrentDisplay.SetLabel("Item", 0);
this.CurrentDisplay.Close();
} |
See → Displays Multiple Popups Example for more scenarios.
See → Code Editor for complete feature documentation.
For using custom DLLs and assemblies:
See → Scripts References for configuration.
csharp
private bool initialized = false;
public void DisplayIsOpen()
{
if (!initialized)
{
InitializeDisplay();
initialized = true;
}
}csharp
private DateTime lastUpdate = DateTime.MinValue;
public void DisplayIsOpen()
{
if (DateTime.Now - lastUpdate > TimeSpan.FromSeconds(1))
{
UpdateDisplay();
lastUpdate = DateTime.Now;
}
}Display Freezing:
Methods Not Firing:
Variable Scope Issues: