Change OBJ_EDIT text from chart event

Hello,

i have a problem that i can’t set the text for an OBJ_EDIT when being within chart event.

The goal: When the edit field is clicked, i want to set a new text. This is not working.

Please check the script. How to accomplish this ? Thank you

#property copyright "chinaski"
#property version   "1.00"
#property strict

string edit_name="edit";
//+------------------------------------------------------------------+
int OnInit()
{
     if(!ObjectCreate(0,edit_name,OBJ_EDIT,0,0,0)) 
     { 
       Print("Failed. Error code = ",GetLastError()); 
       return INIT_FAILED;
     }   
   
   ObjectSetInteger(0, edit_name,OBJPROP_XDISTANCE,100);
   ObjectSetInteger(0, edit_name,OBJPROP_YDISTANCE,100);
   
   ObjectSetInteger(0, edit_name,OBJPROP_XSIZE,200);
   ObjectSetInteger(0, edit_name,OBJPROP_YSIZE,60);
   
   ObjectSetString(0, edit_name,OBJPROP_TEXT,"MY INIT TEXT");

   // set text outside chart event         
   ObjectSetString(0, edit_name,OBJPROP_TEXT,"MY NEW TEXT");
   return INIT_SUCCEEDED;
}
void OnDeinit(const int reason)
{
   ObjectDelete(0,edit_name);
   
   
}

//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
      switch(id)
      {
         case CHARTEVENT_KEYDOWN:
         {
           Print("CHARTEVENT_KEYDOWN");  
         }break;
         case CHARTEVENT_MOUSE_MOVE:
         {
          Print("CHARTEVENT_MOUSE_MOVE");  
           
         }break;
         case CHARTEVENT_OBJECT_CHANGE:
         {
            Print("CHARTEVENT_OBJECT_CHANGE");  
         }break;
         case CHARTEVENT_OBJECT_DELETE:
         {
            Print("CHARTEVENT_OBJECT_DELETE");  
         }break;
         case CHARTEVENT_CLICK:
         {
               Print("CHARTEVENT_CLICK");  
         }break;
         case CHARTEVENT_OBJECT_CLICK:
         {
            PrintFormat("CHARTEVENT_OBJECT_CLICK");
            if(sparam == edit_name)
            {
               // no error here but does not change text
               if(ObjectSetString(0, edit_name,OBJPROP_TEXT,"MY CLICK TEXT") == false)
               {
                  Print("error");
               }
            }
         }break;
         case CHARTEVENT_OBJECT_DRAG:
         {
            PrintFormat("CHARTEVENT_OBJECT_DRAG");
         }break;
         
         case CHARTEVENT_OBJECT_ENDEDIT:
         {
            PrintFormat("CHARTEVENT_OBJECT_ENDEDIT");
         }break;
         case CHARTEVENT_CHART_CHANGE:
         {
            PrintFormat("CHARTEVENT_CHART_CHANGE");
         }break;
         
         case (CHARTEVENT_CUSTOM+1):
         {
            PrintFormat("CHARTEVENT_CUSTOM");
         }break;
         
         
         default: 
         {
                 PrintFormat("default");
         }break;
      }
   
}

So far i figured out, the point is in the focus, If you click in, the focus is set to this edit field

and until focus released, you won’t see the changes.

Unfortunately, it seems not to be possible to change focus programmatically (in MT4).

For instance, creating a hidden field and set the focus to this field after ObjectSetString in CHARTEVENT_OBJECT_CLICK.

If you want to set the text in the edit field then you need to capture the left-click down event instead of the object click event. In order to capture the left click down event on an object you need to turn on chartevent_mouse_move; check the event id == chartevent_mouse_move and sparam == “1”, and then check the coordinates. Here is a simple working example of setting the field text when the edit object is clicked.

//+------------------------------------------------------------------+
//|                                      PopulateEditWhenClicked.mq4 |
//|                                      Copyright 2018, nicholishen |
//|                                          http://forexfactory.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, nicholishen"
#property link      "http://forexfactory.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#include <ChartObjects\ChartObjectsTxtControls.mqh>
//+------------------------------------------------------------------+
class CClickDefaultEdit : public CChartObjectEdit
{
   int   m_count;
   bool  m_locked;
public:
   CClickDefaultEdit():m_count(0),m_locked(false)
   {
      ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true);
   }
   void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
   { 
      if(id==CHARTEVENT_OBJECT_ENDEDIT && sparam==Name())
         m_locked = false;
      if(!m_locked && id==CHARTEVENT_MOUSE_MOVE && sparam=="1")// left click down
      {
         int x = (int)lparam;
         int y = (int)dparam;
         if(x>=X_Distance() && x<=X_Distance()+X_Size() && y>=Y_Distance() && y<=Y_Distance()+Y_Size() )
         {
            Description(StringFormat("[%d] Clicks on Edit Object", ++m_count));
            m_locked = true;
         }
      }
   }
};
//+------------------------------------------------------------------+
CClickDefaultEdit g_edit;
//+------------------------------------------------------------------+
int OnInit()
{
   if(!g_edit.Create(0,"EDIT",0,50,50,250,30))
      return INIT_FAILED;
   return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   g_edit.OnChartEvent(id,lparam,dparam,sparam);
}
//+------------------------------------------------------------------+
int start(){ return 0; }

Yes, this works. Many thanks!

Well done, a great job. I should start considering this feature (CHARTEVENT_MOUSE_MOVE).

Thanks