problem timer and winform...

Created at 03 Oct 2019, 11:54
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!
XA

xavier.affringue

Joined 06.09.2019

problem timer and winform...
03 Oct 2019, 11:54


hi,

i begin with winform,and i don't know why when i use a timer, itcause the crash of ctrader...

in this exemple i want to see the current symbol ASK price in my winform.

if somebody can help me.

thanks

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;


namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NewIndicator : Indicator
    {
        private Form _f1;
        private Label label1;
        private Thread _thread;
        private System.Windows.Forms.Timer timer1;

        protected override void Initialize()
        {
            // Initialize and create nested indicators
            #region form
            _thread = new Thread(new ThreadStart(WindowsApp));
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
            while (_thread.IsAlive)
                Application.DoEvents();
            #endregion
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
        }
        [STAThread()]
        public void WindowsApp()
        {
            _f1 = new Form();
            label1 = new Label();
            InitializeComponent();
            // 
            // Form1
            // 
            _f1.AutoScaleDimensions = new SizeF(6f, 13f);
            _f1.AutoScaleMode = AutoScaleMode.Font;
            _f1.ClientSize = new Size(284, 261);
            _f1.Controls.Add(label1);
            _f1.Name = "Form1";
            _f1.Text = "Form1";
            _f1.Load += new EventHandler(Form1_Load);
            _f1.ResumeLayout(false);
            _f1.PerformLayout();

            Application.Run(_f1);
            
        }
        private void InitializeComponent()
        {


            // 
            // label1
            // 
            label1.AutoSize = true;
            label1.Location = new System.Drawing.Point(86, 65);
            label1.Name = "label1";
            label1.Size = new System.Drawing.Size(35, 13);
            label1.TabIndex = 0;
            // 
            // timer1
            // 
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Enabled = true;
            timer1.Interval = 100;


        }
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = Symbol.Ask.ToString();
            timer1.Start();
        }


    }
}

 


@xavier.affringue
Replies

PanagiotisCharalampous
03 Oct 2019, 12:08

Hi xavier.affringue,

It seems you just copied and pasted code expecitng it to work. To add a WinForm to your project, it is better to edit the indicator in Visual Studio and add a new form as a new class. Then initialize it and load it from the Indicator. Also, you cannot access the Symbol information from a form since this is not a property of the form but a property of the indicator. You need to pass this information as a parameter.

Best Regards,

Panagiotis


@PanagiotisCharalampous

xavier.affringue
03 Oct 2019, 12:14

ok

so how can i do this?

i try to add winforms in visual studio from my indicator but in dont understand how all it work.


@xavier.affringue

PanagiotisCharalampous
03 Oct 2019, 12:37 ( Updated at: 21 Dec 2023, 09:21 )

Hi xavier.affringue,

See an example below

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private Form1 _f1;
        private Thread _thread;

        protected override void Initialize()
        {
            _f1 = new Form1();
            _thread = new Thread(() => _f1.ShowDialog());
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();

        }

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


Best Regards,

Panagiotis


@PanagiotisCharalampous

xavier.affringue
03 Oct 2019, 13:05

thank you very much.

but where i need write the text to see the value of symbol ask tothe label of the form?


@xavier.affringue

PanagiotisCharalampous
03 Oct 2019, 14:18

Hi xavier.affringue,

Yuo will need to create a public function in the form, pass the price as a parameter and print the value on a label.

Best Regards,

Panagiotis


@PanagiotisCharalampous

xavier.affringue
03 Oct 2019, 14:34

in form designer? can you show me an exemple please?


@xavier.affringue

PanagiotisCharalampous
03 Oct 2019, 14:35

Hi xavier.affringue,

Yes you can add a label in the form designer as well. Exactly as you would in a typical WinForms project.

Best Regards,

Panagiotis


@PanagiotisCharalampous

xavier.affringue
03 Oct 2019, 14:37

and to pass the price as a parameter and print in the label?


@xavier.affringue

PanagiotisCharalampous
04 Oct 2019, 08:27

Hi xavier.affringue,

I have explained this above. You need to create a public function in the form, pass the price as a parameter and print the value on a label.

Best Regards,

Panagiotis


@PanagiotisCharalampous

xavier.affringue
04 Oct 2019, 08:58

 

thank you very much i managed to post the price in my win form.


@xavier.affringue

dordkash@gmail.com
23 Mar 2020, 06:37 ( Updated at: 21 Dec 2023, 09:21 )

RE:

PanagiotisCharalampous said:

Hi xavier.affringue,

See an example below

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private Form1 _f1;
        private Thread _thread;

        protected override void Initialize()
        {
            _f1 = new Form1();
            _thread = new Thread(() => _f1.ShowDialog());
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();

        }

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


Best Regards,

Panagiotis

Dear PanagiotisCharalampous

Please paste this code with a button or Stakepanel
I'm confused and this is very helpful
Thankful


@dordkash@gmail.com

PanagiotisCharalampous
23 Mar 2020, 09:22 ( Updated at: 21 Dec 2023, 09:21 )

RE: RE:

dordkash@gmail.com said:

PanagiotisCharalampous said:

Hi xavier.affringue,

See an example below

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private Form1 _f1;
        private Thread _thread;

        protected override void Initialize()
        {
            _f1 = new Form1();
            _thread = new Thread(() => _f1.ShowDialog());
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();

        }

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


Best Regards,

Panagiotis

Dear PanagiotisCharalampous

Please paste this code with a button or Stakepanel
I'm confused and this is very helpful
Thankful

Hi,

Where do you want to add the button or stackpanel?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

dordkash@gmail.com
23 Mar 2020, 10:01 ( Updated at: 21 Dec 2023, 09:21 )

RE: RE: RE:

PanagiotisCharalampous said:

dordkash@gmail.com said:

PanagiotisCharalampous said:

Hi xavier.affringue,

See an example below

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private Form1 _f1;
        private Thread _thread;

        protected override void Initialize()
        {
            _f1 = new Form1();
            _thread = new Thread(() => _f1.ShowDialog());
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();

        }

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


Best Regards,

Panagiotis

Dear PanagiotisCharalampous

Please paste this code with a button or Stakepanel
I'm confused and this is very helpful
Thankful

Hi,

Where do you want to add the button or stackpanel?

Best Regards,

Panagiotis 

Join us on Telegram

 

In this code

I would like to put a Button or a Stakepanel in the window that is made in this code
The same form window that is created by VS


@dordkash@gmail.com

PanagiotisCharalampous
23 Mar 2020, 10:21

Hi,

If you mean the chart UI controls, then this is not possible. WinForms have their own controls. You can drag them on the form editor from Visual Studio toolbox.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

dordkash@gmail.com
23 Mar 2020, 10:35

RE:

PanagiotisCharalampous said:

Hi,

If you mean the chart UI controls, then this is not possible. WinForms have their own controls. You can drag them on the form editor from Visual Studio toolbox.

Best Regards,

Panagiotis 

Join us on Telegram

Could you tell me what command I should transfer to VS?
So that I can get on the button on form
I was looking for an example of how to do this


@dordkash@gmail.com