Adding Controls Via Script

Scripting is not supported by TWC

You can dynamically add controls to a screen via script during runtime. This allows you to create a control from Canvas.Controls or other plugin control assemblies you may have added to Canvas.

Some potential reasons why you might want to do this include:

Before You Start

Example

Start with the following simple script example, which generally shows how to create and remove a control during runtime:

Copy
Create and remove a control during runtime
// we can get control view models via Objects.Screen.GetAllScreenControls(), but
// when adding/removing controls, we need access to the view, so it can be easier
// to maintain direct references to the view classes
 
private List<ICanvasControlView> _addedControls = new List<ICanvasControlView>();
 
private void AddControl(int x, int y)
{
    //create a variable with the control’s view
    var shape = new Canvas.Controls.Shape.CanvasShape();
     
    //create a variable with the control’s view model
    var shapeViewModel = shape.ViewModel as Canvas.Controls.Shape.CanvasShapeViewModel;
     
    //modify properties of the view model
    shapeViewModel.X = x;
    shapeViewModel.Y = y;
    shapeViewModel.Height = 20;
    shapeViewModel.Width = 20;
    shapeViewModel.LineStyle = Canvas.Controls.Shape.CanvasShapeViewModel.CanvasShapeLineStyle.Dotted;
     
    //add control’s view to screen
    Objects.Screen.AddControl(shape);
     
    //add control’s view to list to keep track of all added controls
    _addedControls.Add(shape);
}
 
private void RemoveAllControls()
{
    foreach (var controls in _addedControls)
    {
        Objects.Screen.RemoveControl(controls);
    }
    _addedControls.Clear();
}

Scripting Notes

The following notes will get you started.

Canvas.Helpers

The sample script has the following line:

Copy
private List<ICanvasControlView> _addedControls = new List<ICanvasControlView>();

ICanvasControlView requires that you add a using statement for Canvas.Helpers (using Canvas.Helpers) in order for this to work. The using statement for Canvas.Helpers goes with all of the other using statements at the very top of the script. The Canvas.Helpers assembly is added by default, but you need to add the using statement in order to address any errors ICanvasControlView may produce.

Adding Assemblies

When dynamically adding controls, you must add control assemblies as references for the screen. This is done via the Assembly Manager.

Once you have installed the Microsoft .NET Framework 4.7.2 Developer Pack or later , open the Assembly Manger for the scripted screen (click Manage external assemblies loaded for script in the screen's toolbar).

Select System Assemblies and click Use default system path for .NET SDK.

All necessary assemblies will be listed, and you can select the required ones. Review any script errors to determine which assemblies are required. You will need to select those assemblies for inclusion.

Control View, ViewModel, Properties

In general, you must create a variable for the control’s view you are interested in, for example:

Copy
var shape = new Canvas.Controls.Shape.CanvasShape();

Then create a separate variable for the control’s view model, for example:

Copy
var shapeViewModel = shape.ViewModel as Canvas.Controls.Shape.CanvasShapeViewModel;

Then modify the view model’s properties with desired values, for example:

Copy
shapeViewModel.X = x;
shapeViewModel.Y = y;
shapeViewModel.Height = 20;
shapeViewModel.Width = 20;
shapeViewModel.LineStyle = Canvas.Controls.Shape.CanvasShapeViewModel.CanvasShapeLineStyle.Dotted;

Then add the control’s view to the screen, for example:

Copy
Objects.Screen.AddControl(shape);

Control View and ViewModel Location

Most of the control view and view models are located in the same general place (Canvas.Controls.<control>). However, the following controls deviate from this rule:

Chart

The Chart's View and ViewModel are located here:

Navigator

The Navigator's View and ViewModel are located here:

Tag Chooser

The Tag Chooser's View and ViewModel are located here: