Forms

Macros are able to create forms to interact with the user. The example that follows is a simple example of a form, reading the results and displaying them in a custom function. Elements from this example can be copied to build up a more complex form to display.

 

// Sample form to demonstrate use of form controls
CreateForm(); // Create and display the form

function CreateForm() {
// The basic form
  var form = new System.Windows.Forms.Form();
  form.Text = "Show equipment";
  form.ShowIcon = false;
  form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
  form.Size = new System.Drawing.Size(10, 10);
  form.TopMost = true;
  form.AutoSize = true;

// Create a label
  var label1 = new System.Windows.Forms.Label();
  label1.AutoSize = true;
  label1.Location = new System.Drawing.Point(10, 10);
  form.Controls.Add(label1);
  label1.Text = "Please enter the name of the equipment: ";

// Create a text box
  var textBox1 = new System.Windows.Forms.TextBox();
  textBox1.Location = new System.Drawing.Point(10, 35);
  form.Controls.Add(textBox1);
  textBox1.Text = "Conveyor1"; // Set the default

// Create a check box
  var checkBox1 = new System.Windows.Forms.CheckBox();
  checkBox1.Text = "Enabled";
  checkBox1.Location = new System.Drawing.Point(120, 35);
  form.Controls.Add(checkBox1);
  checkBox1.Checked = true; // Set the default

// Create a combo box
  var comboBox1 = new System.Windows.Forms.ComboBox();
  comboBox1.Location = new System.Drawing.Point(10, 60);
  form.Controls.Add(comboBox1);
  comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
// Add Sym3 colors to the combo
  var colours = Project.Colors.Objects;
  for (var i = 0; i < colours.Count; i++)
    comboBox1.Items.Add(colours[i].Name);
  comboBox1.Text = "Sym3 Orange"; // Set the default

// Create two radio buttons
  var radio1 = new System.Windows.Forms.RadioButton();
  var radio2 = new System.Windows.Forms.RadioButton();
  radio1.Text = "Option 1";
  radio2.Text = "Option 2";
  radio1.Location = new System.Drawing.Point(10, 90);
  radio2.Location = new System.Drawing.Point(120, 90);
  form.Controls.Add(radio1);
  form.Controls.Add(radio2);
  radio1.Checked = true; // Set the default

// Create a button to link to a function:
  button1 = new System.Windows.Forms.Button();
  button1.Location = new System.Drawing.Point(10, 120);
  button1.Text = "Execute";
  button1.add_Click(function() {
  try {
    var sValues = "Equipment=" + textBox1.Text
      + ", Colour=" + comboBox1.Text
      + ", Enabled=" + checkBox1.Checked
      + ", Option=" + (radio1.Checked ? "1" : "2")
    myFunction(sValues); // Call this function
  }
  catch(err) {
    print("Error in the macro: " + err);
  }  
  });
  form.Controls.Add(button1);

// Create a cancel button:
  button2 = new System.Windows.Forms.Button();
  button2.Location = new System.Drawing.Point(100, 120);
  button2.Text = "Cancel";
  button2.add_Click(function () {
  try { form.Close() }
    catch (err) { print(err); }
  });
  form.Controls.Add(button2);
  
  form.Show(); // Display the completed form to the user
}

// Display the results from the form
function myFunction(sValue) {
  System.Windows.Forms.MessageBox.Show("myFunction: Values are '"+sValue+"'");
}