change text of OBJ_EDIT in chart event

To add comments, please log in or register
chinaski
1009
chinaski  

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;
      }
   
}
Petr Nosek
3528
Petr Nosek  
chinaski:

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

IMHO you won't achieve your goal this way. I think there are two buffers for the object. You can set/get the first by using OBJPROP_TEXT but the second you can't set/get.

In my opinion, the following is happening if you click into the object:

  1. the second buffer is filled by the first buffer
  2. you change the first buffer
  3. at the end of editing the first buffer is rewritten by the second buffer.

If you change something between this points in the first buffer (see 2.) it will be rewritten (see 3.)

Maybe I'm wrong. If you'll find a solution could be so kind and write me PM. Thanks

Alain Verleyen
40310
Alain Verleyen  
Petr Nosek:

IMHO you won't achieve your goal this way. I think there are two buffers for the object. You can set/get the first by using OBJPROP_TEXT but the second you can't set/get.

In my opinion, the following is happening if you click into the object:

  1. the second buffer is filled by the first buffer
  2. you change the first buffer
  3. at the end of editing the first buffer is rewritten by the second buffer.

If you change something between this points in the first buffer (see 2.) it will be rewritten (see 3.)

Maybe I'm wrong. If you'll find a solution could be so kind and write me PM. Thanks

Why PM ?

chinaski
1009
chinaski  

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.

Petr Nosek
3528
Petr Nosek  
Alain Verleyen:

Why PM ?

Only as an alert. Of course he should add this solution into this topic.
nicholi shen
2624
nicholi shen  
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; }

 
chinaski
1009
chinaski  
Yes, this works. Many thanks!
Petr Nosek
3528
Petr Nosek  
nicholishen:
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. 
 

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

Thanks

spottypegasus
16
spottypegasus  
nicholi shen:
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. 
 

Hi there....

I'm working on getting the edited value of OBJ_EDIT.... I am getting the old value every time...

It seemed like the buffer of OBJ_EDIT was not updated when I changed the text

Can anyone point at what I'm doing wrong?

Your help is greatly appreciated...


Regards...

...   
   ObjectCreate(0,"EditTP",OBJ_EDIT,0,0,0);
   
   ObjectSetInteger(0, "EditTP",OBJPROP_XDISTANCE,510);
   ObjectSetInteger(0, "EditTP",OBJPROP_YDISTANCE,202);
   
   ObjectSetInteger(0, "EditTP",OBJPROP_XSIZE,100);
   ObjectSetInteger(0, "EditTP",OBJPROP_YSIZE,18);
   
   ObjectSetInteger(0, "EditTP", OBJPROP_READONLY, false);

   ObjectSetString(0, "EditTP",OBJPROP_TEXT,"OLD VALUE");

...

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
  if(id==CHARTEVENT_OBJECT_ENDEDIT && sparam=="EditTP"){
     ObjectSetString(0, "EditTP", OBJPROP_TEXT, ObjectGetString(0,"EditTP",OBJPROP_TEXT));
     Print(ObjectGetString(0,"EditTP",OBJPROP_TEXT)); //Prints OLD VALUE
  }

// or

  if(id==CHARTEVENT_MOUSE_MOVE && sparam=="1"){// left click down
     ObjectSetString(0, "EditTP", OBJPROP_TEXT, ObjectGetString(0,"EditTP",OBJPROP_TEXT));
     Print(ObjectGetString(0,"EditTP",OBJPROP_TEXT)); //Prints OLD VALUE
  }  
} 
spottypegasus
16
spottypegasus  
spottypegasus:

Hi there....

I'm working on getting the edited value of OBJ_EDIT.... I am getting the old value every time...

It seemed like the buffer of OBJ_EDIT was not updated when I changed the text

Can anyone point at what I'm doing wrong?

Your help is greatly appreciated...


Regards...

Nevermind, I found the override of ObjectGetString function:


ObjectGetString(0,curr_obj_name,OBJPROP_TEXT,0,obj_text)


Thanks & Regards

To add comments, please log in or register