issue with deleting objects by text string in object name

 
The following code inserts two label objects onto the chart, each containing a common identified string, e.g. "_tbeg". When the EA is removed from the chart, I desire to remove all objects created by the EA. So, I'm using StringFind to locate the text string within the object name. However, when I remove the EA, only one of the objects gets deleted.

What am I doing wrong?

Thanks

Bill

//+------------------------------------------------------------------+
//|                                             TBEGlobals_v1[0].mq4 |
//+------------------------------------------------------------------+
 
extern int           TimeZoneShift  =  4;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
  
GlobalVariableSet("TimeZoneShiftGlobal",TimeZoneShift);   
 
// Insert labels
ObjectCreate("Title_tbeg",OBJ_LABEL,0,0,0);
ObjectSet("Title_tbeg",OBJPROP_XDISTANCE,2);
ObjectSet("Title_tbeg",OBJPROP_YDISTANCE,2);
ObjectSetText("Title_tbeg","TBE Global Settings",12,"Arial",Gray);
 
ObjectCreate("TimeZoneShift_tbeg",OBJ_LABEL,0,0,0);
ObjectSet("TimeZoneShift_tbeg",OBJPROP_XDISTANCE,2);
ObjectSet("TimeZoneShift_tbeg",OBJPROP_YDISTANCE,20);
ObjectSetText("TimeZoneShift_tbeg","TimeZoneShift: "+TimeZoneShift,9,"Arial",Gray);
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
// Remove labels EA posted to chart when EA is removed from chart
for(int i=0;i<=ObjectsTotal();i++)
   if(StringFind(ObjectName(i),"_tbeg",0)>0)
   {
   ObjectDelete(ObjectName(i));
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
Try to change loop direction
for(int i=ObjectsTotal()-1;i>=0;i--)
   if(StringFind(ObjectName(i),"_tbeg",0)>0)
      ObjectDelete(ObjectName(i));
 
stringo wrote:
Try to change loop direction
for(int i=ObjectsTotal()-1;i>=0;i--)
   if(StringFind(ObjectName(i),"_tbeg",0)>0)
      ObjectDelete(ObjectName(i));

That works. Thanks stringo!

Bill
 
billworld:
stringo wrote:
Try to change loop direction
for(int i=ObjectsTotal()-1;i>=0;i--)
   if(StringFind(ObjectName(i),"_tbeg",0)>0)
      ObjectDelete(ObjectName(i));

That works. Thanks stringo!

Bill

FWIW this no longer works. Maybe there was a change with how StringFind operates? Anyway, this is what I use now which works fine.


//+------------------------------------------------------------------+
//|                                                RemoveObjects.mq4 |
//|                                                   Bill Doerrfeld |
//|                                                billworld@mac.com |
//+------------------------------------------------------------------+
#property copyright "Bill Doerrfeld"
#property link      "billworld@mac.com"
#property show_inputs
 
extern string NameOfObject = "test";
 
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   for(int i = ObjectsTotal() - 1; i >= 0; i--)
     {
       string label = ObjectName(i);
       if(StringSubstr(label, 0, StringLen(NameOfObject)) != NameOfObject)
           continue;
       ObjectDelete(label);   
     } 
 
   return(0);
  }
//+------------------------------------------------------------------+
 
I've checked your code. It works. More details please
 
stringo:
I've checked your code. It works. More details please

To be clear, in my last post I supplied two code excerpts. The top one which uses StringFind does NOT work but it used to work in earlier versions of MT4. The bottom one which uses StringSubstr DOES work and this is the method I now use to remove objects. No big deal, I just struggled with trying to figure out why StringFind wasn't yielding the expected result until I just decided to go with StringSubstr.

 

Once more - it works.

Hint for your code

if(StringFind(ObjectName(i),"_tbeg",0)>0)

Your objects should have such names as "1_tbegTrendLine", "XXX_tbegSymbol" and so on. If your objects begin from "_tbeg" then You must change condition

if(StringFind(ObjectName(i),"_tbeg",0)==0)
or

if(StringFind(ObjectName(i),"_tbeg",0)>=0)

See https://docs.mql4.com/strings/StringFind. Position begins from 0. So was always.

Reason: