MQL4参考 物件函数 ObjectDelete

ObjectDelete

The function removes the object with the specified name at the specified chart. There are two variants of the function:

bool  ObjectDelete(
   long     chart_id,     // chart ID
   string   object_name   // object name
   );

The function removes the object with the specified name:

bool  ObjectDelete(
   string   object_name   // object name
   );

Parameters

chart_id

[in]  Chart identifier.

object_name

[in]  Name of object to be deleted.

Return Value

Returns true if the removal was successful, otherwise returns false. To read more about the error call GetLastError().

Note

When the function is used with no chart ID specified, the function is supposed to be working with the current chart to which it has a direct access. In this case, the return value means the function execution result.

If the ID of a chart other than the current one is specified, the return value only informs whether the command has been added to the queue of that chart. In this case an asynchronous call is used, which means that the function does not wait for the execution of the command that has been added to the queue of another chart. Instead, it immediately returns control.

To check the result of command execution on a chart other than the current one, you can use a function that checks the specified object property. However, you should keep in mind that such functions are added to the end of the queue of that chart and wait for the execution result, and can therefore be time consuming. This feature should be taken into account when working with a large number of objects on a chart.

Example:

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   int i;
   long current_chart_id=ChartID();
//--- creates several objects of label type
   for(i=0; i<300; i+=10)
     {
      string obj_name="label_object"+IntegerToString(i);
      //--- creating label object (it does not have time/price coordinates)
      if(ObjectCreate(obj_name,OBJ_LABEL,0,0,0))
        {
         PrintFormat("Object %s created.",obj_name);
         //--- set random color
         ObjectSetInteger(current_chart_id,obj_name,OBJPROP_COLOR,MathRand());
         //--- set text property
         ObjectSetString(current_chart_id,obj_name,OBJPROP_TEXT,StringFormat("Simple Label at y= %d",i));
         //--- set distance property
         ObjectSet(obj_name,OBJPROP_XDISTANCE,i);
         ObjectSet(obj_name,OBJPROP_YDISTANCE,i);
         //-- forced chart redraw
         ChartRedraw(current_chart_id);
         Sleep(10);
        }
      else
        {
         Print("Error: can't create label! code #",GetLastError());
         return(0);
        }
     }
//--- sleep to see the objects created 
   Sleep(3000);
//--- delete all objects
   int obj_total=ObjectsTotal();
   PrintFormat("Total %d objects",obj_total);
   for(i=obj_total-1;i>=0;i--)
     {
      string name=ObjectName(i);
      PrintFormat("object %d: %s",i,name);
      ObjectDelete(name);
     }
   return(0);
  }