Creating a CustomButton
This example provides information on creating custom commandbar controls like textbox, combobox, etc in RobotStudio.
Solution
Create a Ribbon
Group(string, string) // Create a new group in the ribbon for our buttons RibbonGroup customControlGroup = new RibbonGroup("MyCustomControls", "MyCustom Control");
Create Command
Bar controlCombo Box(string) // Create combobox button control CommandBarComboBox buttonComboBox = new CommandBarComboBox("MyModule"); buttonComboBox.Caption = "Procedure"; buttonComboBox.Image = Image.FromFile(ReplaceWithValidImagePath); buttonComboBox.HelpText = "Select a procedure to execute from the dropdown list."; // Add event handler triggered by pressing the dropdown button. buttonComboBox.DropDown += BtnComboBox_DropDown;
Create two Command
Bar and add them to CommandBarComboBoxCombo Box Item(string) // Add two items to the combo box for selection. CommandBarComboBoxItem cmbBoxItem1 = new CommandBarComboBoxItem("procedure1"); CommandBarComboBoxItem cmbBoxItem2 = new CommandBarComboBoxItem("procedure2"); buttonComboBox.Items.Add(cmbBoxItem1); buttonComboBox.Items.Add(cmbBoxItem2);
Add EventHandler, and add the CommandBarComboBox to the ribbon group
// Add event handler triggered by changing the selected item. buttonComboBox.SelectionChanged += BtnComboBox_SelectionChanged; buttonComboBox.SelectedIndex = 0; // Add control to ribbon group customControlGroup.Controls.Add(buttonComboBox);
Create and set Ribbon
Control Layout // Set 'Small' layout for buttonComboBox RibbonControlLayout[] comboBoxLayout = new RibbonControlLayout[] { RibbonControlLayout.Small, RibbonControlLayout.Small }; customControlGroup.SetControlLayout(buttonComboBox, comboBoxLayout);
Add RibbonGroup to Ribbon
Tab(string, string) // Add ribbon group to the active ribbon tab UIEnvironment.ActiveRibbonTab.Groups.Add(customControlGroup);
Example
This example provides information on creating custom command bar controls in RobotStudio.
public void CustomControl()
{
// Begin UndoStep
Project.UndoContext.BeginUndoStep("AddCustomControlButtons");
try
{
// Create a new group in the ribbon for our buttons
RibbonGroup customControlGroup = new RibbonGroup("MyCustomControls", "MyCustom Control");
// Create combobox button control
CommandBarComboBox buttonComboBox = new CommandBarComboBox("MyModule");
buttonComboBox.Caption = "Procedure";
buttonComboBox.Image = Image.FromFile(ReplaceWithValidImagePath);
buttonComboBox.HelpText = "Select a procedure to execute from the dropdown list.";
// Add event handler triggered by pressing the dropdown button.
buttonComboBox.DropDown += BtnComboBox_DropDown;
// Add two items to the combo box for selection.
CommandBarComboBoxItem cmbBoxItem1 = new CommandBarComboBoxItem("procedure1");
CommandBarComboBoxItem cmbBoxItem2 = new CommandBarComboBoxItem("procedure2");
buttonComboBox.Items.Add(cmbBoxItem1);
buttonComboBox.Items.Add(cmbBoxItem2);
// Add event handler triggered by changing the selected item.
buttonComboBox.SelectionChanged += BtnComboBox_SelectionChanged;
buttonComboBox.SelectedIndex = 0;
// Add control to ribbon group
customControlGroup.Controls.Add(buttonComboBox);
// Set 'Small' layout for buttonComboBox
RibbonControlLayout[] comboBoxLayout = new RibbonControlLayout[] { RibbonControlLayout.Small, RibbonControlLayout.Small };
customControlGroup.SetControlLayout(buttonComboBox, comboBoxLayout);
// Create TextBox
TextBox dataInputTextBox = new TextBox();
dataInputTextBox.Enabled = true;
dataInputTextBox.ReadOnly = false;
// Set appearence
dataInputTextBox.BackColor = Color.Black;
dataInputTextBox.ForeColor = Color.SpringGreen;
dataInputTextBox.TextAlign = HorizontalAlignment.Right;
dataInputTextBox.Text = "Type here";
dataInputTextBox.Width = 150;
// Create a CommandBarCustomControl from the TextBox
CommandBarCustomControl buttonTextBox = new CommandBarCustomControl("MyTextBox", dataInputTextBox);
buttonTextBox.Caption = "Data";
buttonTextBox.Image = Image.FromFile(ReplaceWithValidImagePath);
buttonTextBox.HelpText = "Displays data typed";
// Add control to ribbon group
customControlGroup.Controls.Add(buttonTextBox);
// Set 'Small' layout for buttonTextBox
RibbonControlLayout[] textBoxlayout = new RibbonControlLayout[] { RibbonControlLayout.Small, RibbonControlLayout.Small };
customControlGroup.SetControlLayout(buttonTextBox, textBoxlayout);
// Add ribbon group to the active ribbon tab
UIEnvironment.ActiveRibbonTab.Groups.Add(customControlGroup);
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}
}
static void BtnComboBox_SelectionChanged(object sender, EventArgs e)
{
//Called when index in comboxbox changes
}
static void BtnComboBox_DropDown(object sender, EventArgs e)
{
//Called when combobox is clicked
}
Required Namespaces
ABB.