TextBox

Created at 18 Mar 2021, 03:20
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
WI

WienAT

Joined 07.04.2020

TextBox
18 Mar 2021, 03:20


Dear all,

i am using Text block:

                var Overall1 = new TextBlock
                {
                    BackgroundColor = Color.Green,
                    Text = "WinRatio Overall: " + WinRatio + " %"
                };

Can i change color according to value of one variable? If variable xy > 0, then Color.Green


@WienAT
Replies

winsonet
18 Mar 2021, 15:21

I don't know what's the problems of your case, if you want to change the background color, just set it's color should be ok:

 

Overall1.BackgroundColor = Color.Red;

but I don't know when and where will you want to check the variable for do this? 


@winsonet

WienAT
18 Mar 2021, 18:34

RE:

winsonet said:

I don't know what's the problems of your case, if you want to change the background color, just set it's color should be ok:

 

Overall1.BackgroundColor = Color.Red;

but I don't know when and where will you want to check the variable for do this? 

I will try like you suggested.

 

I have one textbox where some text is presented (let say that also value of currenct profit is shown).

if profit is more then 0, then i would like to have green background ... if profit is less then 0, then i would like to have red background ...

If i understood you correctly, i should od like this:

 

if (profit > 0)
Overall1.BackgroundColor = Color.Green;
else
Overall1.BackgroundColor = Color.Red;

 


@WienAT

amusleh
19 Mar 2021, 09:24

First make your text box globally accessible by making it a private field inside your cBot/indicator class, then you can change any of its properties anywhere based on any condition, this sample indicator might be helpful for you:

using cAlgo.API;

namespace cAlgo
{
    /// <summary>
    /// This is a sample of how to catch or handle the keyboard events on chart
    /// </summary>
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ChartKeyboardSample : Indicator
    {
        private TextBlock _keyDownTextBlock, _keyCombinationTextBlock;

        protected override void Initialize()
        {
            var stackPanel = new StackPanel 
            {
                Orientation = Orientation.Vertical,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                BackgroundColor = Color.Gold,
                Opacity = 0.7,
                Width = 200
            };

            stackPanel.AddChild(new TextBlock 
            {
                Text = "Keyboard Handler",
                FontWeight = FontWeight.ExtraBold,
                Margin = 5,
                HorizontalAlignment = HorizontalAlignment.Center,
                ForegroundColor = Color.Black
            });

            var grid = new Grid(2, 2);

            grid.AddChild(new TextBlock 
            {
                Text = "Key Down",
                Margin = 5,
                ForegroundColor = Color.Black
            }, 0, 0);

            _keyDownTextBlock = new TextBlock 
            {
                Margin = 5,
                ForegroundColor = Color.Black
            };

            grid.AddChild(_keyDownTextBlock, 0, 1);

            grid.AddChild(new TextBlock 
            {
                Text = "Key Combination",
                Margin = 5,
                ForegroundColor = Color.Black
            }, 1, 0);

            _keyCombinationTextBlock = new TextBlock 
            {
                Margin = 5,
                ForegroundColor = Color.Black
            };

            grid.AddChild(_keyCombinationTextBlock, 1, 1);

            stackPanel.AddChild(grid);

            Chart.AddControl(stackPanel);

            Chart.KeyDown += Chart_KeyDown;
        }

        private void Chart_KeyDown(ChartKeyboardEventArgs obj)
        {
            _keyDownTextBlock.Text = obj.Key.ToString();

            _keyCombinationTextBlock.Text = string.Empty;

            if (obj.AltKey)
                _keyCombinationTextBlock.Text += "Alt, ";
            if (obj.ShiftKey)
                _keyCombinationTextBlock.Text += "Shift, ";
            if (obj.CtrlKey)
                _keyCombinationTextBlock.Text += "Ctrl, ";
        }

        public override void Calculate(int index)
        {
        }
    }
}

 


@amusleh

winsonet
19 Mar 2021, 14:21

RE: RE:

WienAT said:

winsonet said:

I don't know what's the problems of your case, if you want to change the background color, just set it's color should be ok:

 

Overall1.BackgroundColor = Color.Red;

but I don't know when and where will you want to check the variable for do this? 

I will try like you suggested.

 

I have one textbox where some text is presented (let say that also value of currenct profit is shown).

if profit is more then 0, then i would like to have green background ... if profit is less then 0, then i would like to have red background ...

If i understood you correctly, i should od like this:

 

if (profit > 0)
Overall1.BackgroundColor = Color.Green;
else
Overall1.BackgroundColor = Color.Red;

 

Yes, and if you want to base on current profit to dynamic to change it, you can put the codes in 

public override void Calculate(int index)
{
     //your logic for calculate the profit
     //update the background color
}

 


@winsonet

WienAT
21 Mar 2021, 01:44

tnx :) It's working :)


@WienAT