Help with windows forms
Help with windows forms
30 Jan 2019, 11:56
Hello.
I'm trying to learn how to use windows forms in cTrader. Why in this example cBot does not perform "Print (" OnTick test ")".
Thank you in advance for your help.
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; using System.Drawing; using System.Windows.Forms; using System.Data; using System.Threading; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)] public class FormRobot1test : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } private Thread thread; private Form form; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.ComboBox comboBox1; [STAThread()] protected override void OnStart() { thread = new Thread(new ThreadStart(WindowsApp)); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); while (thread.IsAlive) System.Windows.Forms.Application.DoEvents(); } protected override void OnTick() { Print("OnTick test"); } protected override void OnStop() { // Put your deinitialization logic here } [STAThread()] private void WindowsApp() { form = new Form(); InitializeComponent(); form.AutoScaleDimensions = new System.Drawing.SizeF(8f, 16f); form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; form.ClientSize = new System.Drawing.Size(443, 633); form.Controls.Add(this.groupBox1); form.Name = "Form1"; form.Text = "Test"; form.TopMost = true; form.ResumeLayout(false); form.PerformLayout(); Application.Run(form); } // // PButton "TEST" // private void button1_Click(object sender, EventArgs e) { Print("Button test"); int comboBox1Value = Convert.ToInt32(comboBox1.SelectedItem); Print("ComboBox1 = " + comboBox1Value); } private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.button1 = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.comboBox1); this.groupBox1.Controls.Add(this.button1); this.groupBox1.Location = new System.Drawing.Point(0, 0); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(444, 225); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "groupBox1"; // // comboBox1 // this.comboBox1.FormattingEnabled = true; this.comboBox1.Items.AddRange(new object[] { 1, 2, 3, 5, 10 }); this.comboBox1.Location = new System.Drawing.Point(15, 71); this.comboBox1.Name = "comboBox10"; this.comboBox1.Size = new System.Drawing.Size(61, 24); this.comboBox1.TabIndex = 4; // // button1 test // this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0))))); this.button1.Cursor = System.Windows.Forms.Cursors.Hand; this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238))); this.button1.Location = new System.Drawing.Point(295, 21); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(122, 40); this.button1.TabIndex = 2; this.button1.Text = "TEST"; this.button1.UseVisualStyleBackColor = false; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); } } }
Replies
mrha1000
31 Mar 2020, 00:06
RE:
PanagiotisCharalampous said:
Hi lw3010996,
if you remove this piece of code
thread.Join(); while (thread.IsAlive) System.Windows.Forms.Application.DoEvents();It should work fine.
Best Regards,
Panagiotis
I'm also trying to learn
I made a new bot and copied the code into it
Then I hit Edith With Visual Studio(2017)
Then I right-clicked it in Visual Studio and then Add -----> Windows Form
And I added a Windows Form to it
But I get an error when I hit Boild
Even delete what you said
please guide me
@mrha1000
PanagiotisCharalampous
31 Mar 2020, 08:30
Hi mrha1000,
You need to provide more information. What error do your get?
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
02 Apr 2020, 08:20
Hi mrha1000,
You need to change that line of code to the following
System.Windows.Forms.Application.Run(form);
since there is a conflict with the cBot's Application property.
Best Regards,
Panagiotis
@PanagiotisCharalampous
mrha1000
02 Apr 2020, 17:13
RE:
PanagiotisCharalampous said:
Hi mrha1000,
You need to change that line of code to the following
System.Windows.Forms.Application.Run(form);
since there is a conflict with the cBot's Application property.
Best Regards,
Panagiotis
Thankful
Corrected with this code
@mrha1000
dwalkhul
29 Dec 2020, 08:44
( Updated at: 29 Dec 2020, 08:56 )
Hello - A test template -> any pointer how to get this to work - see Call KBE. I have tried numerous approaches without success. Meaning - I can't get past errors calling KBE to test if it will work. Thank you.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Windows.Forms;
namespace Key_Test
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class KeyTest : Robot
{
[Parameter("SL", Group = "Parameters", DefaultValue = 0)]
public int defaultSL { get; set; }
[Parameter("TP", Group = "Parameters", DefaultValue = 0)]
public int defaultTP { get; set; }
[Parameter("Lots", Group = "Parameters", DefaultValue = 0.01)]
public double defaultLots { get; set; }
private string label = "KeyTest";
protected override void OnStart()
{
// Call KBE ????
}
private void KBE(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Z)
{
allClose();
}
else if (e.KeyCode == Keys.B)
{
entry(TradeType.Buy);
}
else if (e.KeyCode == Keys.S)
{
entry(TradeType.Sell);
}
}
private void entry(TradeType type)
{
var order = ExecuteMarketOrder(type, SymbolName, defaultLots * 100000, label, defaultSL, defaultTP);
if (!order.IsSuccessful)
{
Print("Error:{0}", order.Error.ToString());
}
}
private void allClose()
{
var positions = Positions.FindAll(label);
foreach (Position p in positions)
{
ClosePosition(p);
}
}
}
}
@dwalkhul
PanagiotisCharalampous
29 Dec 2020, 08:58
Hi mrha1000,
You seem to be using an event handler without an event. Did you just copy and paste this from somewhere?
Best Regards,
Panagiotis
@PanagiotisCharalampous
dwalkhul
29 Dec 2020, 14:29
( Updated at: 29 Dec 2020, 17:42 )
RE:
PanagiotisCharalampous said:
Hi mrha1000,
You seem to be using an event handler without an event. Did you just copy and paste this from somewhere?
Best Regards,
Panagiotis
Did you just copy and paste this from somewhere? Fair question, but no.
I found numerous variation repeats of the below with regards C# Key Events
--------------------------------------------------------
Control.KeyPress Event
Definition
Namespace:
Assembly:
System.Windows.Forms.dll
-----------------------------------------------------
So went with
using System.Windows.Forms;
which first required invoking Manage References >> .NET Framework >> System.Windows.Forms
The rest is straight forward standard code.
My original request: A test template -> any pointer how to get this to work - see Call KBE. I have tried numerous approaches without success. Meaning - I can't get past errors calling KBE to test if it will work. Thank you.
C# examples merely illustrate calling KBE as KBE(sender, e); but this produces error -
You seem to be using an event handler without an event. ?
mmm ... yes..,.I thought I could call KBE and KBE would detect the relevant Key Events.
@dwalkhul
PanagiotisCharalampous
30 Dec 2020, 08:15
Hi dwalkhul,
mmm ... yes..,.I thought I could call KBE and KBE would detect the relevant Key Events.
This will not happen. An event handler will be triggered only if you assign it to an event. Read more here.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
30 Jan 2019, 12:14
Hi lw3010996,
if you remove this piece of code
It should work fine.
Best Regards,
Panagiotis
@PanagiotisCharalampous