How to change textblock text?

Created at 19 Aug 2022, 11:49
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!
HI

hishalv

Joined 25.02.2019

How to change textblock text?
19 Aug 2022, 11:49


I am trying to change text of a textblock I created in OnStart()

protected override void OnStart()
    {
        var textblock = new TextBlock 
        {
            Width = 100,
            Height = 50,
            Left = 200,
            Top = 40,
            Text = "",
            Margin = "2 6 2 2",
            BackgroundColor = Color.Black,
            ForegroundColor = "#39ff14",
            FontSize = 24
        };
        var canvas = new Canvas();
        Chart.AddControl(canvas);
        canvas.AddChild(textblock);
    }

but  cannot access "textblock.Text" to set its text to something else. I am trying to do the following...

protected override void OnTick()
    {
        textblock.Text = "Some new text"
    }

How can I achieve something like this?

thanks


@hishalv
Replies

PanagiotisCharalampous
19 Aug 2022, 11:56

Hi hishalv,

You need to move textblock outside OnStart and make is a class variable so that it is accessible by other members too.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

hishalv
19 Aug 2022, 12:28

RE:

PanagiotisCharalampous said:

Hi hishalv,

You need to move textblock outside OnStart and make is a class variable so that it is accessible by other members too.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Hi Panagiotis,

thanks for the quick reply, however when I tried this I get the error...

"the contexual keyword var may only appear within a local variable declaration or in a script code"

It works if the data type is a string, bool, etc....except a var.

Any thoughts?


@hishalv

PanagiotisCharalampous
19 Aug 2022, 12:46

Hi hishalv,

You need to use the actual type

TextBlock _textBlock

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

hishalv
19 Aug 2022, 12:49

RE:

PanagiotisCharalampous said:

Hi hishalv,

You need to use the actual type

TextBlock _textBlock

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Ah I see. I understand now. I will try that. 

Thank you


@hishalv