Creating a Button
This example provides information on creating Command
Solution
Create Ribbon
Tab(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;
Create Ribbon
Group(string, string) // Create a group for buttons RibbonGroup ribbonGroup = new RibbonGroup("MyButtons", "MyButtons");
Create and add a Command
Bar Button(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);
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.