Show / Hide Table of Contents

Initializing when loaded into a Station

This topic shows the necessary steps to override the OnLoad(SmartComponent) from a SmartComponent's CodeBehind.

This method is called when RobotStudio loads the CodeBehind assembly of a SmartComponent. The developer can use the passed arguments, which contains the SmartComponent itself, to modify it or interact with other elements of the Station.

In this example, we show a very simple use case in which we access the name of the passed argument and display it RobotStudio's terminal as soon as the component is loaded into RobotStudio.

The steps contained in this topic are:

  1. Add an overriding method called OnLoad(SmartComponent).

  2. Write the statements to execute once the component loads.

  3. Execute the component in RobotStudio.

Note

The code in this topic can be tested using the default RobotStudio Smart Component template.

Solution

  1. Open the CodeBehind.cs source file of the SmartComponent and, inside the CodeBehind class, add the following overriding method:

    public override void OnLoad(SmartComponent component)
    {
        base.OnLoad(component);
    }
    
  2. As mentioned before, the component argument contains the SmartComponent that called the function. We will access one of its properties (its name) and display it as a message.

    public override void OnLoad(SmartComponent component)
    {
        Logger.AddMessage(new LogMessage($"Hello, my name is {component.Name} and I am being loaded!"));
        base.OnLoad(component);
    }
    
  3. To run this example, load the built .rslib file in RobotStudio.

    Running this component will produce the following output:

    Hello, my name is SmartComponent1 and I am being loaded!

Example

The CodeBehind file is shown in the following example:

using System;
using System.Collections.Generic;
using System.Text;

using ABB.Robotics.Math;
using ABB.Robotics.RobotStudio;
using ABB.Robotics.RobotStudio.Stations;

namespace SmartComponent1
{
    /// <summary>
    /// Code-behind class for the SmartComponent1 Smart Component.
    /// </summary>
    /// <remarks>
    /// The code-behind class should be seen as a service provider used by the 
    /// Smart Component runtime. Only one instance of the code-behind class
    /// is created, regardless of how many instances there are of the associated
    /// Smart Component.
    /// Therefore, the code-behind class should not store any state information.
    /// Instead, use the SmartComponent.StateCache collection.
    /// </remarks>
    public class CodeBehind : SmartComponentCodeBehind
    {
        public override void OnLoad(SmartComponent component)
        {
            Logger.AddMessage(new LogMessage($"Hello, my name is {component.Name} and I am being loaded!"));
            base.OnLoad(component);
        }
		
        // Additional methods are defined here.
    }
}

Required Namespaces

ABB.Robotics.RobotStudio

ABB.Robotics.RobotStudio.Stations

See Also

  • SmartComponent Introduction
  • Creating a SmartComponent
  • Overriding OnSimulationStart/Stop
  • Overriding OnIOSignalValueChanged
  • Overriding CustomValidation
In this article
Back to top Copyright © 2025 ABB