Panel CButton Onclick ChartEvent not working

 

Hello Members,

I am trying to get onclick event for the panel and dialogue CButton. But is not working. Its working for other control types like Combo box etc. But not working with buttons. 

How to solve this issue?

#include <Controls\Dialog.mqh>
#include <Controls\Label.mqh>
#include <Controls\Panel.mqh>
#include <Controls\Button.mqh>

//+------------------------------------------------------------------+
//| Global parameters                                                |
//+------------------------------------------------------------------+
int      panelXX     =  20;
int      panelYY     =  20;
int      panelWidth  =  300;
int      panelHeight =  300;
//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
//--- Panel itself
CAppDialog m_panel;
CButton tbt;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Panel create
   m_panel.Create(0,"TEST PANEL",0,panelXX,panelYY,panelWidth,panelHeight);

//--- Button 
   tbt.Create(0,"btn",0,100,50,15,20);
   tbt.ColorBackground(clrAliceBlue);
   tbt.Text("Settings");
   tbt.FontSize(10);
    tbt.ZOrder(100);
   m_panel.Add(tbt);

//--- Run panel
   m_panel.Run();
//--- succeed
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//------------------
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- Destroy panel
   m_panel.Destroy(reason);
//--- Delete all objects
   ObjectsDeleteAll(0,0);
   Comment("");
  }
//+------------------------------------------------------------------+                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,         // event ID
                  const long& lparam,   // event parameter of the long type
                  const double& dparam, // event parameter of the double type
                  const string& sparam) // event parameter of the string type
  {
//--- Move the panel with the mouse
   m_panel.ChartEvent(id,lparam,dparam,sparam);
 if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam=="btn")
        {
        printf("test");
        }
     }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

Thank you in advance.

 
Somsri Sarkar:

Hello Members,

I am trying to get onclick event for the panel and dialogue CButton. But is not working. Its working for other control types like Combo box etc. But not working with buttons. 

How to solve this issue?

Thank you in advance.

To create a button on each tick can not work, fix your code

 
amando:

To create a button on each tick can not work, fix your code

Hello Amando,

Thank you for your reply.

I have fixed my code, move the button create part to OnInit().

But still clicking on the button not triggering "CHARTEVENT_OBJECT_CLICK" part.

 
 
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,         // Event identifier  
                  const long& lparam,   // Event parameter of long type
                  const double& dparam, // Event parameter of double type
                  const string& sparam) // Event parameter of string type
  {
//--- the left mouse button has been pressed on the chart
   if(id==CHARTEVENT_CLICK)
     {
      Print("The coordinates of the mouse click on the chart are: x = ",lparam,"  y = ",dparam);
     }
//--- the mouse has been clicked on the graphic object
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      Print("The mouse has been clicked on the object with name '"+sparam+"'");
     }
//--- the key has been pressed
   if(id==CHARTEVENT_KEYDOWN)
     {
      Print("The key with code ",int(lparam)," has been pressed");
     }
//--- the object has been deleted
   if(id==CHARTEVENT_OBJECT_DELETE)
     {
      Print("The object with name ",sparam," has been deleted");
     }
//--- the object has been created
   if(id==CHARTEVENT_OBJECT_CREATE)
     {
      Print("The object with name ",sparam," has been created");
     }
//--- the object has been moved or its anchor point coordinates has been changed
   if(id==CHARTEVENT_OBJECT_DRAG)
     {
      Print("The anchor point coordinates of the object with name ",sparam," has been changed");
     }
//--- the text in the Edit of object has been changed
   if(id==CHARTEVENT_OBJECT_ENDEDIT)
     {
      Print("The text in the Edit field of the object with name ",sparam," has been changed");
     }
  }
//+------------------------------------------------------------------+
 
Marco vd Heijden:

Thank you for the code.

Here click on the button, showing its clicking on the "Clientback" instead of "btn" (button name), Why?

 
Okay please set 
OBJPROP_ZORDER

so that your button will have priority over the other layers.

OBJPROP_ZORDER

Priority of a graphical object for receiving events of clicking on a chart (CHARTEVENT_CLICK). The default zero value is set when creating an object; the priority can be increased if necessary. When objects are placed one atop another, only one of them with the highest priority will receive the CHARTEVENT_CLICK event.

long

https://www.mql5.com/en/docs/standardlibrary/controls/cwndobj/cwndobjzorder

Documentation on MQL5: Standard Library / Panels and Dialogs / CWndObj / ZOrder
Documentation on MQL5: Standard Library / Panels and Dialogs / CWndObj / ZOrder
  • www.mql5.com
ZOrder - CWndObj - Panels and Dialogs - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Somsri Sarkar:

Thank you for the code.

Here click on the button, showing its clicking on the "Clientback" instead of "btn" (button name), Why?

your button Description is "Settings", read the documentation

tbt.Text("Settings");

There a couple of codes with panels in the codebase,  I suggest, download one and study it

 
Marco vd Heijden:
Okay please set 

so that your button will have priority over the other layers.

OBJPROP_ZORDER

Priority of a graphical object for receiving events of clicking on a chart (CHARTEVENT_CLICK). The default zero value is set when creating an object; the priority can be increased if necessary. When objects are placed one atop another, only one of them with the highest priority will receive the CHARTEVENT_CLICK event.

long

https://www.mql5.com/en/docs/standardlibrary/controls/cwndobj/cwndobjzorder

Thank you for the idea,

I did that 

  tbt.ZOrder(100);

Still its clicking on the layer. Strange issue, for other control types, I can easily check click events. But only for the buttons its not working. 

 
int OnInit()
  {
//--- Panel create
   m_panel.Create(0,"TEST PANEL",0,panelXX,panelYY,panelWidth,panelHeight);

//--- Button 
   tbt.Create(0,"btn",0,100,50,15,20);
   tbt.ColorBackground(clrAliceBlue);
   tbt.Text("Settings");
   tbt.FontSize(10);
    tbt.ZOrder(100);
   m_panel.Add(tbt);

//--- Run panel
   m_panel.Run();
//--- succeed
   return(INIT_SUCCEEDED);
  }

do you have any idea what this code in your OnInit is doing?

you create an Panel, and then you create a Button, what have absolut no connection to the panel.

even what ist

tbt.Text=

for the description of the Button use Description


then you Add the button to the Panel?? Did you check if this is working?


and then you have in OnChartevent

m_panel.ChartEvent(id,lparam,dparam,sparam);

did you check the Function?

 
amando:

do you have any idea what this code in your OnInit is doing?

you create an Panel, and then you create a Button, what have absolut no connection to the panel.

even what ist

tbt.Text=

for the description of the Button use Description


then you Add the button to the Panel?? Did you check if this is working?


and then you have in OnChartevent

did you check the Function?

Hello Amando,

Thank you for having patience with me.

I am new to Panel.

   m_panel.Add(tbt);

I thought above code connects tbt button with the Panel like other controls.

 tbt.Text("Settings");

This .Text is used for button text as mentioned in the standard library.

https://www.mql5.com/en/docs/standardlibrary/controls/cwndobj/cwndobjtext

With  OnChartevent I did checked the function as mentioned in the previous message.

Only thing is button is visible on the chart panel. But while clicking on it, its showing its clicking on the "Client back". So I am confused here. Other controls are working fine with Onclick event.

Documentation on MQL5: Standard Library / Panels and Dialogs / CWndObj / Text
Documentation on MQL5: Standard Library / Panels and Dialogs / CWndObj / Text
  • www.mql5.com
Text - CWndObj - Panels and Dialogs - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Debug the add function.

  if(!m_panel.Add(tbt)){
  printf("failed");
  }

Add(tbt) is working. Showing button is connected with the panel.

Reason: