Show / Hide Table of Contents

BeforeLoad and Close callbacks

The CodeBehind of a SmartComponent can access the BeforeLoadProjectFile and Closed Project events to bind a callback method after a project loads or before it unloads, respectively.

Note

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

Using the BeforeLoadProjectFile and Project.Closed

  1. Add the callback functions as methods of the SmartComponent in CodeBehind:

    public class CodeBehind : SmartComponentCodeBehind
    {
        public static void BeforeProjectLoadCallback(object o, EventArgs e)
        {
            Logger.AddMessage(new LogMessage("A project is about to be loaded!"));
        }
    
        public static void SayGoodbye(object o, EventArgs e)
        {
            Logger.AddMessage(new LogMessage("The project is about to be unloaded!"));
        }
    
        // Additional methods are defined here.
    }
    
  2. We will bind the call back functions as soon as the SmartComponent is loaded, for this, we will override the OnLoad() function with the commands needed for doing so.

    public class CodeBehind : SmartComponentCodeBehind
    {
        public static void BeforeProjectLoadCallback(object o, EventArgs e)
        {
            Logger.AddMessage(new LogMessage("A project is about to be loaded!"));
        }
    
        public static void SayGoodbye(object o, EventArgs e)
        {
            Logger.AddMessage(new LogMessage("The project is about to be unloaded!"));
        }
    
        public override void OnLoad(SmartComponent component)
        {
            base.OnLoad(component);
        }
    
        // Additional methods are defined here.
    }
    
  3. Since the Project.Closed event is handled using the built-in System.EventHandler we use that Handler to create a new instance of the event and bind it to the Project.ActiveProject.Closed object, that represents the project in which the SmartComponent will run.

    public class CodeBehind : SmartComponentCodeBehind
    {
        public static void BeforeProjectLoadCallback(object o, EventArgs e)
        {
            Logger.AddMessage(new LogMessage("A project is about to be loaded!"));
        }
    
        public static void SayGoodbye(object o, EventArgs e)
        {
            Logger.AddMessage(new LogMessage("The project is about to be unloaded!"));
        }
    
        public override void OnLoad(SmartComponent component)
        {
            Project.ActiveProject.Closed += new EventHandler(SayGoodbye);
            base.OnLoad(component);
        }
    
        // Additional methods are defined here.
    }
    
  4. The BeforeLoadProjectFile event is handled using the ProjectFileEventHandler, so we need to use that handler to create a new instance of the event and bind it to the Project.BeforeLoadProjectFile event.

    public class CodeBehind : SmartComponentCodeBehind
    {
        public static void BeforeProjectLoadCallback(object o, EventArgs e)
        {
            Logger.AddMessage(new LogMessage("A project is about to be loaded!"));
        }
    
        public static void SayGoodbye(object o, EventArgs e)
        {
            Logger.AddMessage(new LogMessage("The project is about to be unloaded!"));
        }
    
        public override void OnLoad(SmartComponent component)
        {
            Project.BeforeLoadProjectFile += new ProjectFileEventHandler(BeforeProjectLoadCallback);
            Project.ActiveProject.Closed += new EventHandler(SayGoodbye);
            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 OnLoad
  • Using the Unload and Shutdown Callbacks
In this article
Back to top Copyright © 2025 ABB