Show / Hide Table of Contents

Creating a Button

This example provides information on creating CommandBarButton(string, string) in RobotStudio.

Solution

  1. Create RibbonTab(string, string)

    // Create a new tab
    RibbonTab ribbonTab = new RibbonTab("MyTab", "MyTab");
    UIEnvironment.RibbonTabs.Add(ribbonTab);
    
    // Set ribbonTab as the active tab
    UIEnvironment.ActiveRibbonTab = ribbonTab;
    
  2. Create RibbonGroup(string, string)

    // Create a group for buttons
    RibbonGroup ribbonGroup = new RibbonGroup("MyButtons", "MyButtons");
    
  3. Create and add a CommandBarButton(string, string)

    // Create first button
    CommandBarButton buttonFirst = new CommandBarButton("MyFirstButton", "MyFirstButton");
    buttonFirst.HelpText = "Help text for small button";
    buttonFirst.Image = Image.FromFile(ReplaceWithValidImagePath); // Set the image of the button
    buttonFirst.DefaultEnabled = true;
    ribbonGroup.Controls.Add(buttonFirst);
    
  4. Initialize CommandBarButton events

    // Attach event handlers to the first button to handle user actions  
    buttonFirst.ExecuteCommand += Button_ExecuteCommand;
    

Example

This example provides information on creating buttons in RobotStudio.

public void CreateButton()
{
    //Begin UndoStep
    Project.UndoContext.BeginUndoStep("Add Buttons");

    try
    {
        // Create a new tab
        RibbonTab ribbonTab = new RibbonTab("MyTab", "MyTab");
        UIEnvironment.RibbonTabs.Add(ribbonTab);

        // Set ribbonTab as the active tab
        UIEnvironment.ActiveRibbonTab = ribbonTab;

        // Create a group for buttons
        RibbonGroup ribbonGroup = new RibbonGroup("MyButtons", "MyButtons");

        // Create first button
        CommandBarButton buttonFirst = new CommandBarButton("MyFirstButton", "MyFirstButton");
        buttonFirst.HelpText = "Help text for small button";
        buttonFirst.Image = Image.FromFile(ReplaceWithValidImagePath); // Set the image of the button
        buttonFirst.DefaultEnabled = true;
        ribbonGroup.Controls.Add(buttonFirst);

        // Include seperator between buttons
        CommandBarSeparator separator = new CommandBarSeparator();
        ribbonGroup.Controls.Add(separator);

        // Create second button
        CommandBarButton buttonSecond = new CommandBarButton("MySecondButton", "MySecondButton");
        buttonSecond.HelpText = "Help text for large button";
        buttonSecond.Image = Image.FromFile(ReplaceWithValidImagePath); // Set the image of the button
        buttonSecond.DefaultEnabled = true;
        ribbonGroup.Controls.Add(buttonSecond);

        // Set the size of the buttons
        RibbonControlLayout[] ribbonControlLayout = { RibbonControlLayout.Small, RibbonControlLayout.Large };
        ribbonGroup.SetControlLayout(buttonFirst, ribbonControlLayout[0]);
        ribbonGroup.SetControlLayout(buttonSecond, ribbonControlLayout[1]);

        // Add ribbon group to ribbon tab
        ribbonTab.Groups.Add(ribbonGroup);

        // Attach event handlers to the first button to handle user actions  
        buttonFirst.ExecuteCommand += Button_ExecuteCommand;
    }
    catch
    {
        Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
        throw;
    }
    finally
    {
        Project.UndoContext.EndUndoStep();
    }
}
private static void Button_ExecuteCommand(object sender, ExecuteCommandEventArgs e)
{
    // Perform any action like creating paths and targets
}

Required Namespaces

ABB.Robotics.RobotStudio.Environment

See Also

  • Creating SplitButton
  • Creating PopupButton
In this article
Back to top Copyright © 2025 ABB