KeyDown does not work in winform

Created at 15 Aug 2019, 19:43
PI

piter

Joined 15.08.2019

KeyDown does not work in winform
15 Aug 2019, 19:43


Hello.

Below I have the code with the winform application in which I want, after pressing the appropriate key, e.g. "K", cTrader performed the operation (in this example, printing the text).
Unfortunately, the following code does not work. Is anyone able to help me with this problem?

 

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();
            
        }

        protected override void OnTick()
        {
            
        }

        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.KeyDown += Form_KeyDown;
            form.ResumeLayout(false);
            form.PerformLayout();
            Application.Run(form);
        }

        private void Form_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 75)
            {
                Print("cokolwiek");
            }
        }

        //
        // PButton "TEST"
        //        
        private void button1_Click(object sender, EventArgs e)
        {
            Print("Button test");
            Chart.DrawStaticText("tekscik", "test formy", VerticalAlignment.Center, API.HorizontalAlignment.Center, API.Color.Aqua);
            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();
        }
    }
}

 


@piter
Replies

PanagiotisCharalampous
26 Aug 2019, 10:05

Hi piterpaw@gmail.com,

Thanks for posting in our forum. It seems that you are just copying and pasing a windows form cBot inside a cBot and expect it work. Maybe you can consider contacting a Consultant to help you with your cBot development.

Best Regards,

Panagiotis


@PanagiotisCharalampous

piter
27 Aug 2019, 21:11

Everything works, I just forgot about the KeyPreview property :)


@piter