Skip to content

CustomControl

class cAlgo.API

DesktopCLICloud

Represents the custom control to create reusable controls with customized look and behavior.

Environment behaviour

CLI — not supported. Accepted without error but never displayed, so control events such as Click are never raised.

Cloud — not supported. Accepted without error but never displayed, so control events such as Click are never raised.

Definition

public abstract class CustomControl : ControlBase

Namespace: cAlgo.API · Group: Controls · Inherits: ControlBase

Remarks

Using CustomControl is currently not possible in Python algos.

Examples

using cAlgo.API;
using System.Collections.Generic;

namespace cAlgo
{
    // This sample indicator shows how to use Chart Custom controls to create your own controls
    // by combining multiple built-in controls, the combobox control is not a fully functioning combobox, its just for learning purpose
    public partial class CustomControlSample : Indicator
    {
        protected override void Initialize()
        {
            var comboBox = new ComboBox
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

            comboBox.AddItem("Item 1");
            comboBox.AddItem("Item 2");
            comboBox.AddItem("Item 3");
            comboBox.AddItem("Item 4");
            comboBox.AddItem("Item 5");

            Chart.AddControl(comboBox);
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
        }
    }

    public class ComboBox : CustomControl
    {
        private TextBox _textBox;

        private Button _button;

        private Grid _itemsGrid;

        private StackPanel _panel;

        private readonly List<object> _items = new List<object>();

        private bool _isExpanded;

        public ComboBox()
        {
            _textBox = new TextBox
            {
                Width = 100,
                IsReadOnly = true,
                IsReadOnlyCaretVisible = false
            };

            _button = new Button
            {
                Text = "â–¼"
            };

            _button.Click += Button_Click;

            var stackPanel = new StackPanel
            {
                Orientation = Orientation.Horizontal
            };

            stackPanel.AddChild(_textBox);
            stackPanel.AddChild(_button);

            _panel = new StackPanel
            {
                Orientation = Orientation.Vertical
            };

            _panel.AddChild(stackPanel);

            AddChild(_panel);
        }

        public void AddItem(object item)
        {
            _items.Add(item);
        }

        public bool RemoveItem(object item)
        {
            return _items.Remove(item);
        }

        private void Button_Click(ButtonClickEventArgs obj)
        {
            if (_itemsGrid != null)
                _panel.RemoveChild(_itemsGrid);

            if (_isExpanded)
            {
                _isExpanded = false;

                return;
            }

            _isExpanded = true;

            _itemsGrid = new Grid(_items.Count, 1);

            for (int i = 0; i < _items.Count; i++)
            {
                var item = _items[i];

                _itemsGrid.AddChild(new TextBlock
                {
                    Text = item.ToString()
                }, i, 0);
            }

            _panel.AddChild(_itemsGrid);
        }
    }
}
# Not available in Python: CustomControl subclasses are C# classes, compose built-in controls instead
textBox = TextBox()
textBox.Width = 100
textBox.IsReadOnly = True
button = Button()
button.Text = "v"
panel = StackPanel()
panel.Orientation = Orientation.Horizontal
panel.AddChild(textBox)
panel.AddChild(button)
api.Chart.AddControl(panel)
{
  "AccessRights": "None",
  "TimeZone": "UTC",
  "IsOverlay": true
}

Methods

Name Description
AddChildDesktopCLICloudvoid Adds a child control.

Inherited members

From ControlBase: ResetProperty, UniqueId, Style, Left, Top, Right, Bottom, Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight, IsEnabled, IsVisible, Opacity, Margin, VerticalAlignment, HorizontalAlignment, Dock, IsHitTestVisible